12
'use strict';
13
14
describe('ReactChildren', () => {
15
- var ReactChildren;
15
var React;
16
var ReactFragment;
17
18
beforeEach(() => {
20
- ReactChildren = require('ReactChildren');
19
+ jest.resetModuleRegistry();
20
React = require('React');
21
ReactFragment = require('ReactFragment');
22
});
23
24
it('should support identity for simple', () => {
25
+ var context = {};
26
var callback = jasmine.createSpy().and.callFake(function(kid, index) {
27
+ expect(this).toBe(context);
28
return kid;
29
});
30
34
// using structures that arrive from transforms.
35
36
var instance = <div>{simpleKid}</div>;
36
- ReactChildren.forEach(instance.props.children, callback);
37
+ React.Children.forEach(instance.props.children, callback, context);
38
expect(callback).toHaveBeenCalledWith(simpleKid, 0);
39
callback.calls.reset();
39
- var mappedChildren = ReactChildren.map(instance.props.children, callback);
40
+ var mappedChildren = React.Children.map(instance.props.children, callback, context);
41
expect(callback).toHaveBeenCalledWith(simpleKid, 0);
42
expect(mappedChildren[0]).toEqual(<span key=".$simple" />);
43
});
44
45
it('should treat single arrayless child as being in array', () => {
46
+ var context = {};
47
var callback = jasmine.createSpy().and.callFake(function(kid, index) {
48
+ expect(this).toBe(context);
49
return kid;
50
});
51
52
var simpleKid = <span />;
53
var instance = <div>{simpleKid}</div>;
51
- ReactChildren.forEach(instance.props.children, callback);
54
+ React.Children.forEach(instance.props.children, callback, context);
55
expect(callback).toHaveBeenCalledWith(simpleKid, 0);
56
callback.calls.reset();
54
- var mappedChildren = ReactChildren.map(instance.props.children, callback);
57
+ var mappedChildren = React.Children.map(instance.props.children, callback, context);
58
expect(callback).toHaveBeenCalledWith(simpleKid, 0);
59
expect(mappedChildren[0]).toEqual(<span key=".0" />);
60
});
61
62
it('should treat single child in array as expected', () => {
63
+ var context = {};
64
var callback = jasmine.createSpy().and.callFake(function(kid, index) {
65
+ expect(this).toBe(context);
66
return kid;
67
});
68
69
var simpleKid = <span key="simple" />;
70
var instance = <div>{[simpleKid]}</div>;
66
- ReactChildren.forEach(instance.props.children, callback);
71
+ React.Children.forEach(instance.props.children, callback, context);
72
expect(callback).toHaveBeenCalledWith(simpleKid, 0);
73
callback.calls.reset();
69
- var mappedChildren = ReactChildren.map(instance.props.children, callback);
74
+ var mappedChildren = React.Children.map(instance.props.children, callback, context);
75
expect(callback).toHaveBeenCalledWith(simpleKid, 0);
76
expect(mappedChildren[0]).toEqual(<span key=".$simple" />);
77
+ });
78
+
79
+ it('should be called for each child', () => {
80
+ var zero = <div key="keyZero" />;
81
+ var one = null;
82
+ var two = <div key="keyTwo" />;
83
+ var three = null;
84
+ var four = <div key="keyFour" />;
85
+ var context = {};
86
+
87
+ var callback =
88
+ jasmine.createSpy().and.callFake(function(kid) {
89
+ expect(this).toBe(context);
90
+ return kid;
91
+ });
92
+
93
+ var instance = (
94
+ <div>
95
+ {zero}
96
+ {one}
97
+ {two}
98
+ {three}
99
+ {four}
100
+ </div>
101
+ );
102
+
103
+ function assertCalls() {
104
+ expect(callback).toHaveBeenCalledWith(zero, 0);
105
+ expect(callback).toHaveBeenCalledWith(one, 1);
106
+ expect(callback).toHaveBeenCalledWith(two, 2);
107
+ expect(callback).toHaveBeenCalledWith(three, 3);
108
+ expect(callback).toHaveBeenCalledWith(four, 4);
109
+ callback.calls.reset();
110
+ }
111
+
112
+ React.Children.forEach(instance.props.children, callback, context);
113
+ assertCalls();
114
+
115
+ var mappedChildren = React.Children.map(instance.props.children, callback, context);
116
+ assertCalls();
117
+ expect(mappedChildren).toEqual([
118
+ <div key=".$keyZero" />,
119
+ <div key=".$keyTwo" />,
120
+ <div key=".$keyFour" />,
121
+ ]);
122
+ });
123
+
124
+ it('should traverse children of different kinds', () => {
125
+ var div = <div key="divNode" />;
126
+ var span = <span key="spanNode" />;
127
+ var a = <a key="aNode" />;
128
+
129
+ var context = {};
130
+ var callback =
131
+ jasmine.createSpy().and.callFake(function(kid) {
132
+ expect(this).toBe(context);
133
+ return kid;
134
+ });
135
+
136
+ var instance = (
137
+ <div>
138
+ {div}
139
+ {[ReactFragment.create({span})]}
140
+ {ReactFragment.create({a: a})}
141
+ {'string'}
142
+ {1234}
143
+ {true}
144
+ {false}
145
+ {null}
146
+ {undefined}
147
+ </div>
148
+ );
149
+
150
+ function assertCalls() {
151
+ expect(callback.calls.count()).toBe(9);
152
+ expect(callback).toHaveBeenCalledWith(div, 0);
153
+ expect(callback).toHaveBeenCalledWith(<span key="span/.$spanNode" />, 1);
154
+ expect(callback).toHaveBeenCalledWith(<a key="a/.$aNode" />, 2);
155
+ expect(callback).toHaveBeenCalledWith('string', 3);
156
+ expect(callback).toHaveBeenCalledWith(1234, 4);
157
+ expect(callback).toHaveBeenCalledWith(null, 5);
158
+ expect(callback).toHaveBeenCalledWith(null, 6);
159
+ expect(callback).toHaveBeenCalledWith(null, 7);
160
+ expect(callback).toHaveBeenCalledWith(null, 8);
161
+ callback.calls.reset();
162
+ }
163
+
164
+ React.Children.forEach(instance.props.children, callback, context);
165
+ assertCalls();
166
+
167
+ var mappedChildren = React.Children.map(instance.props.children, callback, context);
168
+ assertCalls();
169
+ expect(mappedChildren).toEqual([
170
+ <div key=".$divNode" />,
171
+ <span key=".1:0:$span/.$spanNode" />,
172
+ <a key=".2:$a/.$aNode" />,
173
+ 'string',
174
+ 1234,
175
+ ]);
176
+ });
177
+
178
+ it('should be called for each child in nested structure', () => {
179
+ var zero = <div key="keyZero" />;
180
+ var one = null;
181
+ var two = <div key="keyTwo" />;
182
+ var three = null;
183
+ var four = <div key="keyFour" />;
184
+ var five = <div key="keyFiveInner" />;
185
+ // five is placed into a JS object with a key that is joined to the
186
+ // component key attribute.
187
+ // Precedence is as follows:
188
+ // 1. If grouped in an Object, the object key combined with `key` prop
189
+ // 2. If grouped in an Array, the `key` prop, falling back to array index
190
+
191
+ var context = {};
192
+ var callback =
193
+ jasmine.createSpy().and.callFake(function(kid) {
194
+ return kid;
195
+ });
196
197
+ var instance = (
198
+ <div>{[
199
+ ReactFragment.create({
200
+ firstHalfKey: [zero, one, two],
201
+ secondHalfKey: [three, four],
202
+ keyFive: five,
203
+ }),
204
+ ]}</div>
205
+ );
206
+
207
+ function assertCalls() {
208
+ expect(callback.calls.count()).toBe(4);
209
+ expect(callback).toHaveBeenCalledWith(<div key="firstHalfKey/.$keyZero" />, 0);
210
+ expect(callback).toHaveBeenCalledWith(<div key="firstHalfKey/.$keyTwo" />, 1);
211
+ expect(callback).toHaveBeenCalledWith(<div key="secondHalfKey/.$keyFour" />, 2);
212
+ expect(callback).toHaveBeenCalledWith(<div key="keyFive/.$keyFiveInner" />, 3);
213
+ callback.calls.reset();
214
+ }
215
+
216
+ React.Children.forEach(instance.props.children, callback, context);
217
+ assertCalls();
218
+
219
+ var mappedChildren = React.Children.map(instance.props.children, callback, context);
220
+ assertCalls();
221
+ expect(mappedChildren).toEqual([
222
+ <div key=".0:$firstHalfKey/.$keyZero" />,
223
+ <div key=".0:$firstHalfKey/.$keyTwo" />,
224
+ <div key=".0:$secondHalfKey/.$keyFour" />,
225
+ <div key=".0:$keyFive/.$keyFiveInner" />,
226
+ ]);
227
+ });
228
+
229
+ it('should retain key across two mappings', () => {
230
+ var zeroForceKey = <div key="keyZero" />;
231
+ var oneForceKey = <div key="keyOne" />;
232
+ var context = {};
233
+ var callback =
234
+ jasmine.createSpy().and.callFake(function(kid) {
235
+ expect(this).toBe(context);
236
+ return kid;
237
+ });
238
+
239
+ var forcedKeys = (
240
+ <div>
241
+ {zeroForceKey}
242
+ {oneForceKey}
243
+ </div>
244
+ );
245
+
246
+ function assertCalls() {
247
+ expect(callback).toHaveBeenCalledWith(zeroForceKey, 0);
248
+ expect(callback).toHaveBeenCalledWith(oneForceKey, 1);
249
+ callback.calls.reset();
250
+ }
251
+
252
+ React.Children.forEach(forcedKeys.props.children, callback, context);
253
+ assertCalls();
254
+
255
+ var mappedChildren = React.Children.map(forcedKeys.props.children, callback, context);
256
+ assertCalls();
257
+ expect(mappedChildren).toEqual([
258
+ <div key=".$keyZero" />,
259
+ <div key=".$keyOne" />,
260
+ ]);
261
+ });
262
+
263
+ it('should be called for each child in an iterable without keys', () => {
264
+ spyOn(console, 'error');
265
+ var threeDivIterable = {
266
+ '@@iterator': function() {
267
+ var i = 0;
268
+ return {
269
+ next: function() {
270
+ if (i++ < 3) {
271
+ return {value: <div />, done: false};
272
+ } else {
273
+ return {value: undefined, done: true};
274
+ }
275
+ },
276
+ };
277
+ },
278
+ };
279
+
280
+ var context = {};
281
+ var callback =
282
+ jasmine.createSpy().and.callFake(function(kid) {
283
+ expect(this).toBe(context);
284
+ return kid;
285
+ });
286
+
287
+ var instance = (
288
+ <div>
289
+ {threeDivIterable}
290
+ </div>
291
+ );
292
+
293
+ function assertCalls() {
294
+ expect(callback.calls.count()).toBe(3);
295
+ expect(callback).toHaveBeenCalledWith(<div />, 0);
296
+ expect(callback).toHaveBeenCalledWith(<div />, 1);
297
+ expect(callback).toHaveBeenCalledWith(<div />, 2);
298
+ callback.calls.reset();
299
+ }
300
+
301
+ React.Children.forEach(instance.props.children, callback, context);
302
+ assertCalls();
303
+ expectDev(console.error.calls.count()).toBe(1);
304
+ expectDev(console.error.calls.argsFor(0)[0]).toContain(
305
+ 'Warning: Each child in an array or iterator should have a unique "key" prop.'
306
+ );
307
+ console.error.calls.reset();
308
+
309
+ var mappedChildren = React.Children.map(instance.props.children, callback, context);
310
+ assertCalls();
311
+ expectDev(console.error.calls.count()).toBe(0);
312
+ expect(mappedChildren).toEqual([
313
+ <div key=".0" />,
314
+ <div key=".1" />,
315
+ <div key=".2" />,
316
+ ]);
317
+ });
318
+
319
+ it('should be called for each child in an iterable with keys', () => {
320
+ var threeDivIterable = {
321
+ '@@iterator': function() {
322
+ var i = 0;
323
+ return {
324
+ next: function() {
325
+ if (i++ < 3) {
326
+ return {value: <div key={'#' + i} />, done: false};
327
+ } else {
328
+ return {value: undefined, done: true};
329
+ }
330
+ },
331
+ };
332
+ },
333
+ };
334
+
335
+ var context = {};
336
+ var callback =
337
+ jasmine.createSpy().and.callFake(function(kid) {
338
+ expect(this).toBe(context);
339
+ return kid;
340
+ });
341
+
342
+ var instance = (
343
+ <div>
344
+ {threeDivIterable}
345
+ </div>
346
+ );
347
+
348
+ function assertCalls() {
349
+ expect(callback.calls.count()).toBe(3);
350
+ expect(callback).toHaveBeenCalledWith(<div key="#1" />, 0);
351
+ expect(callback).toHaveBeenCalledWith(<div key="#2" />, 1);
352
+ expect(callback).toHaveBeenCalledWith(<div key="#3" />, 2);
353
+ callback.calls.reset();
354
+ }
355
+
356
+ React.Children.forEach(instance.props.children, callback, context);
357
+ assertCalls();
358
+
359
+ var mappedChildren = React.Children.map(instance.props.children, callback, context);
360
+ assertCalls();
361
+ expect(mappedChildren).toEqual([
362
+ <div key=".$#1" />,
363
+ <div key=".$#2" />,
364
+ <div key=".$#3" />,
365
+ ]);
366
+ });
367
+
368
+ it('should use keys from entry iterables', () => {
369
+ spyOn(console, 'error');
370
+
371
+ var threeDivEntryIterable = {
372
+ '@@iterator': function() {
373
+ var i = 0;
374
+ return {
375
+ next: function() {
376
+ if (i++ < 3) {
377
+ return {value: ['#' + i, <div />], done: false};
378
+ } else {
379
+ return {value: undefined, done: true};
380
+ }
381
+ },
382
+ };
383
+ },
384
+ };
385
+ threeDivEntryIterable.entries = threeDivEntryIterable['@@iterator'];
386
+
387
+ var context = {};
388
+ var callback =
389
+ jasmine.createSpy().and.callFake(function(kid) {
390
+ expect(this).toBe(context);
391
+ return kid;
392
+ });
393
+
394
+ var instance = (
395
+ <div>
396
+ {threeDivEntryIterable}
397
+ </div>
398
+ );
399
+
400
+ function assertCalls() {
401
+ expect(callback.calls.count()).toBe(3);
402
+ // TODO: why
403
+ expect(callback).toHaveBeenCalledWith(<div />, 0);
404
+ expect(callback).toHaveBeenCalledWith(<div />, 1);
405
+ expect(callback).toHaveBeenCalledWith(<div />, 2);
406
+
407
+ callback.calls.reset();
408
+ }
409
+
410
+ React.Children.forEach(instance.props.children, callback, context);
411
+ assertCalls();
412
+ expectDev(console.error.calls.count()).toBe(1);
413
+ expectDev(console.error.calls.argsFor(0)[0]).toContain(
414
+ 'Warning: Using Maps as children is not yet fully supported. It is an ' +
415
+ 'experimental feature that might be removed. Convert it to a sequence ' +
416
+ '/ iterable of keyed ReactElements instead.'
417
+ );
418
+ console.error.calls.reset();
419
+
420
+ var mappedChildren = React.Children.map(instance.props.children, callback, context);
421
+ assertCalls();
422
+ expect(mappedChildren).toEqual([
423
+ <div key=".$#1:0" />,
424
+ <div key=".$#2:0" />,
425
+ <div key=".$#3:0" />,
426
+ ]);
427
+ expectDev(console.error.calls.count()).toBe(0);
428
+ });
429
+
430
+ it('should not enumerate enumerable numbers (#4776)', () => {
431
+ /*eslint-disable no-extend-native */
432
+ Number.prototype['@@iterator'] = function() {
433
+ throw new Error('number iterator called');
434
+ };
435
+ /*eslint-enable no-extend-native */
436
+
437
+ try {
438
+ var instance = (
439
+ <div>
440
+ {5}
441
+ {12}
442
+ {13}
443
+ </div>
444
+ );
445
+
446
+ var context = {};
447
+ var callback = jasmine.createSpy().and.callFake(function(kid) {
448
+ expect(this).toBe(context);
449
+ return kid;
450
+ });
451
+
452
+ var assertCalls = function() {
453
+ expect(callback.calls.count()).toBe(3);
454
+ expect(callback).toHaveBeenCalledWith(5, 0);
455
+ expect(callback).toHaveBeenCalledWith(12, 1);
456
+ expect(callback).toHaveBeenCalledWith(13, 2);
457
+ callback.calls.reset();
458
+ };
459
+
460
+ React.Children.forEach(instance.props.children, callback, context);
461
+ assertCalls();
462
+
463
+ var mappedChildren = React.Children.map(instance.props.children, callback, context);
464
+ assertCalls();
465
+ expect(mappedChildren).toEqual([5, 12, 13]);
466
+ } finally {
467
+ delete Number.prototype['@@iterator'];
468
+ }
469
+ });
470
+
471
+ it('should allow extension of native prototypes', () => {
472
+ /*eslint-disable no-extend-native */
473
+ String.prototype.key = 'react';
474
+ Number.prototype.key = 'rocks';
475
+ /*eslint-enable no-extend-native */
476
+
477
+ var instance = (
478
+ <div>
479
+ {'a'}
480
+ {13}
481
+ </div>
482
+ );
483
+
484
+ var context = {};
485
+ var callback = jasmine.createSpy().and.callFake(function(kid) {
486
+ expect(this).toBe(context);
487
+ return kid;
488
+ });
489
+
490
+ function assertCalls() {
491
+ expect(callback.calls.count()).toBe(2, 0);
492
+ expect(callback).toHaveBeenCalledWith('a', 0);
493
+ expect(callback).toHaveBeenCalledWith(13, 1);
494
+ callback.calls.reset();
495
+ }
496
+
497
+ React.Children.forEach(instance.props.children, callback, context);
498
+ assertCalls();
499
+
500
+ var mappedChildren = React.Children.map(instance.props.children, callback, context);
501
+ assertCalls();
502
+ expect(mappedChildren).toEqual([
503
+ 'a',
504
+ 13,
505
+ ]);
506
+
507
+ delete String.prototype.key;
508
+ delete Number.prototype.key;
509
});
510
511
it('should pass key to returned component', () => {
516
var simpleKid = <span key="simple" />;
517
518
var instance = <div>{simpleKid}</div>;
83
- var mappedChildren = ReactChildren.map(instance.props.children, mapFn);
519
+ var mappedChildren = React.Children.map(instance.props.children, mapFn);
520
85
- expect(ReactChildren.count(mappedChildren)).toBe(1);
521
+ expect(React.Children.count(mappedChildren)).toBe(1);
522
expect(mappedChildren[0]).not.toBe(simpleKid);
523
expect(mappedChildren[0].props.children).toBe(simpleKid);
524
expect(mappedChildren[0].key).toBe('.$simple');
536
537
var simpleKid = <span key="simple" />;
538
var instance = <div>{simpleKid}</div>;
103
- ReactChildren.forEach(instance.props.children, callback, scopeTester);
539
+ React.Children.forEach(instance.props.children, callback, scopeTester);
540
expect(lastContext).toBe(scopeTester);
541
542
var mappedChildren =
107
- ReactChildren.map(instance.props.children, callback, scopeTester);
543
+ React.Children.map(instance.props.children, callback, scopeTester);
544
109
- expect(ReactChildren.count(mappedChildren)).toBe(1);
545
+ expect(React.Children.count(mappedChildren)).toBe(1);
546
expect(mappedChildren[0]).toBe(scopeTester);
547
});
548
574
</div>
575
);
576
141
- ReactChildren.forEach(instance.props.children, callback);
577
+ React.Children.forEach(instance.props.children, callback);
578
expect(callback).toHaveBeenCalledWith(zero, 0);
579
expect(callback).toHaveBeenCalledWith(one, 1);
580
expect(callback).toHaveBeenCalledWith(two, 2);
583
callback.calls.reset();
584
585
var mappedChildren =
150
- ReactChildren.map(instance.props.children, callback);
586
+ React.Children.map(instance.props.children, callback);
587
expect(callback.calls.count()).toBe(5);
152
- expect(ReactChildren.count(mappedChildren)).toBe(4);
588
+ expect(React.Children.count(mappedChildren)).toBe(4);
589
// Keys default to indices.
590
expect([
591
mappedChildren[0].key,
651
'keyFive/.$keyFiveInner',
652
]);
653
218
- ReactChildren.forEach(instance.props.children, callback);
654
+ React.Children.forEach(instance.props.children, callback);
655
expect(callback.calls.count()).toBe(4);
656
expect(callback).toHaveBeenCalledWith(frag[0], 0);
657
expect(callback).toHaveBeenCalledWith(frag[1], 1);
659
expect(callback).toHaveBeenCalledWith(frag[3], 3);
660
callback.calls.reset();
661
226
- var mappedChildren = ReactChildren.map(instance.props.children, callback);
662
+ var mappedChildren = React.Children.map(instance.props.children, callback);
663
expect(callback.calls.count()).toBe(4);
664
expect(callback).toHaveBeenCalledWith(frag[0], 0);
665
expect(callback).toHaveBeenCalledWith(frag[1], 1);
666
expect(callback).toHaveBeenCalledWith(frag[2], 2);
667
expect(callback).toHaveBeenCalledWith(frag[3], 3);
668
233
- expect(ReactChildren.count(mappedChildren)).toBe(4);
669
+ expect(React.Children.count(mappedChildren)).toBe(4);
670
// Keys default to indices.
671
expect([
672
mappedChildren[0].key,
708
709
var expectedForcedKeys = ['giraffe/.$keyZero', '.$keyOne'];
710
var mappedChildrenForcedKeys =
275
- ReactChildren.map(forcedKeys.props.children, mapFn);
711
+ React.Children.map(forcedKeys.props.children, mapFn);
712
var mappedForcedKeys = mappedChildrenForcedKeys.map((c) => c.key);
713
expect(mappedForcedKeys).toEqual(expectedForcedKeys);
714
717
'.$.$keyOne',
718
];
719
var remappedChildrenForcedKeys =
284
- ReactChildren.map(mappedChildrenForcedKeys, mapFn);
720
+ React.Children.map(mappedChildrenForcedKeys, mapFn);
721
expect(
722
remappedChildrenForcedKeys.map((c) => c.key)
723
).toEqual(expectedRemappedForcedKeys);
740
);
741
742
expect(function() {
307
- ReactChildren.map(instance.props.children, mapFn);
743
+ React.Children.map(instance.props.children, mapFn);
744
}).not.toThrow();
745
});
746
751
</div>
752
);
753
318
- var mapped = ReactChildren.map(
754
+ var mapped = React.Children.map(
755
instance.props.children,
756
element => element,
757
);
758
323
- var mappedWithClone = ReactChildren.map(
759
+ var mappedWithClone = React.Children.map(
760
instance.props.children,
761
element => React.cloneElement(element),
762
);
771
</div>
772
);
773
338
- var mapped = ReactChildren.map(
774
+ var mapped = React.Children.map(
775
instance.props.children,
776
element => element,
777
);
778
343
- var mappedWithClone = ReactChildren.map(
779
+ var mappedWithClone = React.Children.map(
780
instance.props.children,
781
element => React.cloneElement(element, {key: 'unique'}),
782
);
785
});
786
787
it('should return 0 for null children', () => {
352
- var numberOfChildren = ReactChildren.count(null);
788
+ var numberOfChildren = React.Children.count(null);
789
expect(numberOfChildren).toBe(0);
790
});
791
792
it('should return 0 for undefined children', () => {
357
- var numberOfChildren = ReactChildren.count(undefined);
793
+ var numberOfChildren = React.Children.count(undefined);
794
expect(numberOfChildren).toBe(0);
795
});
796
797
it('should return 1 for single child', () => {
798
var simpleKid = <span key="simple" />;
799
var instance = <div>{simpleKid}</div>;
364
- var numberOfChildren = ReactChildren.count(instance.props.children);
800
+ var numberOfChildren = React.Children.count(instance.props.children);
801
expect(numberOfChildren).toBe(1);
802
});
803
817
{four}
818
</div>
819
);
384
- var numberOfChildren = ReactChildren.count(instance.props.children);
820
+ var numberOfChildren = React.Children.count(instance.props.children);
821
expect(numberOfChildren).toBe(5);
822
});
823
844
null,
845
]}</div>
846
);
411
- var numberOfChildren = ReactChildren.count(instance.props.children);
847
+ var numberOfChildren = React.Children.count(instance.props.children);
848
expect(numberOfChildren).toBe(5);
849
});
850
851
it('should flatten children to an array', () => {
416
- expect(ReactChildren.toArray(undefined)).toEqual([]);
417
- expect(ReactChildren.toArray(null)).toEqual([]);
852
+ expect(React.Children.toArray(undefined)).toEqual([]);
853
+ expect(React.Children.toArray(null)).toEqual([]);
854
419
- expect(ReactChildren.toArray(<div />).length).toBe(1);
420
- expect(ReactChildren.toArray([<div />]).length).toBe(1);
855
+ expect(React.Children.toArray(<div />).length).toBe(1);
856
+ expect(React.Children.toArray([<div />]).length).toBe(1);
857
expect(
422
- ReactChildren.toArray(<div />)[0].key
858
+ React.Children.toArray(<div />)[0].key
859
).toBe(
424
- ReactChildren.toArray([<div />])[0].key
860
+ React.Children.toArray([<div />])[0].key
861
);
862
427
- var flattened = ReactChildren.toArray([
863
+ var flattened = React.Children.toArray([
864
[<div key="apple" />, <div key="banana" />, <div key="camel" />],
865
[<div key="banana" />, <div key="camel" />, <div key="deli" />],
866
]);
869
expect(flattened[3].key).toContain('banana');
870
expect(flattened[1].key).not.toBe(flattened[3].key);
871
436
- var reversed = ReactChildren.toArray([
872
+ var reversed = React.Children.toArray([
873
[<div key="camel" />, <div key="banana" />, <div key="apple" />],
874
[<div key="deli" />, <div key="camel" />, <div key="banana" />],
875
]);
881
expect(flattened[5].key).toBe(reversed[3].key);
882
883
// null/undefined/bool are all omitted
448
- expect(ReactChildren.toArray([1, 'two', null, undefined, true])).toEqual(
884
+ expect(React.Children.toArray([1, 'two', null, undefined, true])).toEqual(
885
[1, 'two']
886
);
887
});
888
889
+ it('should throw on object', () => {
890
+ expect(function() {
891
+ React.Children.forEach({a: 1, b: 2}, function() {}, null);
892
+ }).toThrowError(
893
+ 'Objects are not valid as a React child (found: object with keys ' +
894
+ '{a, b}). If you meant to render a collection of children, use an ' +
895
+ 'array instead or wrap the object using createFragment(object) from ' +
896
+ 'the React add-ons.'
897
+ );
898
+ });
899
+
900
+ it('should throw on regex', () => {
901
+ // Really, we care about dates (#4840) but those have nondeterministic
902
+ // serialization (timezones) so let's test a regex instead:
903
+ expect(function() {
904
+ React.Children.forEach(/abc/, function() {}, null);
905
+ }).toThrowError(
906
+ 'Objects are not valid as a React child (found: /abc/). If you meant ' +
907
+ 'to render a collection of children, use an array instead or wrap the ' +
908
+ 'object using createFragment(object) from the React add-ons.'
909
+ );
910
+ });
911
});