12
12
let React ;
13
13
let ReactDOM ;
14
14
let ReactTestUtils ;
15
+ let jsxRuntime ;
15
16
16
17
// NOTE: We're explicitly not using JSX here. This is intended to test
17
18
// a new React.jsx api which does not have a JSX transformer yet.
@@ -29,6 +30,7 @@ describe('ReactElement.jsx', () => {
29
30
global . Symbol = undefined ;
30
31
31
32
React = require ( 'react' ) ;
33
+ jsxRuntime = require ( 'react/jsx-runtime' ) ;
32
34
ReactDOM = require ( 'react-dom' ) ;
33
35
ReactTestUtils = require ( 'react-dom/test-utils' ) ;
34
36
} ) ;
@@ -45,24 +47,24 @@ describe('ReactElement.jsx', () => {
45
47
it ( 'allows static methods to be called using the type property' , ( ) => {
46
48
class StaticMethodComponentClass extends React . Component {
47
49
render ( ) {
48
- return React . jsx ( 'div' , { } ) ;
50
+ return jsxRuntime . jsx ( 'div' , { } ) ;
49
51
}
50
52
}
51
53
StaticMethodComponentClass . someStaticMethod = ( ) => 'someReturnValue' ;
52
54
53
- const element = React . jsx ( StaticMethodComponentClass , { } ) ;
55
+ const element = jsxRuntime . jsx ( StaticMethodComponentClass , { } ) ;
54
56
expect ( element . type . someStaticMethod ( ) ) . toBe ( 'someReturnValue' ) ;
55
57
} ) ;
56
58
57
59
it ( 'identifies valid elements' , ( ) => {
58
60
class Component extends React . Component {
59
61
render ( ) {
60
- return React . jsx ( 'div' , { } ) ;
62
+ return jsxRuntime . jsx ( 'div' , { } ) ;
61
63
}
62
64
}
63
65
64
- expect ( React . isValidElement ( React . jsx ( 'div' , { } ) ) ) . toEqual ( true ) ;
65
- expect ( React . isValidElement ( React . jsx ( Component , { } ) ) ) . toEqual ( true ) ;
66
+ expect ( React . isValidElement ( jsxRuntime . jsx ( 'div' , { } ) ) ) . toEqual ( true ) ;
67
+ expect ( React . isValidElement ( jsxRuntime . jsx ( Component , { } ) ) ) . toEqual ( true ) ;
66
68
67
69
expect ( React . isValidElement ( null ) ) . toEqual ( false ) ;
68
70
expect ( React . isValidElement ( true ) ) . toEqual ( false ) ;
@@ -83,58 +85,58 @@ describe('ReactElement.jsx', () => {
83
85
expect ( React . isValidElement ( Component ) ) . toEqual ( false ) ;
84
86
expect ( React . isValidElement ( { type : 'div' , props : { } } ) ) . toEqual ( false ) ;
85
87
86
- const jsonElement = JSON . stringify ( React . jsx ( 'div' , { } ) ) ;
88
+ const jsonElement = JSON . stringify ( jsxRuntime . jsx ( 'div' , { } ) ) ;
87
89
expect ( React . isValidElement ( JSON . parse ( jsonElement ) ) ) . toBe ( true ) ;
88
90
} ) ;
89
91
90
92
it ( 'is indistinguishable from a plain object' , ( ) => {
91
- const element = React . jsx ( 'div' , { className : 'foo' } ) ;
93
+ const element = jsxRuntime . jsx ( 'div' , { className : 'foo' } ) ;
92
94
const object = { } ;
93
95
expect ( element . constructor ) . toBe ( object . constructor ) ;
94
96
} ) ;
95
97
96
98
it ( 'should use default prop value when removing a prop' , ( ) => {
97
99
class Component extends React . Component {
98
100
render ( ) {
99
- return React . jsx ( 'span' , { } ) ;
101
+ return jsxRuntime . jsx ( 'span' , { } ) ;
100
102
}
101
103
}
102
104
Component . defaultProps = { fruit : 'persimmon' } ;
103
105
104
106
const container = document . createElement ( 'div' ) ;
105
107
const instance = ReactDOM . render (
106
- React . jsx ( Component , { fruit : 'mango' } ) ,
108
+ jsxRuntime . jsx ( Component , { fruit : 'mango' } ) ,
107
109
container ,
108
110
) ;
109
111
expect ( instance . props . fruit ) . toBe ( 'mango' ) ;
110
112
111
- ReactDOM . render ( React . jsx ( Component , { } ) , container ) ;
113
+ ReactDOM . render ( jsxRuntime . jsx ( Component , { } ) , container ) ;
112
114
expect ( instance . props . fruit ) . toBe ( 'persimmon' ) ;
113
115
} ) ;
114
116
115
117
it ( 'should normalize props with default values' , ( ) => {
116
118
class Component extends React . Component {
117
119
render ( ) {
118
- return React . jsx ( 'span' , { children : this . props . prop } ) ;
120
+ return jsxRuntime . jsx ( 'span' , { children : this . props . prop } ) ;
119
121
}
120
122
}
121
123
Component . defaultProps = { prop : 'testKey' } ;
122
124
123
125
const instance = ReactTestUtils . renderIntoDocument (
124
- React . jsx ( Component , { } ) ,
126
+ jsxRuntime . jsx ( Component , { } ) ,
125
127
) ;
126
128
expect ( instance . props . prop ) . toBe ( 'testKey' ) ;
127
129
128
130
const inst2 = ReactTestUtils . renderIntoDocument (
129
- React . jsx ( Component , { prop : null } ) ,
131
+ jsxRuntime . jsx ( Component , { prop : null } ) ,
130
132
) ;
131
133
expect ( inst2 . props . prop ) . toBe ( null ) ;
132
134
} ) ;
133
135
134
136
it ( 'throws when changing a prop (in dev) after element creation' , ( ) => {
135
137
class Outer extends React . Component {
136
138
render ( ) {
137
- const el = React . jsx ( 'div' , { className : 'moo' } ) ;
139
+ const el = jsxRuntime . jsx ( 'div' , { className : 'moo' } ) ;
138
140
139
141
if ( __DEV__ ) {
140
142
expect ( function ( ) {
@@ -150,7 +152,7 @@ describe('ReactElement.jsx', () => {
150
152
}
151
153
}
152
154
const outer = ReactTestUtils . renderIntoDocument (
153
- React . jsx ( Outer , { color : 'orange' } ) ,
155
+ jsxRuntime . jsx ( Outer , { color : 'orange' } ) ,
154
156
) ;
155
157
if ( __DEV__ ) {
156
158
expect ( ReactDOM . findDOMNode ( outer ) . className ) . toBe ( 'moo' ) ;
@@ -163,7 +165,7 @@ describe('ReactElement.jsx', () => {
163
165
const container = document . createElement ( 'div' ) ;
164
166
class Outer extends React . Component {
165
167
render ( ) {
166
- const el = React . jsx ( 'div' , { children : this . props . sound } ) ;
168
+ const el = jsxRuntime . jsx ( 'div' , { children : this . props . sound } ) ;
167
169
168
170
if ( __DEV__ ) {
169
171
expect ( function ( ) {
@@ -179,7 +181,7 @@ describe('ReactElement.jsx', () => {
179
181
}
180
182
}
181
183
Outer . defaultProps = { sound : 'meow' } ;
182
- const outer = ReactDOM . render ( React . jsx ( Outer , { } ) , container ) ;
184
+ const outer = ReactDOM . render ( jsxRuntime . jsx ( Outer , { } ) , container ) ;
183
185
expect ( ReactDOM . findDOMNode ( outer ) . textContent ) . toBe ( 'meow' ) ;
184
186
if ( __DEV__ ) {
185
187
expect ( ReactDOM . findDOMNode ( outer ) . className ) . toBe ( '' ) ;
@@ -191,11 +193,11 @@ describe('ReactElement.jsx', () => {
191
193
it ( 'does not warn for NaN props' , ( ) => {
192
194
class Test extends React . Component {
193
195
render ( ) {
194
- return React . jsx ( 'div' , { } ) ;
196
+ return jsxRuntime . jsx ( 'div' , { } ) ;
195
197
}
196
198
}
197
199
const test = ReactTestUtils . renderIntoDocument (
198
- React . jsx ( Test , { value : + undefined } ) ,
200
+ jsxRuntime . jsx ( Test , { value : + undefined } ) ,
199
201
) ;
200
202
expect ( test . props . value ) . toBeNaN ( ) ;
201
203
} ) ;
@@ -204,21 +206,23 @@ describe('ReactElement.jsx', () => {
204
206
const container = document . createElement ( 'div' ) ;
205
207
class Child extends React . Component {
206
208
render ( ) {
207
- return React . jsx ( 'div' , { children : this . props . key } ) ;
209
+ return jsxRuntime . jsx ( 'div' , { children : this . props . key } ) ;
208
210
}
209
211
}
210
212
class Parent extends React . Component {
211
213
render ( ) {
212
- return React . jsxs ( 'div' , {
214
+ return jsxRuntime . jsxs ( 'div' , {
213
215
children : [
214
- React . jsx ( Child , { } , '0' ) ,
215
- React . jsx ( Child , { } , '1' ) ,
216
- React . jsx ( Child , { } , '2' ) ,
216
+ jsxRuntime . jsx ( Child , { } , '0' ) ,
217
+ jsxRuntime . jsx ( Child , { } , '1' ) ,
218
+ jsxRuntime . jsx ( Child , { } , '2' ) ,
217
219
] ,
218
220
} ) ;
219
221
}
220
222
}
221
- expect ( ( ) => ReactDOM . render ( React . jsx ( Parent , { } ) , container ) ) . toErrorDev (
223
+ expect ( ( ) =>
224
+ ReactDOM . render ( jsxRuntime . jsx ( Parent , { } ) , container ) ,
225
+ ) . toErrorDev (
222
226
'Child: `key` is not a prop. Trying to access it will result ' +
223
227
'in `undefined` being returned. If you need to access the same ' +
224
228
'value within the child component, you should pass it as a different ' +
@@ -229,7 +233,10 @@ describe('ReactElement.jsx', () => {
229
233
it ( 'warns when a jsxs is passed something that is not an array' , ( ) => {
230
234
const container = document . createElement ( 'div' ) ;
231
235
expect ( ( ) =>
232
- ReactDOM . render ( React . jsxs ( 'div' , { children : 'foo' } , null ) , container ) ,
236
+ ReactDOM . render (
237
+ jsxRuntime . jsxs ( 'div' , { children : 'foo' } , null ) ,
238
+ container ,
239
+ ) ,
233
240
) . toErrorDev (
234
241
'React.jsx: Static children should always be an array. ' +
235
242
'You are likely explicitly calling React.jsxs or React.jsxDEV. ' +
@@ -239,7 +246,7 @@ describe('ReactElement.jsx', () => {
239
246
} ) ;
240
247
241
248
it ( 'should warn when `key` is being accessed on a host element' , ( ) => {
242
- const element = React . jsxs ( 'div' , { } , '3' ) ;
249
+ const element = jsxRuntime . jsxs ( 'div' , { } , '3' ) ;
243
250
expect (
244
251
( ) => void element . props . key ,
245
252
) . toErrorDev (
@@ -255,17 +262,19 @@ describe('ReactElement.jsx', () => {
255
262
const container = document . createElement ( 'div' ) ;
256
263
class Child extends React . Component {
257
264
render ( ) {
258
- return React . jsx ( 'div' , { children : this . props . ref } ) ;
265
+ return jsxRuntime . jsx ( 'div' , { children : this . props . ref } ) ;
259
266
}
260
267
}
261
268
class Parent extends React . Component {
262
269
render ( ) {
263
- return React . jsx ( 'div' , {
264
- children : React . jsx ( Child , { ref : 'childElement' } ) ,
270
+ return jsxRuntime . jsx ( 'div' , {
271
+ children : jsxRuntime . jsx ( Child , { ref : 'childElement' } ) ,
265
272
} ) ;
266
273
}
267
274
}
268
- expect ( ( ) => ReactDOM . render ( React . jsx ( Parent , { } ) , container ) ) . toErrorDev (
275
+ expect ( ( ) =>
276
+ ReactDOM . render ( jsxRuntime . jsx ( Parent , { } ) , container ) ,
277
+ ) . toErrorDev (
269
278
'Child: `ref` is not a prop. Trying to access it will result ' +
270
279
'in `undefined` being returned. If you need to access the same ' +
271
280
'value within the child component, you should pass it as a different ' +
@@ -292,15 +301,16 @@ describe('ReactElement.jsx', () => {
292
301
jest . resetModules ( ) ;
293
302
294
303
React = require ( 'react' ) ;
304
+ jsxRuntime = require ( 'react/jsx-runtime' ) ;
295
305
296
306
class Component extends React . Component {
297
307
render ( ) {
298
- return React . jsx ( 'div' ) ;
308
+ return jsxRuntime . jsx ( 'div' ) ;
299
309
}
300
310
}
301
311
302
- expect ( React . isValidElement ( React . jsx ( 'div' , { } ) ) ) . toEqual ( true ) ;
303
- expect ( React . isValidElement ( React . jsx ( Component , { } ) ) ) . toEqual ( true ) ;
312
+ expect ( React . isValidElement ( jsxRuntime . jsx ( 'div' , { } ) ) ) . toEqual ( true ) ;
313
+ expect ( React . isValidElement ( jsxRuntime . jsx ( Component , { } ) ) ) . toEqual ( true ) ;
304
314
305
315
expect ( React . isValidElement ( null ) ) . toEqual ( false ) ;
306
316
expect ( React . isValidElement ( true ) ) . toEqual ( false ) ;
@@ -321,29 +331,31 @@ describe('ReactElement.jsx', () => {
321
331
expect ( React . isValidElement ( Component ) ) . toEqual ( false ) ;
322
332
expect ( React . isValidElement ( { type : 'div' , props : { } } ) ) . toEqual ( false ) ;
323
333
324
- const jsonElement = JSON . stringify ( React . jsx ( 'div' , { } ) ) ;
334
+ const jsonElement = JSON . stringify ( jsxRuntime . jsx ( 'div' , { } ) ) ;
325
335
expect ( React . isValidElement ( JSON . parse ( jsonElement ) ) ) . toBe ( false ) ;
326
336
} ) ;
327
337
328
338
it ( 'should warn when unkeyed children are passed to jsx' , ( ) => {
329
339
const container = document . createElement ( 'div' ) ;
330
340
class Child extends React . Component {
331
341
render ( ) {
332
- return React . jsx ( 'div' , { } ) ;
342
+ return jsxRuntime . jsx ( 'div' , { } ) ;
333
343
}
334
344
}
335
345
class Parent extends React . Component {
336
346
render ( ) {
337
- return React . jsx ( 'div' , {
347
+ return jsxRuntime . jsx ( 'div' , {
338
348
children : [
339
- React . jsx ( Child , { } ) ,
340
- React . jsx ( Child , { } ) ,
341
- React . jsx ( Child , { } ) ,
349
+ jsxRuntime . jsx ( Child , { } ) ,
350
+ jsxRuntime . jsx ( Child , { } ) ,
351
+ jsxRuntime . jsx ( Child , { } ) ,
342
352
] ,
343
353
} ) ;
344
354
}
345
355
}
346
- expect ( ( ) => ReactDOM . render ( React . jsx ( Parent , { } ) , container ) ) . toErrorDev (
356
+ expect ( ( ) =>
357
+ ReactDOM . render ( jsxRuntime . jsx ( Parent , { } ) , container ) ,
358
+ ) . toErrorDev (
347
359
'Warning: Each child in a list should have a unique "key" prop.\n\n' +
348
360
'Check the render method of `Parent`. See https://fb.me/react-warning-keys for more information.\n' +
349
361
' in Child (created by Parent)\n' +
@@ -356,18 +368,18 @@ describe('ReactElement.jsx', () => {
356
368
const container = document . createElement ( 'div' ) ;
357
369
class Child extends React . Component {
358
370
render ( ) {
359
- return React . jsx ( 'div' , { } ) ;
371
+ return jsxRuntime . jsx ( 'div' , { } ) ;
360
372
}
361
373
}
362
374
class Parent extends React . Component {
363
375
render ( ) {
364
- return React . jsx ( 'div' , {
365
- children : [ React . jsx ( Child , { key : '0' } ) ] ,
376
+ return jsxRuntime . jsx ( 'div' , {
377
+ children : [ jsxRuntime . jsx ( Child , { key : '0' } ) ] ,
366
378
} ) ;
367
379
}
368
380
}
369
381
expect ( ( ) =>
370
- ReactDOM . render ( React . jsx ( Parent , { } ) , container ) ,
382
+ ReactDOM . render ( jsxRuntime . jsx ( Parent , { } ) , container ) ,
371
383
) . toErrorDev (
372
384
'Warning: React.jsx: Spreading a key to JSX is a deprecated pattern. ' +
373
385
'Explicitly pass a key after spreading props in your JSX call. ' +
@@ -380,21 +392,21 @@ describe('ReactElement.jsx', () => {
380
392
const container = document . createElement ( 'div' ) ;
381
393
class Child extends React . Component {
382
394
render ( ) {
383
- return React . jsx ( 'div' , { } ) ;
395
+ return jsxRuntime . jsx ( 'div' , { } ) ;
384
396
}
385
397
}
386
398
class Parent extends React . Component {
387
399
render ( ) {
388
- return React . jsxs ( 'div' , {
400
+ return jsxRuntime . jsxs ( 'div' , {
389
401
children : [
390
- React . jsx ( Child , { } ) ,
391
- React . jsx ( Child , { } ) ,
392
- React . jsx ( Child , { } ) ,
402
+ jsxRuntime . jsx ( Child , { } ) ,
403
+ jsxRuntime . jsx ( Child , { } ) ,
404
+ jsxRuntime . jsx ( Child , { } ) ,
393
405
] ,
394
406
} ) ;
395
407
}
396
408
}
397
409
// TODO: an explicit expect for no warning?
398
- ReactDOM . render ( React . jsx ( Parent , { } ) , container ) ;
410
+ ReactDOM . render ( jsxRuntime . jsx ( Parent , { } ) , container ) ;
399
411
} ) ;
400
412
} ) ;
0 commit comments