forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReactART-test.js
482 lines (410 loc) · 12.7 KB
/
ReactART-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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/**
* 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
*/
/*jslint evil: true */
'use strict';
import * as React from 'react';
import * as ReactART from 'react-art';
import ARTSVGMode from 'art/modes/svg';
import ARTCurrentMode from 'art/modes/current';
// Since these are default exports, we need to import them using ESM.
// Since they must be on top, we need to import this before ReactDOM.
import Circle from 'react-art/Circle';
import Rectangle from 'react-art/Rectangle';
import Wedge from 'react-art/Wedge';
// Isolate DOM renderer.
jest.resetModules();
const ReactDOM = require('react-dom');
const ReactTestUtils = require('react-dom/test-utils');
// Isolate test renderer.
jest.resetModules();
const ReactTestRenderer = require('react-test-renderer');
// Isolate the noop renderer
jest.resetModules();
const ReactNoop = require('react-noop-renderer');
const Scheduler = require('scheduler');
let Group;
let Shape;
let Surface;
let TestComponent;
const Missing = {};
function testDOMNodeStructure(domNode, expectedStructure) {
expect(domNode).toBeDefined();
expect(domNode.nodeName).toBe(expectedStructure.nodeName);
for (const prop in expectedStructure) {
if (!expectedStructure.hasOwnProperty(prop)) {
continue;
}
if (prop !== 'nodeName' && prop !== 'children') {
if (expectedStructure[prop] === Missing) {
expect(domNode.hasAttribute(prop)).toBe(false);
} else {
expect(domNode.getAttribute(prop)).toBe(expectedStructure[prop]);
}
}
}
if (expectedStructure.children) {
expectedStructure.children.forEach(function(subTree, index) {
testDOMNodeStructure(domNode.childNodes[index], subTree);
});
}
}
describe('ReactART', () => {
let container;
beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
ARTCurrentMode.setCurrent(ARTSVGMode);
Group = ReactART.Group;
Shape = ReactART.Shape;
Surface = ReactART.Surface;
TestComponent = class extends React.Component {
group = React.createRef();
render() {
const a = (
<Shape
d="M0,0l50,0l0,50l-50,0z"
fill={new ReactART.LinearGradient(['black', 'white'])}
key="a"
width={50}
height={50}
x={50}
y={50}
opacity={0.1}
/>
);
const b = (
<Shape
fill="#3C5A99"
key="b"
scale={0.5}
x={50}
y={50}
title="This is an F"
cursor="pointer">
M64.564,38.583H54l0.008-5.834c0-3.035,0.293-4.666,4.657-4.666
h5.833V16.429h-9.33c-11.213,0-15.159,5.654-15.159,15.16v6.994
h-6.99v11.652h6.99v33.815H54V50.235h9.331L64.564,38.583z
</Shape>
);
const c = <Group key="c" />;
return (
<Surface width={150} height={200}>
<Group ref={this.group}>
{this.props.flipped ? [b, a, c] : [a, b, c]}
</Group>
</Surface>
);
}
};
});
afterEach(() => {
document.body.removeChild(container);
container = null;
});
it('should have the correct lifecycle state', () => {
let instance = <TestComponent />;
instance = ReactTestUtils.renderIntoDocument(instance);
const group = instance.group.current;
// Duck type test for an ART group
expect(typeof group.indicate).toBe('function');
});
it('should render a reasonable SVG structure in SVG mode', () => {
let instance = <TestComponent />;
instance = ReactTestUtils.renderIntoDocument(instance);
const expectedStructure = {
nodeName: 'svg',
width: '150',
height: '200',
children: [
{nodeName: 'defs'},
{
nodeName: 'g',
children: [
{
nodeName: 'defs',
children: [{nodeName: 'linearGradient'}],
},
{nodeName: 'path'},
{nodeName: 'path'},
{nodeName: 'g'},
],
},
],
};
const realNode = ReactDOM.findDOMNode(instance);
testDOMNodeStructure(realNode, expectedStructure);
});
it('should be able to reorder components', () => {
const instance = ReactDOM.render(
<TestComponent flipped={false} />,
container,
);
const expectedStructure = {
nodeName: 'svg',
children: [
{nodeName: 'defs'},
{
nodeName: 'g',
children: [
{nodeName: 'defs'},
{nodeName: 'path', opacity: '0.1'},
{nodeName: 'path', opacity: Missing},
{nodeName: 'g'},
],
},
],
};
const realNode = ReactDOM.findDOMNode(instance);
testDOMNodeStructure(realNode, expectedStructure);
ReactDOM.render(<TestComponent flipped={true} />, container);
const expectedNewStructure = {
nodeName: 'svg',
children: [
{nodeName: 'defs'},
{
nodeName: 'g',
children: [
{nodeName: 'defs'},
{nodeName: 'path', opacity: Missing},
{nodeName: 'path', opacity: '0.1'},
{nodeName: 'g'},
],
},
],
};
testDOMNodeStructure(realNode, expectedNewStructure);
});
it('should be able to reorder many components', () => {
class Component extends React.Component {
render() {
const chars = this.props.chars.split('');
return (
<Surface>
{chars.map(text => (
<Shape key={text} title={text} />
))}
</Surface>
);
}
}
// Mini multi-child stress test: lots of reorders, some adds, some removes.
const before = 'abcdefghijklmnopqrst';
const after = 'mxhpgwfralkeoivcstzy';
let instance = ReactDOM.render(<Component chars={before} />, container);
const realNode = ReactDOM.findDOMNode(instance);
expect(realNode.textContent).toBe(before);
instance = ReactDOM.render(<Component chars={after} />, container);
expect(realNode.textContent).toBe(after);
ReactDOM.unmountComponentAtNode(container);
});
it('renders composite with lifecycle inside group', () => {
let mounted = false;
class CustomShape extends React.Component {
render() {
return <Shape />;
}
componentDidMount() {
mounted = true;
}
}
ReactTestUtils.renderIntoDocument(
<Surface>
<Group>
<CustomShape />
</Group>
</Surface>,
);
expect(mounted).toBe(true);
});
it('resolves refs before componentDidMount', () => {
class CustomShape extends React.Component {
render() {
return <Shape />;
}
}
let ref = null;
class Outer extends React.Component {
test = React.createRef();
componentDidMount() {
ref = this.test.current;
}
render() {
return (
<Surface>
<Group>
<CustomShape ref={this.test} />
</Group>
</Surface>
);
}
}
ReactTestUtils.renderIntoDocument(<Outer />);
expect(ref.constructor).toBe(CustomShape);
});
it('resolves refs before componentDidUpdate', () => {
class CustomShape extends React.Component {
render() {
return <Shape />;
}
}
let ref = {};
class Outer extends React.Component {
test = React.createRef();
componentDidMount() {
ref = this.test.current;
}
componentDidUpdate() {
ref = this.test.current;
}
render() {
return (
<Surface>
<Group>
{this.props.mountCustomShape && <CustomShape ref={this.test} />}
</Group>
</Surface>
);
}
}
ReactDOM.render(<Outer />, container);
expect(ref).toBe(null);
ReactDOM.render(<Outer mountCustomShape={true} />, container);
expect(ref.constructor).toBe(CustomShape);
});
it('adds and updates event handlers', () => {
function render(onClick) {
return ReactDOM.render(
<Surface>
<Shape onClick={onClick} />
</Surface>,
container,
);
}
function doClick(instance) {
const path = ReactDOM.findDOMNode(instance).querySelector('path');
path.dispatchEvent(
new MouseEvent('click', {
bubbles: true,
}),
);
}
const onClick1 = jest.fn();
let instance = render(onClick1);
doClick(instance);
expect(onClick1).toBeCalled();
const onClick2 = jest.fn();
instance = render(onClick2);
doClick(instance);
expect(onClick2).toBeCalled();
});
it('can concurrently render with a "primary" renderer while sharing context', () => {
const CurrentRendererContext = React.createContext(null);
function Yield(props) {
Scheduler.unstable_yieldValue(props.value);
return null;
}
let ops = [];
function LogCurrentRenderer() {
return (
<CurrentRendererContext.Consumer>
{currentRenderer => {
ops.push(currentRenderer);
return null;
}}
</CurrentRendererContext.Consumer>
);
}
// Using test renderer instead of the DOM renderer here because async
// testing APIs for the DOM renderer don't exist.
ReactNoop.render(
<CurrentRendererContext.Provider value="Test">
<Yield value="A" />
<Yield value="B" />
<LogCurrentRenderer />
<Yield value="C" />
</CurrentRendererContext.Provider>,
);
expect(Scheduler).toFlushAndYieldThrough(['A']);
ReactDOM.render(
<Surface>
<LogCurrentRenderer />
<CurrentRendererContext.Provider value="ART">
<LogCurrentRenderer />
</CurrentRendererContext.Provider>
</Surface>,
container,
);
expect(ops).toEqual([null, 'ART']);
ops = [];
expect(Scheduler).toFlushAndYield(['B', 'C']);
expect(ops).toEqual(['Test']);
});
});
describe('ReactARTComponents', () => {
it('should generate a <Shape> with props for drawing the Circle', () => {
const circle = ReactTestRenderer.create(
<Circle radius={10} stroke="green" strokeWidth={3} fill="blue" />,
);
expect(circle.toJSON()).toMatchSnapshot();
});
it('should warn if radius is missing on a Circle component', () => {
expect(() =>
ReactTestRenderer.create(
<Circle stroke="green" strokeWidth={3} fill="blue" />,
),
).toErrorDev(
'Warning: Failed prop type: The prop `radius` is marked as required in `Circle`, ' +
'but its value is `undefined`.' +
'\n in Circle (at **)',
);
});
it('should generate a <Shape> with props for drawing the Rectangle', () => {
const rectangle = ReactTestRenderer.create(
<Rectangle width={50} height={50} stroke="green" fill="blue" />,
);
expect(rectangle.toJSON()).toMatchSnapshot();
});
it('should warn if width/height is missing on a Rectangle component', () => {
expect(() =>
ReactTestRenderer.create(<Rectangle stroke="green" fill="blue" />),
).toErrorDev([
'Warning: Failed prop type: The prop `width` is marked as required in `Rectangle`, ' +
'but its value is `undefined`.' +
'\n in Rectangle (at **)',
'Warning: Failed prop type: The prop `height` is marked as required in `Rectangle`, ' +
'but its value is `undefined`.' +
'\n in Rectangle (at **)',
]);
});
it('should generate a <Shape> with props for drawing the Wedge', () => {
const wedge = ReactTestRenderer.create(
<Wedge outerRadius={50} startAngle={0} endAngle={360} fill="blue" />,
);
expect(wedge.toJSON()).toMatchSnapshot();
});
it('should return null if startAngle equals to endAngle on Wedge', () => {
const wedge = ReactTestRenderer.create(
<Wedge outerRadius={50} startAngle={0} endAngle={0} fill="blue" />,
);
expect(wedge.toJSON()).toBeNull();
});
it('should warn if outerRadius/startAngle/endAngle is missing on a Wedge component', () => {
expect(() => ReactTestRenderer.create(<Wedge fill="blue" />)).toErrorDev([
'Warning: Failed prop type: The prop `outerRadius` is marked as required in `Wedge`, ' +
'but its value is `undefined`.' +
'\n in Wedge (at **)',
'Warning: Failed prop type: The prop `startAngle` is marked as required in `Wedge`, ' +
'but its value is `undefined`.' +
'\n in Wedge (at **)',
'Warning: Failed prop type: The prop `endAngle` is marked as required in `Wedge`, ' +
'but its value is `undefined`.' +
'\n in Wedge (at **)',
]);
});
});