/
ReactElementJSX-test.js
412 lines (370 loc) · 13 KB
/
ReactElementJSX-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/
'use strict';
let React;
let ReactDOM;
let ReactTestUtils;
let jsxRuntime;
// NOTE: We're explicitly not using JSX here. This is intended to test
// a new React.jsx api which does not have a JSX transformer yet.
// A lot of these tests are pulled from ReactElement-test because
// this api is meant to be backwards compatible.
describe('ReactElement.jsx', () => {
let originalSymbol;
beforeEach(() => {
jest.resetModules();
// Delete the native Symbol if we have one to ensure we test the
// unpolyfilled environment.
originalSymbol = global.Symbol;
global.Symbol = undefined;
React = require('react');
jsxRuntime = require('react/jsx-runtime');
ReactDOM = require('react-dom');
ReactTestUtils = require('react-dom/test-utils');
});
afterEach(() => {
global.Symbol = originalSymbol;
});
if (!__EXPERIMENTAL__) {
it("empty test so Jest doesn't complain", () => {});
return;
}
it('allows static methods to be called using the type property', () => {
class StaticMethodComponentClass extends React.Component {
render() {
return jsxRuntime.jsx('div', {});
}
}
StaticMethodComponentClass.someStaticMethod = () => 'someReturnValue';
const element = jsxRuntime.jsx(StaticMethodComponentClass, {});
expect(element.type.someStaticMethod()).toBe('someReturnValue');
});
it('identifies valid elements', () => {
class Component extends React.Component {
render() {
return jsxRuntime.jsx('div', {});
}
}
expect(React.isValidElement(jsxRuntime.jsx('div', {}))).toEqual(true);
expect(React.isValidElement(jsxRuntime.jsx(Component, {}))).toEqual(true);
expect(React.isValidElement(null)).toEqual(false);
expect(React.isValidElement(true)).toEqual(false);
expect(React.isValidElement({})).toEqual(false);
expect(React.isValidElement('string')).toEqual(false);
if (!__EXPERIMENTAL__) {
let factory;
expect(() => {
factory = React.createFactory('div');
}).toWarnDev(
'Warning: React.createFactory() is deprecated and will be removed in a ' +
'future major release. Consider using JSX or use React.createElement() ' +
'directly instead.',
{withoutStack: true},
);
expect(React.isValidElement(factory)).toEqual(false);
}
expect(React.isValidElement(Component)).toEqual(false);
expect(React.isValidElement({type: 'div', props: {}})).toEqual(false);
const jsonElement = JSON.stringify(jsxRuntime.jsx('div', {}));
expect(React.isValidElement(JSON.parse(jsonElement))).toBe(true);
});
it('is indistinguishable from a plain object', () => {
const element = jsxRuntime.jsx('div', {className: 'foo'});
const object = {};
expect(element.constructor).toBe(object.constructor);
});
it('should use default prop value when removing a prop', () => {
class Component extends React.Component {
render() {
return jsxRuntime.jsx('span', {});
}
}
Component.defaultProps = {fruit: 'persimmon'};
const container = document.createElement('div');
const instance = ReactDOM.render(
jsxRuntime.jsx(Component, {fruit: 'mango'}),
container,
);
expect(instance.props.fruit).toBe('mango');
ReactDOM.render(jsxRuntime.jsx(Component, {}), container);
expect(instance.props.fruit).toBe('persimmon');
});
it('should normalize props with default values', () => {
class Component extends React.Component {
render() {
return jsxRuntime.jsx('span', {children: this.props.prop});
}
}
Component.defaultProps = {prop: 'testKey'};
const instance = ReactTestUtils.renderIntoDocument(
jsxRuntime.jsx(Component, {}),
);
expect(instance.props.prop).toBe('testKey');
const inst2 = ReactTestUtils.renderIntoDocument(
jsxRuntime.jsx(Component, {prop: null}),
);
expect(inst2.props.prop).toBe(null);
});
it('throws when changing a prop (in dev) after element creation', () => {
class Outer extends React.Component {
render() {
const el = jsxRuntime.jsx('div', {className: 'moo'});
if (__DEV__) {
expect(function() {
el.props.className = 'quack';
}).toThrow();
expect(el.props.className).toBe('moo');
} else {
el.props.className = 'quack';
expect(el.props.className).toBe('quack');
}
return el;
}
}
const outer = ReactTestUtils.renderIntoDocument(
jsxRuntime.jsx(Outer, {color: 'orange'}),
);
if (__DEV__) {
expect(ReactDOM.findDOMNode(outer).className).toBe('moo');
} else {
expect(ReactDOM.findDOMNode(outer).className).toBe('quack');
}
});
it('throws when adding a prop (in dev) after element creation', () => {
const container = document.createElement('div');
class Outer extends React.Component {
render() {
const el = jsxRuntime.jsx('div', {children: this.props.sound});
if (__DEV__) {
expect(function() {
el.props.className = 'quack';
}).toThrow();
expect(el.props.className).toBe(undefined);
} else {
el.props.className = 'quack';
expect(el.props.className).toBe('quack');
}
return el;
}
}
Outer.defaultProps = {sound: 'meow'};
const outer = ReactDOM.render(jsxRuntime.jsx(Outer, {}), container);
expect(ReactDOM.findDOMNode(outer).textContent).toBe('meow');
if (__DEV__) {
expect(ReactDOM.findDOMNode(outer).className).toBe('');
} else {
expect(ReactDOM.findDOMNode(outer).className).toBe('quack');
}
});
it('does not warn for NaN props', () => {
class Test extends React.Component {
render() {
return jsxRuntime.jsx('div', {});
}
}
const test = ReactTestUtils.renderIntoDocument(
jsxRuntime.jsx(Test, {value: +undefined}),
);
expect(test.props.value).toBeNaN();
});
it('should warn when `key` is being accessed on composite element', () => {
const container = document.createElement('div');
class Child extends React.Component {
render() {
return jsxRuntime.jsx('div', {children: this.props.key});
}
}
class Parent extends React.Component {
render() {
return jsxRuntime.jsxs('div', {
children: [
jsxRuntime.jsx(Child, {}, '0'),
jsxRuntime.jsx(Child, {}, '1'),
jsxRuntime.jsx(Child, {}, '2'),
],
});
}
}
expect(() =>
ReactDOM.render(jsxRuntime.jsx(Parent, {}), container),
).toErrorDev(
'Child: `key` is not a prop. Trying to access it will result ' +
'in `undefined` being returned. If you need to access the same ' +
'value within the child component, you should pass it as a different ' +
'prop. (https://fb.me/react-special-props)',
);
});
it('warns when a jsxs is passed something that is not an array', () => {
const container = document.createElement('div');
expect(() =>
ReactDOM.render(
jsxRuntime.jsxs('div', {children: 'foo'}, null),
container,
),
).toErrorDev(
'React.jsx: Static children should always be an array. ' +
'You are likely explicitly calling React.jsxs or React.jsxDEV. ' +
'Use the Babel transform instead.',
{withoutStack: true},
);
});
it('should warn when `key` is being accessed on a host element', () => {
const element = jsxRuntime.jsxs('div', {}, '3');
expect(
() => void element.props.key,
).toErrorDev(
'div: `key` is not a prop. Trying to access it will result ' +
'in `undefined` being returned. If you need to access the same ' +
'value within the child component, you should pass it as a different ' +
'prop. (https://fb.me/react-special-props)',
{withoutStack: true},
);
});
it('should warn when `ref` is being accessed', () => {
const container = document.createElement('div');
class Child extends React.Component {
render() {
return jsxRuntime.jsx('div', {children: this.props.ref});
}
}
class Parent extends React.Component {
render() {
return jsxRuntime.jsx('div', {
children: jsxRuntime.jsx(Child, {ref: 'childElement'}),
});
}
}
expect(() =>
ReactDOM.render(jsxRuntime.jsx(Parent, {}), container),
).toErrorDev(
'Child: `ref` is not a prop. Trying to access it will result ' +
'in `undefined` being returned. If you need to access the same ' +
'value within the child component, you should pass it as a different ' +
'prop. (https://fb.me/react-special-props)',
);
});
it('identifies elements, but not JSON, if Symbols are supported', () => {
// Rudimentary polyfill
// Once all jest engines support Symbols natively we can swap this to test
// WITH native Symbols by default.
const REACT_ELEMENT_TYPE = function() {}; // fake Symbol
const OTHER_SYMBOL = function() {}; // another fake Symbol
global.Symbol = function(name) {
return OTHER_SYMBOL;
};
global.Symbol.for = function(key) {
if (key === 'react.element') {
return REACT_ELEMENT_TYPE;
}
return OTHER_SYMBOL;
};
jest.resetModules();
React = require('react');
jsxRuntime = require('react/jsx-runtime');
class Component extends React.Component {
render() {
return jsxRuntime.jsx('div');
}
}
expect(React.isValidElement(jsxRuntime.jsx('div', {}))).toEqual(true);
expect(React.isValidElement(jsxRuntime.jsx(Component, {}))).toEqual(true);
expect(React.isValidElement(null)).toEqual(false);
expect(React.isValidElement(true)).toEqual(false);
expect(React.isValidElement({})).toEqual(false);
expect(React.isValidElement('string')).toEqual(false);
if (!__EXPERIMENTAL__) {
let factory;
expect(() => {
factory = React.createFactory('div');
}).toWarnDev(
'Warning: React.createFactory() is deprecated and will be removed in a ' +
'future major release. Consider using JSX or use React.createElement() ' +
'directly instead.',
{withoutStack: true},
);
expect(React.isValidElement(factory)).toEqual(false);
}
expect(React.isValidElement(Component)).toEqual(false);
expect(React.isValidElement({type: 'div', props: {}})).toEqual(false);
const jsonElement = JSON.stringify(jsxRuntime.jsx('div', {}));
expect(React.isValidElement(JSON.parse(jsonElement))).toBe(false);
});
it('should warn when unkeyed children are passed to jsx', () => {
const container = document.createElement('div');
class Child extends React.Component {
render() {
return jsxRuntime.jsx('div', {});
}
}
class Parent extends React.Component {
render() {
return jsxRuntime.jsx('div', {
children: [
jsxRuntime.jsx(Child, {}),
jsxRuntime.jsx(Child, {}),
jsxRuntime.jsx(Child, {}),
],
});
}
}
expect(() =>
ReactDOM.render(jsxRuntime.jsx(Parent, {}), container),
).toErrorDev(
'Warning: Each child in a list should have a unique "key" prop.\n\n' +
'Check the render method of `Parent`. See https://fb.me/react-warning-keys for more information.\n' +
' in Child (created by Parent)\n' +
' in Parent',
);
});
if (require('shared/ReactFeatureFlags').warnAboutSpreadingKeyToJSX) {
it('should warn when keys are passed as part of props', () => {
const container = document.createElement('div');
class Child extends React.Component {
render() {
return jsxRuntime.jsx('div', {});
}
}
class Parent extends React.Component {
render() {
return jsxRuntime.jsx('div', {
children: [jsxRuntime.jsx(Child, {key: '0'})],
});
}
}
expect(() =>
ReactDOM.render(jsxRuntime.jsx(Parent, {}), container),
).toErrorDev(
'Warning: React.jsx: Spreading a key to JSX is a deprecated pattern. ' +
'Explicitly pass a key after spreading props in your JSX call. ' +
'E.g. <Child {...props} key={key} />',
);
});
}
it('should not warn when unkeyed children are passed to jsxs', () => {
const container = document.createElement('div');
class Child extends React.Component {
render() {
return jsxRuntime.jsx('div', {});
}
}
class Parent extends React.Component {
render() {
return jsxRuntime.jsxs('div', {
children: [
jsxRuntime.jsx(Child, {}),
jsxRuntime.jsx(Child, {}),
jsxRuntime.jsx(Child, {}),
],
});
}
}
// TODO: an explicit expect for no warning?
ReactDOM.render(jsxRuntime.jsx(Parent, {}), container);
});
});