-
Notifications
You must be signed in to change notification settings - Fork 356
/
PropTypesProductionReact15-test.js
1144 lines (1023 loc) · 35.6 KB
/
PropTypesProductionReact15-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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* 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 PropTypes;
function resetWarningCache() {
jest.resetModules();
// Set production mode throughout this test.
process.env.NODE_ENV = 'production';
React = require('react');
// We are testing that when imported in the same way React 15 imports `prop-types`,
// it just suppresses warnings but doesn't actually throw when calling validators.
PropTypes = require('../factory')(React.isValidElement);
}
function expectNoop(declaration, value) {
if (!console.error.calls) {
spyOn(console, 'error');
} else {
console.error.calls.reset();
}
const props = {testProp: value};
const propName = 'testProp' + Math.random().toString();
const componentName = 'testComponent' + Math.random().toString();
// Try calling it manually
for (let i = 0; i < 3; i++) {
declaration(props, propName, componentName, 'prop');
}
// Try calling it via checkPropTypes
const propTypes = {
testProp: declaration,
};
PropTypes.checkPropTypes(propTypes, props, 'prop', 'testComponent');
// They should all be no-ops
expect(console.error.calls.count()).toBe(0);
console.error.calls.reset();
}
describe('PropTypesProductionReact15', () => {
beforeEach(() => {
resetWarningCache();
});
describe('Primitive Types', () => {
it('should warn for invalid strings', () => {
expectNoop(
PropTypes.string,
[],
'Invalid prop `testProp` of type `array` supplied to ' +
'`testComponent`, expected `string`.',
);
expectNoop(
PropTypes.string,
false,
'Invalid prop `testProp` of type `boolean` supplied to ' +
'`testComponent`, expected `string`.',
);
expectNoop(
PropTypes.string,
0,
'Invalid prop `testProp` of type `number` supplied to ' +
'`testComponent`, expected `string`.',
);
expectNoop(
PropTypes.string,
{},
'Invalid prop `testProp` of type `object` supplied to ' +
'`testComponent`, expected `string`.',
);
expectNoop(
PropTypes.string,
Symbol(),
'Invalid prop `testProp` of type `symbol` supplied to ' +
'`testComponent`, expected `string`.',
);
});
it('should fail date and regexp correctly', () => {
expectNoop(
PropTypes.string,
new Date(),
'Invalid prop `testProp` of type `date` supplied to ' +
'`testComponent`, expected `string`.',
);
expectNoop(
PropTypes.string,
/please/,
'Invalid prop `testProp` of type `regexp` supplied to ' +
'`testComponent`, expected `string`.',
);
});
it('should not warn for valid values', () => {
expectNoop(PropTypes.array, []);
expectNoop(PropTypes.bool, false);
expectNoop(PropTypes.func, function() {});
expectNoop(PropTypes.number, 0);
expectNoop(PropTypes.string, '');
expectNoop(PropTypes.object, {});
expectNoop(PropTypes.object, new Date());
expectNoop(PropTypes.object, /please/);
expectNoop(PropTypes.symbol, Symbol());
});
it('should be implicitly optional and not warn without values', () => {
expectNoop(PropTypes.string, null);
expectNoop(PropTypes.string, undefined);
});
it('should warn for missing required values', () => {
expectNoop(PropTypes.string.isRequired);
expectNoop(PropTypes.array.isRequired);
expectNoop(PropTypes.symbol.isRequired);
expectNoop(PropTypes.number.isRequired);
expectNoop(PropTypes.bool.isRequired);
expectNoop(PropTypes.func.isRequired);
expectNoop(PropTypes.shape({}).isRequired);
});
it('should warn if called manually in development', () => {
spyOn(console, 'error');
expectNoop(PropTypes.array, /please/);
expectNoop(PropTypes.array, []);
expectNoop(PropTypes.array.isRequired, /please/);
expectNoop(PropTypes.array.isRequired, []);
expectNoop(PropTypes.array.isRequired, null);
expectNoop(PropTypes.array.isRequired, undefined);
expectNoop(PropTypes.bool, []);
expectNoop(PropTypes.bool, true);
expectNoop(PropTypes.bool.isRequired, []);
expectNoop(PropTypes.bool.isRequired, true);
expectNoop(PropTypes.bool.isRequired, null);
expectNoop(PropTypes.bool.isRequired, undefined);
expectNoop(PropTypes.func, false);
expectNoop(PropTypes.func, function() {});
expectNoop(PropTypes.func.isRequired, false);
expectNoop(PropTypes.func.isRequired, function() {});
expectNoop(PropTypes.func.isRequired, null);
expectNoop(PropTypes.func.isRequired, undefined);
expectNoop(PropTypes.number, function() {});
expectNoop(PropTypes.number, 42);
expectNoop(PropTypes.number.isRequired, function() {});
expectNoop(PropTypes.number.isRequired, 42);
expectNoop(PropTypes.number.isRequired, null);
expectNoop(PropTypes.number.isRequired, undefined);
expectNoop(PropTypes.string, 0);
expectNoop(PropTypes.string, 'foo');
expectNoop(PropTypes.string.isRequired, 0);
expectNoop(PropTypes.string.isRequired, 'foo');
expectNoop(PropTypes.string.isRequired, null);
expectNoop(PropTypes.string.isRequired, undefined);
expectNoop(PropTypes.symbol, 0);
expectNoop(PropTypes.symbol, Symbol('Foo'));
expectNoop(PropTypes.symbol.isRequired, 0);
expectNoop(PropTypes.symbol.isRequired, Symbol('Foo'));
expectNoop(PropTypes.symbol.isRequired, null);
expectNoop(PropTypes.symbol.isRequired, undefined);
expectNoop(PropTypes.object, '');
expectNoop(PropTypes.object, {foo: 'bar'});
expectNoop(PropTypes.object.isRequired, '');
expectNoop(PropTypes.object.isRequired, {foo: 'bar'});
expectNoop(PropTypes.object.isRequired, null);
expectNoop(PropTypes.object.isRequired, undefined);
});
});
describe('Any type', () => {
it('should should accept any value', () => {
expectNoop(PropTypes.any, 0);
expectNoop(PropTypes.any, 'str');
expectNoop(PropTypes.any, []);
expectNoop(PropTypes.any, Symbol());
});
it('should be implicitly optional and not warn without values', () => {
expectNoop(PropTypes.any, null);
expectNoop(PropTypes.any, undefined);
});
it('should warn for missing required values', () => {
expectNoop(PropTypes.any.isRequired);
});
it('should warn if called manually in development', () => {
spyOn(console, 'error');
expectNoop(PropTypes.any, null);
expectNoop(PropTypes.any.isRequired, null);
expectNoop(PropTypes.any.isRequired, undefined);
});
});
describe('ArrayOf Type', () => {
it('should fail for invalid argument', () => {
expectNoop(
PropTypes.arrayOf({foo: PropTypes.string}),
{foo: 'bar'},
'Property `testProp` of component `testComponent` has invalid PropType notation inside arrayOf.',
);
});
it('should support the arrayOf propTypes', () => {
expectNoop(PropTypes.arrayOf(PropTypes.number), [1, 2, 3]);
expectNoop(PropTypes.arrayOf(PropTypes.string), ['a', 'b', 'c']);
expectNoop(PropTypes.arrayOf(PropTypes.oneOf(['a', 'b'])), ['a', 'b']);
expectNoop(PropTypes.arrayOf(PropTypes.symbol), [Symbol(), Symbol()]);
});
it('should support arrayOf with complex types', () => {
expectNoop(
PropTypes.arrayOf(PropTypes.shape({a: PropTypes.number.isRequired})),
[{a: 1}, {a: 2}],
);
function Thing() {}
expectNoop(PropTypes.arrayOf(PropTypes.instanceOf(Thing)), [
new Thing(),
new Thing(),
]);
});
it('should warn with invalid items in the array', () => {
expectNoop(
PropTypes.arrayOf(PropTypes.number),
[1, 2, 'b'],
'Invalid prop `testProp[2]` of type `string` supplied to ' +
'`testComponent`, expected `number`.',
);
});
it('should warn with invalid complex types', () => {
function Thing() {}
const name = Thing.name || '<<anonymous>>';
expectNoop(
PropTypes.arrayOf(PropTypes.instanceOf(Thing)),
[new Thing(), 'xyz'],
'Invalid prop `testProp[1]` of type `String` supplied to ' +
'`testComponent`, expected instance of `' +
name +
'`.',
);
});
it('should warn when passed something other than an array', () => {
expectNoop(
PropTypes.arrayOf(PropTypes.number),
{'0': 'maybe-array', length: 1},
'Invalid prop `testProp` of type `object` supplied to ' +
'`testComponent`, expected an array.',
);
expectNoop(
PropTypes.arrayOf(PropTypes.number),
123,
'Invalid prop `testProp` of type `number` supplied to ' +
'`testComponent`, expected an array.',
);
expectNoop(
PropTypes.arrayOf(PropTypes.number),
'string',
'Invalid prop `testProp` of type `string` supplied to ' +
'`testComponent`, expected an array.',
);
});
it('should not warn when passing an empty array', () => {
expectNoop(PropTypes.arrayOf(PropTypes.number), []);
});
it('should be implicitly optional and not warn without values', () => {
expectNoop(PropTypes.arrayOf(PropTypes.number), null);
expectNoop(PropTypes.arrayOf(PropTypes.number), undefined);
});
it('should warn for missing required values', () => {
expectNoop(
PropTypes.arrayOf(PropTypes.number).isRequired,
);
});
it('should warn if called manually in development', () => {
spyOn(console, 'error');
expectNoop(PropTypes.arrayOf({foo: PropTypes.string}), {
foo: 'bar',
});
expectNoop(PropTypes.arrayOf(PropTypes.number), [
1,
2,
'b',
]);
expectNoop(PropTypes.arrayOf(PropTypes.number), {
'0': 'maybe-array',
length: 1,
});
expectNoop(
PropTypes.arrayOf(PropTypes.number).isRequired,
null,
);
expectNoop(
PropTypes.arrayOf(PropTypes.number).isRequired,
undefined,
);
});
});
describe('Component Type', () => {
it('should support components', () => {
expectNoop(PropTypes.element, <div />);
});
it('should not support multiple components or scalar values', () => {
expectNoop(
PropTypes.element,
[<div />, <div />],
'Invalid prop `testProp` of type `array` supplied to `testComponent`, ' +
'expected a single ReactElement.',
);
expectNoop(
PropTypes.element,
123,
'Invalid prop `testProp` of type `number` supplied to `testComponent`, ' +
'expected a single ReactElement.',
);
expectNoop(
PropTypes.element,
'foo',
'Invalid prop `testProp` of type `string` supplied to `testComponent`, ' +
'expected a single ReactElement.',
);
expectNoop(
PropTypes.element,
false,
'Invalid prop `testProp` of type `boolean` supplied to `testComponent`, ' +
'expected a single ReactElement.',
);
});
it('should be implicitly optional and not warn without values', () => {
expectNoop(PropTypes.element, null);
expectNoop(PropTypes.element, undefined);
});
it('should warn for missing required values', () => {
expectNoop(PropTypes.element.isRequired);
});
it('should warn if called manually in development', () => {
spyOn(console, 'error');
expectNoop(PropTypes.element, [<div />, <div />]);
expectNoop(PropTypes.element, <div />);
expectNoop(PropTypes.element, 123);
expectNoop(PropTypes.element, 'foo');
expectNoop(PropTypes.element, false);
expectNoop(PropTypes.element.isRequired, null);
expectNoop(PropTypes.element.isRequired, undefined);
});
});
describe('Instance Types', () => {
it('should warn for invalid instances', () => {
function Person() {}
function Cat() {}
const personName = Person.name || '<<anonymous>>';
const dateName = Date.name || '<<anonymous>>';
const regExpName = RegExp.name || '<<anonymous>>';
expectNoop(
PropTypes.instanceOf(Person),
false,
'Invalid prop `testProp` of type `Boolean` supplied to ' +
'`testComponent`, expected instance of `' +
personName +
'`.',
);
expectNoop(
PropTypes.instanceOf(Person),
{},
'Invalid prop `testProp` of type `Object` supplied to ' +
'`testComponent`, expected instance of `' +
personName +
'`.',
);
expectNoop(
PropTypes.instanceOf(Person),
'',
'Invalid prop `testProp` of type `String` supplied to ' +
'`testComponent`, expected instance of `' +
personName +
'`.',
);
expectNoop(
PropTypes.instanceOf(Date),
{},
'Invalid prop `testProp` of type `Object` supplied to ' +
'`testComponent`, expected instance of `' +
dateName +
'`.',
);
expectNoop(
PropTypes.instanceOf(RegExp),
{},
'Invalid prop `testProp` of type `Object` supplied to ' +
'`testComponent`, expected instance of `' +
regExpName +
'`.',
);
expectNoop(
PropTypes.instanceOf(Person),
new Cat(),
'Invalid prop `testProp` of type `Cat` supplied to ' +
'`testComponent`, expected instance of `' +
personName +
'`.',
);
expectNoop(
PropTypes.instanceOf(Person),
Object.create(null),
'Invalid prop `testProp` of type `<<anonymous>>` supplied to ' +
'`testComponent`, expected instance of `' +
personName +
'`.',
);
});
it('should not warn for valid values', () => {
function Person() {}
function Engineer() {}
Engineer.prototype = new Person();
expectNoop(PropTypes.instanceOf(Person), new Person());
expectNoop(PropTypes.instanceOf(Person), new Engineer());
expectNoop(PropTypes.instanceOf(Date), new Date());
expectNoop(PropTypes.instanceOf(RegExp), /please/);
});
it('should be implicitly optional and not warn without values', () => {
expectNoop(PropTypes.instanceOf(String), null);
expectNoop(PropTypes.instanceOf(String), undefined);
});
it('should warn for missing required values', () => {
expectNoop(PropTypes.instanceOf(String).isRequired);
});
it('should warn if called manually in development', () => {
spyOn(console, 'error');
expectNoop(PropTypes.instanceOf(Date), {});
expectNoop(PropTypes.instanceOf(Date), new Date());
expectNoop(PropTypes.instanceOf(Date).isRequired, {});
expectNoop(
PropTypes.instanceOf(Date).isRequired,
new Date(),
);
});
});
describe('React Component Types', () => {
it('should warn for invalid values', () => {
const failMessage = 'Invalid prop `testProp` supplied to ' +
'`testComponent`, expected a ReactNode.';
expectNoop(PropTypes.node, true, failMessage);
expectNoop(PropTypes.node, function() {}, failMessage);
expectNoop(PropTypes.node, {key: function() {}}, failMessage);
expectNoop(PropTypes.node, {key: <div />}, failMessage);
});
it('should not warn for valid values', () => {
function MyComponent() {}
MyComponent.prototype.render = function() {
return <div />;
};
expectNoop(PropTypes.node, <div />);
expectNoop(PropTypes.node, false);
expectNoop(PropTypes.node, <MyComponent />);
expectNoop(PropTypes.node, 'Some string');
expectNoop(PropTypes.node, []);
expectNoop(PropTypes.node, [
123,
'Some string',
<div />,
['Another string', [456], <span />, <MyComponent />],
<MyComponent />,
null,
undefined,
]);
});
it('should not warn for iterables', () => {
function MyComponent() {}
MyComponent.prototype.render = function() {
return <div />;
};
const iterable = {
'@@iterator': function() {
const i = 0;
return {
next: function() {
const done = ++i > 2;
return {value: done ? undefined : <MyComponent />, done: done};
},
};
},
};
expectNoop(PropTypes.node, iterable);
});
it('should not warn for entry iterables', () => {
function MyComponent() {}
MyComponent.prototype.render = function() {
return <div />;
};
const iterable = {
'@@iterator': function() {
const i = 0;
return {
next: function() {
const done = ++i > 2;
return {
value: done ? undefined : ['#' + i, <MyComponent />],
done: done,
};
},
};
},
};
iterable.entries = iterable['@@iterator'];
expectNoop(PropTypes.node, iterable);
});
it('should not warn for null/undefined if not required', () => {
expectNoop(PropTypes.node, null);
expectNoop(PropTypes.node, undefined);
});
it('should warn for missing required values', () => {
expectNoop(PropTypes.node.isRequired);
});
it('should accept empty array for required props', () => {
expectNoop(PropTypes.node.isRequired, []);
});
it('should warn if called manually in development', () => {
spyOn(console, 'error');
expectNoop(PropTypes.node, 'node');
expectNoop(PropTypes.node, {});
expectNoop(PropTypes.node.isRequired, 'node');
expectNoop(PropTypes.node.isRequired, undefined);
expectNoop(PropTypes.node.isRequired, undefined);
});
});
describe('ObjectOf Type', () => {
it('should fail for invalid argument', () => {
expectNoop(
PropTypes.objectOf({foo: PropTypes.string}),
{foo: 'bar'},
'Property `testProp` of component `testComponent` has invalid PropType notation inside objectOf.',
);
});
it('should support the objectOf propTypes', () => {
expectNoop(PropTypes.objectOf(PropTypes.number), {a: 1, b: 2, c: 3});
expectNoop(PropTypes.objectOf(PropTypes.string), {
a: 'a',
b: 'b',
c: 'c',
});
expectNoop(PropTypes.objectOf(PropTypes.oneOf(['a', 'b'])), {
a: 'a',
b: 'b',
});
expectNoop(PropTypes.objectOf(PropTypes.symbol), {
a: Symbol(),
b: Symbol(),
c: Symbol(),
});
});
it('should support objectOf with complex types', () => {
expectNoop(
PropTypes.objectOf(PropTypes.shape({a: PropTypes.number.isRequired})),
{a: {a: 1}, b: {a: 2}},
);
function Thing() {}
expectNoop(PropTypes.objectOf(PropTypes.instanceOf(Thing)), {
a: new Thing(),
b: new Thing(),
});
});
it('should warn with invalid items in the object', () => {
expectNoop(
PropTypes.objectOf(PropTypes.number),
{a: 1, b: 2, c: 'b'},
'Invalid prop `testProp.c` of type `string` supplied to `testComponent`, ' +
'expected `number`.',
);
expectNoop(
PropTypes.objectOf(PropTypes.number.isRequired),
{a: 1, b: 2, c: undefined},
'Warning: Failed prop type: The prop `testProp.c` is marked as required in `testComponent`, ' +
'but its value is `undefined`.'
);
});
it('should warn with invalid complex types', () => {
function Thing() {}
const name = Thing.name || '<<anonymous>>';
expectNoop(
PropTypes.objectOf(PropTypes.instanceOf(Thing)),
{a: new Thing(), b: 'xyz'},
'Invalid prop `testProp.b` of type `String` supplied to ' +
'`testComponent`, expected instance of `' +
name +
'`.',
);
expectNoop(
PropTypes.objectOf(PropTypes.instanceOf(Thing).isRequired),
{a: new Thing(), b: undefined},
'Warning: Failed prop type: The prop `testProp.b` is marked as required in `testComponent`, ' +
'but its value is `undefined`.'
);
});
it('should warn when passed something other than an object', () => {
expectNoop(
PropTypes.objectOf(PropTypes.number),
[1, 2],
'Invalid prop `testProp` of type `array` supplied to ' +
'`testComponent`, expected an object.',
);
expectNoop(
PropTypes.objectOf(PropTypes.number),
123,
'Invalid prop `testProp` of type `number` supplied to ' +
'`testComponent`, expected an object.',
);
expectNoop(
PropTypes.objectOf(PropTypes.number),
'string',
'Invalid prop `testProp` of type `string` supplied to ' +
'`testComponent`, expected an object.',
);
expectNoop(
PropTypes.objectOf(PropTypes.symbol),
Symbol(),
'Invalid prop `testProp` of type `symbol` supplied to ' +
'`testComponent`, expected an object.',
);
});
it('should not warn when passing an object with no prototype', () => {
expectNoop(PropTypes.objectOf(PropTypes.number), Object.create(null));
});
it('should not warn when passing an empty object', () => {
expectNoop(PropTypes.objectOf(PropTypes.number), {});
});
it('should not warn when passing an object with a hasOwnProperty property', () => {
expectNoop(PropTypes.objectOf(PropTypes.number), {
hasOwnProperty: 3,
});
});
it('should be implicitly optional and not warn without values', () => {
expectNoop(PropTypes.objectOf(PropTypes.number), null);
expectNoop(PropTypes.objectOf(PropTypes.number), undefined);
});
it('should warn for missing required values', () => {
expectNoop(
PropTypes.objectOf(PropTypes.number).isRequired,
);
});
it('should warn if called manually in development', () => {
spyOn(console, 'error');
expectNoop(PropTypes.objectOf({foo: PropTypes.string}), {
foo: 'bar',
});
expectNoop(PropTypes.objectOf(PropTypes.number), {
a: 1,
b: 2,
c: 'b',
});
expectNoop(PropTypes.objectOf(PropTypes.number), [1, 2]);
expectNoop(PropTypes.objectOf(PropTypes.number), null);
expectNoop(
PropTypes.objectOf(PropTypes.number),
undefined,
);
});
});
describe('OneOf Types', () => {
it('should ignore invalid argument', () => {
spyOn(console, 'error');
PropTypes.oneOf('red', 'blue');
expect(console.error).not.toHaveBeenCalled();
expectNoop(PropTypes.oneOf('red', 'blue'), 'red');
});
it('should ignore invalid values', () => {
expectNoop(
PropTypes.oneOf(['red', 'blue']),
true,
'Invalid prop `testProp` of value `true` supplied to ' +
'`testComponent`, expected one of ["red","blue"].',
);
expectNoop(
PropTypes.oneOf(['red', 'blue']),
[],
'Invalid prop `testProp` of value `` supplied to `testComponent`, ' +
'expected one of ["red","blue"].',
);
expectNoop(
PropTypes.oneOf(['red', 'blue']),
'',
'Invalid prop `testProp` of value `` supplied to `testComponent`, ' +
'expected one of ["red","blue"].',
);
expectNoop(
PropTypes.oneOf([0, 'false']),
false,
'Invalid prop `testProp` of value `false` supplied to ' +
'`testComponent`, expected one of [0,"false"].',
);
expectNoop(
PropTypes.oneOf([Symbol('red'), Symbol('blue')]),
Symbol('green'),
'Invalid prop `testProp` of value `Symbol(green)` supplied to ' +
'`testComponent`, expected one of ["Symbol(red)","Symbol(blue)"].',
);
expectNoop(
PropTypes.oneOf([0, 'false']).isRequired,
undefined,
'Warning: Failed prop type: The prop `testProp` is marked as required in `testComponent`, ' +
'but its value is `undefined`.'
);
expectNoop(
PropTypes.oneOf([0, 'false']).isRequired,
null,
'Warning: Failed prop type: The prop `testProp` is marked as required in `testComponent`, ' +
'but its value is `null`.'
);
});
it('does not fail when the valid types contain null or undefined', () => {
expectNoop(
PropTypes.oneOf([0, 'false', null, undefined]),
false,
'Warning: Failed prop type: Invalid prop `testProp` of value `false` supplied to ' +
'`testComponent`, expected one of [0,"false",null,undefined].',
);
});
it('should not warn for valid values', () => {
expectNoop(PropTypes.oneOf(['red', 'blue']), 'red');
expectNoop(PropTypes.oneOf(['red', 'blue']), 'blue');
expectNoop(PropTypes.oneOf(['red', 'blue', NaN]), NaN);
});
it('should be implicitly optional and not warn without values', () => {
expectNoop(PropTypes.oneOf(['red', 'blue']), null);
expectNoop(PropTypes.oneOf(['red', 'blue']), undefined);
});
it('should warn for missing required values', () => {
expectNoop(PropTypes.oneOf(['red', 'blue']).isRequired);
});
it('should warn if called manually in development', () => {
spyOn(console, 'error');
expectNoop(PropTypes.oneOf(['red', 'blue']), true);
expectNoop(PropTypes.oneOf(['red', 'blue']), null);
expectNoop(PropTypes.oneOf(['red', 'blue']), undefined);
});
});
describe('ElementWithType Types', () => {
it('should fail for mismatched component.type', () => {
expectNoop(
PropTypes.elementWithType('div'),
<span />,
"Warning: Failed prop type: Invalid prop `testProp` of type [span] supplied to `testComponent`, expected one of type `div`."
);
});
it('should pass for matched component.type', () => {
expectNoop(PropTypes.elementWithType('div'), <div />);
});
it('should pass if no component given', () => {
expectNoop(PropTypes.elementWithType('div'));
});
describe('factory error', () => {
it('should fail if using any value thats not a function nor a string for expectedTypes', () => {
spyOn(console, 'error');
PropTypes.elementWithType(1);
expect(console.error).not.toHaveBeenCalled();
expectNoop(PropTypes.elementWithType(1), <span />);
});
});
describe('with .isRequired', () => {
it('should fail if not passing in anything', () => {
expectNoop(
PropTypes.elementWithType('div').isRequired,
null,
"Warning: Failed prop type: The prop `testProp` is marked as required in `testComponent`, but its value is `null`."
);
});
});
describe('custom component', () => {
const MyComponent = () => <div></div>;
var myComponentPropType;
beforeEach(() => {
myComponentPropType = PropTypes.elementWithType(MyComponent)
});
it('should fail for mismatched component.type', () => {
expectNoop(
myComponentPropType,
<span />,
"Warning: Failed prop type: Invalid prop `testProp` of type [span] supplied to `testComponent`, expected one of type `MyComponent`."
);
});
it('should pass for matched component.type', () => {
expectNoop(myComponentPropType, <MyComponent />);
});
});
describe('given component mismatches with expected', () => {
it('should warn if given invalid element', () => {
[
'<div></div>',
{ type: 'div' },
1,
['div'],
'div'
].forEach(invalidElement =>
expectNoop(
PropTypes.elementWithType('div'),
invalidElement,
"Warning: Failed prop type: Invalid prop `testProp` of component `testComponent` has been given invalid component."
)
);
});
});
describe('list of expected types', () => {
it('should warn if expected type does not include given component.type', () => {
expectNoop(
PropTypes.elementWithType('div'),
<span />,
"Warning: Failed prop type: Invalid prop `testProp` of type [span] supplied to `testComponent`, expected one of type `div`."
);
});
it('should pass if expected type include given component.type', () => {
expectNoop(PropTypes.elementWithType('div'), <div />)
});
});
});
describe('Union Types', () => {
it('should ignore invalid argument', () => {
spyOn(console, 'error');
PropTypes.oneOfType(PropTypes.string, PropTypes.number);
expect(console.error).not.toHaveBeenCalled();
expectNoop(PropTypes.oneOf(PropTypes.string, PropTypes.number), []);
});
it('should warn if none of the types are valid', () => {
expectNoop(
PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
[],
'Invalid prop `testProp` supplied to `testComponent`.',
);
const checker = PropTypes.oneOfType([
PropTypes.shape({a: PropTypes.number.isRequired}),
PropTypes.shape({b: PropTypes.number.isRequired}),
]);
expectNoop(
checker,
{c: 1},
'Invalid prop `testProp` supplied to `testComponent`.',
);
});
it('should not warn if one of the types are valid', () => {
let checker = PropTypes.oneOfType([PropTypes.string, PropTypes.number]);
expectNoop(checker, null);
expectNoop(checker, 'foo');
expectNoop(checker, 123);
checker = PropTypes.oneOfType([
PropTypes.shape({a: PropTypes.number.isRequired}),
PropTypes.shape({b: PropTypes.number.isRequired}),
]);
expectNoop(checker, {a: 1});
expectNoop(checker, {b: 1});
});
it('should be implicitly optional and not warn without values', () => {
expectNoop(
PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
null,
);
expectNoop(
PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
undefined,
);
});
it('should warn for missing required values', () => {
expectNoop(
PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
);
});
it('should warn if called manually in development', () => {
spyOn(console, 'error');
expectNoop(
PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
[],
);
expectNoop(
PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
null,
);
expectNoop(
PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
undefined,
);
});
it('should not warn if setup without checker function', () => {
expectNoop(
PropTypes.oneOfType([null]),
null,
);
expectNoop(
PropTypes.oneOfType([undefined]),
undefined,
);
});
});
describe('Shape Types', () => {
it('should warn for non objects', () => {
expectNoop(
PropTypes.shape({}),
'some string',
'Invalid prop `testProp` of type `string` supplied to ' +
'`testComponent`, expected `object`.',
);
expectNoop(
PropTypes.shape({}),
['array'],
'Invalid prop `testProp` of type `array` supplied to ' +
'`testComponent`, expected `object`.',
);
});
it('should not warn for empty values', () => {
expectNoop(PropTypes.shape({}), undefined);
expectNoop(PropTypes.shape({}), null);
expectNoop(PropTypes.shape({}), {});
});