-
Notifications
You must be signed in to change notification settings - Fork 356
/
PropTypesDevelopmentStandalone-test.js
1457 lines (1300 loc) · 47.8 KB
/
PropTypesDevelopmentStandalone-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();
React = require('react');
PropTypes = require('../index');
}
resetWarningCache();
function getPropTypeWarningMessage(propTypes, object, componentName) {
if (!console.error.calls) {
spyOn(console, 'error');
} else {
console.error.calls.reset();
}
resetWarningCache();
PropTypes.checkPropTypes(propTypes, object, 'prop', componentName);
const callCount = console.error.calls.count();
if (callCount > 1) {
throw new Error('Too many warnings.');
}
const message = console.error.calls.argsFor(0)[0] || null;
console.error.calls.reset();
return message;
}
function typeCheckFail(declaration, value, expectedMessage) {
const propTypes = {
testProp: declaration,
};
const props = {
testProp: value,
};
const message = getPropTypeWarningMessage(propTypes, props, 'testComponent');
expect(message).toContain(expectedMessage);
}
function typeCheckFailRequiredValues(declaration) {
const specifiedButIsNullMsg = 'The prop `testProp` is marked as required in ' +
'`testComponent`, but its value is `null`.';
const unspecifiedMsg = 'The prop `testProp` is marked as required in ' +
'`testComponent`, but its value is \`undefined\`.';
const propTypes = {testProp: declaration};
// Required prop is null
const message1 = getPropTypeWarningMessage(
propTypes,
{testProp: null},
'testComponent',
);
expect(message1).toContain(specifiedButIsNullMsg);
// Required prop is undefined
const message2 = getPropTypeWarningMessage(
propTypes,
{testProp: undefined},
'testComponent',
);
expect(message2).toContain(unspecifiedMsg);
// Required prop is not a member of props object
const message3 = getPropTypeWarningMessage(propTypes, {}, 'testComponent');
expect(message3).toContain(unspecifiedMsg);
}
function typeCheckPass(declaration, value) {
const propTypes = {
testProp: declaration,
};
const props = {
testProp: value,
};
const message = getPropTypeWarningMessage(propTypes, props, 'testComponent');
expect(message).toBe(null);
}
function expectThrowsInDevelopment(declaration, value) {
resetWarningCache();
const props = {testProp: value};
expect(() => {
declaration(props, 'testProp', 'testComponent', 'prop');
}).toThrowError(
'Calling PropTypes validators directly is not supported by the `prop-types` package. ' +
'Use `PropTypes.checkPropTypes()` to call them. ' +
'Read more at http://fb.me/use-check-prop-types'
);
}
function expectInvalidValidatorWarning(declaration, type) {
PropTypes.checkPropTypes({ foo: declaration }, { foo: {} }, 'prop', 'testComponent', null);
expect(console.error.calls.argsFor(0)[0]).toEqual(
'Warning: Failed prop type: testComponent: prop type `foo.bar` is invalid; '
+ 'it must be a function, usually from the `prop-types` package, but received `' + type + '`.'
);
console.error.calls.reset();
}
describe('PropTypesDevelopmentStandalone', () => {
beforeEach(() => {
resetWarningCache();
});
describe('checkPropTypes', () => {
it('should warn for invalid validators', () => {
spyOn(console, 'error')
const propTypes = { foo: undefined };
const props = { foo: 'foo' };
PropTypes.checkPropTypes(
propTypes,
props,
'prop',
'testComponent',
null,
);
expect(console.error.calls.argsFor(0)[0]).toEqual(
'Warning: Failed prop type: testComponent: prop type `foo` is invalid; ' +
'it must be a function, usually from the `prop-types` package, but received `undefined`.' +
'This often happens because of typos such as `PropTypes.function` instead of `PropTypes.func`.'
);
});
it('should only warn once for identical validator failures', () => {
spyOn(console, 'error');
const propTypes = { foo: undefined };
const props = { foo: 'foo' };
PropTypes.checkPropTypes(
propTypes,
props,
'prop',
'testComponent',
null,
);
PropTypes.checkPropTypes(
propTypes,
props,
'prop',
'testComponent',
null,
);
expect(console.error.calls.count()).toEqual(1);
});
describe('checkPropTypes.resetWarningCache', () => {
it('should reset warning cache', () => {
spyOn(console, 'error')
const propTypes = { foo: undefined };
const props = { foo: 'foo' };
PropTypes.checkPropTypes(
propTypes,
props,
'prop',
'testComponent',
null,
);
PropTypes.checkPropTypes.resetWarningCache();
PropTypes.checkPropTypes(
propTypes,
props,
'prop',
'testComponent',
null,
);
expect(console.error.calls.count()).toEqual(2);
});
});
it('does not return a value from a validator', () => {
spyOn(console, 'error');
const propTypes = {
foo(props, propName, componentName) {
return new Error('some error');
},
};
const props = {foo: 'foo'};
const returnValue = PropTypes.checkPropTypes(
propTypes,
props,
'prop',
'testComponent',
null,
);
expect(console.error.calls.argsFor(0)[0]).toContain('some error');
expect(returnValue).toBe(undefined);
});
it('does not throw if validator throws', () => {
spyOn(console, 'error');
const propTypes = {
foo(props, propName, componentName) {
throw new Error('some error');
},
};
const props = {foo: 'foo'};
const returnValue = PropTypes.checkPropTypes(
propTypes,
props,
'prop',
'testComponent',
null,
);
expect(console.error.calls.argsFor(0)[0]).toContain('some error');
expect(returnValue).toBe(undefined);
});
it('warns if any of the propTypes is not a function', () => {
spyOn(console, 'error');
const propTypes = {
foo: PropTypes.invalid_type,
};
const props = { foo: 'foo' };
const returnValue = PropTypes.checkPropTypes(propTypes, props, 'prop', 'testComponent', null);
expect(console.error.calls.argsFor(0)[0]).toEqual(
'Warning: Failed prop type: testComponent: prop type `foo` is invalid; ' +
'it must be a function, usually from the `prop-types` package, but received `undefined`.' +
'This often happens because of typos such as `PropTypes.function` instead of `PropTypes.func`.'
);
expect(returnValue).toBe(undefined);
});
it('should warn for invalid validators inside shape', () => {
spyOn(console, 'error');
expectInvalidValidatorWarning(PropTypes.shape({ bar: PropTypes.invalid_type }), 'undefined');
expectInvalidValidatorWarning(PropTypes.shape({ bar: true }), 'boolean');
expectInvalidValidatorWarning(PropTypes.shape({ bar: 'true' }), 'string');
expectInvalidValidatorWarning(PropTypes.shape({ bar: null }), 'null');
});
it('should warn for invalid validators inside exact', () => {
spyOn(console, 'error');
expectInvalidValidatorWarning(PropTypes.exact({ bar: PropTypes.invalid_type }), 'undefined');
expectInvalidValidatorWarning(PropTypes.exact({ bar: true }), 'boolean');
expectInvalidValidatorWarning(PropTypes.exact({ bar: 'true' }), 'string');
expectInvalidValidatorWarning(PropTypes.exact({ bar: null }), 'null');
});
});
describe('resetWarningCache', () => {
it('should reset warning cache', () => {
spyOn(console, 'error');
const propTypes = { foo: undefined };
const props = { foo: 'foo' };
PropTypes.checkPropTypes(
propTypes,
props,
'prop',
'testComponent',
null,
);
PropTypes.resetWarningCache();
PropTypes.checkPropTypes(
propTypes,
props,
'prop',
'testComponent',
null,
);
expect(console.error.calls.count()).toEqual(2);
});
});
describe('Primitive Types', () => {
it('should warn for invalid strings', () => {
typeCheckFail(
PropTypes.string,
[],
'Invalid prop `testProp` of type `array` supplied to ' +
'`testComponent`, expected `string`.',
);
typeCheckFail(
PropTypes.string,
false,
'Invalid prop `testProp` of type `boolean` supplied to ' +
'`testComponent`, expected `string`.',
);
typeCheckFail(
PropTypes.string,
0,
'Invalid prop `testProp` of type `number` supplied to ' +
'`testComponent`, expected `string`.',
);
typeCheckFail(
PropTypes.string,
{},
'Invalid prop `testProp` of type `object` supplied to ' +
'`testComponent`, expected `string`.',
);
typeCheckFail(
PropTypes.string,
Symbol(),
'Invalid prop `testProp` of type `symbol` supplied to ' +
'`testComponent`, expected `string`.',
);
});
it('should fail date and regexp correctly', () => {
typeCheckFail(
PropTypes.string,
new Date(),
'Invalid prop `testProp` of type `date` supplied to ' +
'`testComponent`, expected `string`.',
);
typeCheckFail(
PropTypes.string,
/please/,
'Invalid prop `testProp` of type `regexp` supplied to ' +
'`testComponent`, expected `string`.',
);
});
it('should not warn for valid values', () => {
typeCheckPass(PropTypes.array, []);
typeCheckPass(PropTypes.bool, false);
typeCheckPass(PropTypes.func, function() {});
typeCheckPass(PropTypes.number, 0);
typeCheckPass(PropTypes.string, '');
typeCheckPass(PropTypes.object, {});
typeCheckPass(PropTypes.object, new Date());
typeCheckPass(PropTypes.object, /please/);
typeCheckPass(PropTypes.symbol, Symbol());
});
it('should be implicitly optional and not warn without values', () => {
typeCheckPass(PropTypes.string, null);
typeCheckPass(PropTypes.string, undefined);
});
it('should warn for missing required values', () => {
typeCheckFailRequiredValues(PropTypes.string.isRequired);
typeCheckFailRequiredValues(PropTypes.array.isRequired);
typeCheckFailRequiredValues(PropTypes.symbol.isRequired);
typeCheckFailRequiredValues(PropTypes.number.isRequired);
typeCheckFailRequiredValues(PropTypes.bool.isRequired);
typeCheckFailRequiredValues(PropTypes.func.isRequired);
typeCheckFailRequiredValues(PropTypes.shape({}).isRequired);
});
it('should warn if called manually in development', () => {
spyOn(console, 'error');
expectThrowsInDevelopment(PropTypes.array, /please/);
expectThrowsInDevelopment(PropTypes.array, []);
expectThrowsInDevelopment(PropTypes.array.isRequired, /please/);
expectThrowsInDevelopment(PropTypes.array.isRequired, []);
expectThrowsInDevelopment(PropTypes.array.isRequired, null);
expectThrowsInDevelopment(PropTypes.array.isRequired, undefined);
expectThrowsInDevelopment(PropTypes.bool, []);
expectThrowsInDevelopment(PropTypes.bool, true);
expectThrowsInDevelopment(PropTypes.bool.isRequired, []);
expectThrowsInDevelopment(PropTypes.bool.isRequired, true);
expectThrowsInDevelopment(PropTypes.bool.isRequired, null);
expectThrowsInDevelopment(PropTypes.bool.isRequired, undefined);
expectThrowsInDevelopment(PropTypes.func, false);
expectThrowsInDevelopment(PropTypes.func, function() {});
expectThrowsInDevelopment(PropTypes.func.isRequired, false);
expectThrowsInDevelopment(PropTypes.func.isRequired, function() {});
expectThrowsInDevelopment(PropTypes.func.isRequired, null);
expectThrowsInDevelopment(PropTypes.func.isRequired, undefined);
expectThrowsInDevelopment(PropTypes.number, function() {});
expectThrowsInDevelopment(PropTypes.number, 42);
expectThrowsInDevelopment(PropTypes.number.isRequired, function() {});
expectThrowsInDevelopment(PropTypes.number.isRequired, 42);
expectThrowsInDevelopment(PropTypes.number.isRequired, null);
expectThrowsInDevelopment(PropTypes.number.isRequired, undefined);
expectThrowsInDevelopment(PropTypes.string, 0);
expectThrowsInDevelopment(PropTypes.string, 'foo');
expectThrowsInDevelopment(PropTypes.string.isRequired, 0);
expectThrowsInDevelopment(PropTypes.string.isRequired, 'foo');
expectThrowsInDevelopment(PropTypes.string.isRequired, null);
expectThrowsInDevelopment(PropTypes.string.isRequired, undefined);
expectThrowsInDevelopment(PropTypes.symbol, 0);
expectThrowsInDevelopment(PropTypes.symbol, Symbol('Foo'));
expectThrowsInDevelopment(PropTypes.symbol.isRequired, 0);
expectThrowsInDevelopment(PropTypes.symbol.isRequired, Symbol('Foo'));
expectThrowsInDevelopment(PropTypes.symbol.isRequired, null);
expectThrowsInDevelopment(PropTypes.symbol.isRequired, undefined);
expectThrowsInDevelopment(PropTypes.object, '');
expectThrowsInDevelopment(PropTypes.object, {foo: 'bar'});
expectThrowsInDevelopment(PropTypes.object.isRequired, '');
expectThrowsInDevelopment(PropTypes.object.isRequired, {foo: 'bar'});
expectThrowsInDevelopment(PropTypes.object.isRequired, null);
expectThrowsInDevelopment(PropTypes.object.isRequired, undefined);
});
});
describe('Any type', () => {
it('should should accept any value', () => {
typeCheckPass(PropTypes.any, 0);
typeCheckPass(PropTypes.any, 'str');
typeCheckPass(PropTypes.any, []);
typeCheckPass(PropTypes.any, Symbol());
});
it('should be implicitly optional and not warn without values', () => {
typeCheckPass(PropTypes.any, null);
typeCheckPass(PropTypes.any, undefined);
});
it('should warn for missing required values', () => {
typeCheckFailRequiredValues(PropTypes.any.isRequired);
});
it('should warn if called manually in development', () => {
spyOn(console, 'error');
expectThrowsInDevelopment(PropTypes.any, null);
expectThrowsInDevelopment(PropTypes.any.isRequired, null);
expectThrowsInDevelopment(PropTypes.any.isRequired, undefined);
});
});
describe('ArrayOf Type', () => {
it('should fail for invalid argument', () => {
typeCheckFail(
PropTypes.arrayOf({foo: PropTypes.string}),
{foo: 'bar'},
'Property `testProp` of component `testComponent` has invalid PropType notation inside arrayOf.',
);
});
it('should support the arrayOf propTypes', () => {
typeCheckPass(PropTypes.arrayOf(PropTypes.number), [1, 2, 3]);
typeCheckPass(PropTypes.arrayOf(PropTypes.string), ['a', 'b', 'c']);
typeCheckPass(PropTypes.arrayOf(PropTypes.oneOf(['a', 'b'])), ['a', 'b']);
typeCheckPass(PropTypes.arrayOf(PropTypes.symbol), [Symbol(), Symbol()]);
});
it('should support arrayOf with complex types', () => {
typeCheckPass(
PropTypes.arrayOf(PropTypes.shape({a: PropTypes.number.isRequired})),
[{a: 1}, {a: 2}],
);
function Thing() {}
typeCheckPass(PropTypes.arrayOf(PropTypes.instanceOf(Thing)), [
new Thing(),
new Thing(),
]);
});
it('should warn with invalid items in the array', () => {
typeCheckFail(
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>>';
typeCheckFail(
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', () => {
typeCheckFail(
PropTypes.arrayOf(PropTypes.number),
{'0': 'maybe-array', length: 1},
'Invalid prop `testProp` of type `object` supplied to ' +
'`testComponent`, expected an array.',
);
typeCheckFail(
PropTypes.arrayOf(PropTypes.number),
123,
'Invalid prop `testProp` of type `number` supplied to ' +
'`testComponent`, expected an array.',
);
typeCheckFail(
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', () => {
typeCheckPass(PropTypes.arrayOf(PropTypes.number), []);
});
it('should be implicitly optional and not warn without values', () => {
typeCheckPass(PropTypes.arrayOf(PropTypes.number), null);
typeCheckPass(PropTypes.arrayOf(PropTypes.number), undefined);
});
it('should warn for missing required values', () => {
typeCheckFailRequiredValues(
PropTypes.arrayOf(PropTypes.number).isRequired,
);
});
it('should warn if called manually in development', () => {
spyOn(console, 'error');
expectThrowsInDevelopment(PropTypes.arrayOf({foo: PropTypes.string}), {
foo: 'bar',
});
expectThrowsInDevelopment(PropTypes.arrayOf(PropTypes.number), [
1,
2,
'b',
]);
expectThrowsInDevelopment(PropTypes.arrayOf(PropTypes.number), {
'0': 'maybe-array',
length: 1,
});
expectThrowsInDevelopment(
PropTypes.arrayOf(PropTypes.number).isRequired,
null,
);
expectThrowsInDevelopment(
PropTypes.arrayOf(PropTypes.number).isRequired,
undefined,
);
});
});
describe('Component Type', () => {
it('should support components', () => {
typeCheckPass(PropTypes.element, <div />);
});
it('should not support multiple components or scalar values', () => {
typeCheckFail(
PropTypes.element,
[<div />, <div />],
'Invalid prop `testProp` of type `array` supplied to `testComponent`, ' +
'expected a single ReactElement.',
);
typeCheckFail(
PropTypes.element,
123,
'Invalid prop `testProp` of type `number` supplied to `testComponent`, ' +
'expected a single ReactElement.',
);
typeCheckFail(
PropTypes.element,
'foo',
'Invalid prop `testProp` of type `string` supplied to `testComponent`, ' +
'expected a single ReactElement.',
);
typeCheckFail(
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', () => {
typeCheckPass(PropTypes.element, null);
typeCheckPass(PropTypes.element, undefined);
});
it('should warn for missing required values', () => {
typeCheckFailRequiredValues(PropTypes.element.isRequired);
});
it('should warn if called manually in development', () => {
spyOn(console, 'error');
expectThrowsInDevelopment(PropTypes.element, [<div />, <div />]);
expectThrowsInDevelopment(PropTypes.element, <div />);
expectThrowsInDevelopment(PropTypes.element, 123);
expectThrowsInDevelopment(PropTypes.element, 'foo');
expectThrowsInDevelopment(PropTypes.element, false);
expectThrowsInDevelopment(PropTypes.element.isRequired, null);
expectThrowsInDevelopment(PropTypes.element.isRequired, undefined);
});
});
describe('ElementType Types', () => {
it('should support native component', () => {
typeCheckPass(PropTypes.elementType, 'div');
});
it('should support stateless component', () => {
var MyComponent = () => <div />;
typeCheckPass(PropTypes.elementType, MyComponent);
});
it('should support stateful component', () => {
class MyComponent extends React.Component {
render() {
return <div />;
}
}
typeCheckPass(PropTypes.elementType, MyComponent);
});
(React.forwardRef ? it : it.skip)('should support forwardRef component', () => {
const MyForwardRef = React.forwardRef((props, ref) => <div ref={ref} />);
typeCheckPass(PropTypes.elementType, MyForwardRef);
});
(React.createContext ? it : it.skip)('should support context provider component', () => {
const MyContext = React.createContext('test');
typeCheckPass(PropTypes.elementType, MyContext.Provider);
});
(React.createContext ? it : it.skip)('should support context consumer component', () => {
const MyContext = React.createContext('test');
typeCheckPass(PropTypes.elementType, MyContext.Consumer);
});
it('should warn for invalid types', () => {
typeCheckFail(
PropTypes.elementType,
true,
'Invalid prop `testProp` of type `boolean` supplied to ' +
'`testComponent`, expected a single ReactElement type.',
);
typeCheckFail(
PropTypes.elementType,
{},
'Invalid prop `testProp` of type `object` supplied to ' +
'`testComponent`, expected a single ReactElement type.',
);
typeCheckFail(
PropTypes.elementType,
[],
'Invalid prop `testProp` of type `array` supplied to ' +
'`testComponent`, expected a single ReactElement type.',
);
it('should warn for missing required values', () => {
typeCheckFailRequiredValues(PropTypes.elementType.isRequired);
});
});
it('should warn for React element', () => {
typeCheckFail(
PropTypes.elementType,
<div />,
'Invalid prop `testProp` of type `object` supplied to ' +
'`testComponent`, expected a single ReactElement type.',
);
});
});
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>>';
typeCheckFail(
PropTypes.instanceOf(Person),
false,
'Invalid prop `testProp` of type `Boolean` supplied to ' +
'`testComponent`, expected instance of `' +
personName +
'`.',
);
typeCheckFail(
PropTypes.instanceOf(Person),
{},
'Invalid prop `testProp` of type `Object` supplied to ' +
'`testComponent`, expected instance of `' +
personName +
'`.',
);
typeCheckFail(
PropTypes.instanceOf(Person),
'',
'Invalid prop `testProp` of type `String` supplied to ' +
'`testComponent`, expected instance of `' +
personName +
'`.',
);
typeCheckFail(
PropTypes.instanceOf(Date),
{},
'Invalid prop `testProp` of type `Object` supplied to ' +
'`testComponent`, expected instance of `' +
dateName +
'`.',
);
typeCheckFail(
PropTypes.instanceOf(RegExp),
{},
'Invalid prop `testProp` of type `Object` supplied to ' +
'`testComponent`, expected instance of `' +
regExpName +
'`.',
);
typeCheckFail(
PropTypes.instanceOf(Person),
new Cat(),
'Invalid prop `testProp` of type `Cat` supplied to ' +
'`testComponent`, expected instance of `' +
personName +
'`.',
);
typeCheckFail(
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();
typeCheckPass(PropTypes.instanceOf(Person), new Person());
typeCheckPass(PropTypes.instanceOf(Person), new Engineer());
typeCheckPass(PropTypes.instanceOf(Date), new Date());
typeCheckPass(PropTypes.instanceOf(RegExp), /please/);
});
it('should be implicitly optional and not warn without values', () => {
typeCheckPass(PropTypes.instanceOf(String), null);
typeCheckPass(PropTypes.instanceOf(String), undefined);
});
it('should warn for missing required values', () => {
typeCheckFailRequiredValues(PropTypes.instanceOf(String).isRequired);
});
it('should warn if called manually in development', () => {
spyOn(console, 'error');
expectThrowsInDevelopment(PropTypes.instanceOf(Date), {});
expectThrowsInDevelopment(PropTypes.instanceOf(Date), new Date());
expectThrowsInDevelopment(PropTypes.instanceOf(Date).isRequired, {});
expectThrowsInDevelopment(
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.';
typeCheckFail(PropTypes.node, true, failMessage);
typeCheckFail(PropTypes.node, function() {}, failMessage);
typeCheckFail(PropTypes.node, {key: function() {}}, failMessage);
typeCheckFail(PropTypes.node, {key: <div />}, failMessage);
});
it('should not warn for valid values', () => {
function MyComponent() {}
MyComponent.prototype.render = function() {
return <div />;
};
typeCheckPass(PropTypes.node, <div />);
typeCheckPass(PropTypes.node, false);
typeCheckPass(PropTypes.node, <MyComponent />);
typeCheckPass(PropTypes.node, 'Some string');
typeCheckPass(PropTypes.node, []);
typeCheckPass(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() {
let i = 0;
return {
next: function() {
const done = ++i > 2;
return {value: done ? undefined : <MyComponent />, done: done};
},
};
},
};
typeCheckPass(PropTypes.node, iterable);
});
it('should not warn for entry iterables', () => {
function MyComponent() {}
MyComponent.prototype.render = function() {
return <div />;
};
const iterable = {
'@@iterator': function() {
let i = 0;
return {
next: function() {
const done = ++i > 2;
return {
value: done ? undefined : ['#' + i, <MyComponent />],
done: done,
};
},
};
},
};
iterable.entries = iterable['@@iterator'];
typeCheckPass(PropTypes.node, iterable);
});
it('should not warn for null/undefined if not required', () => {
typeCheckPass(PropTypes.node, null);
typeCheckPass(PropTypes.node, undefined);
});
it('should warn for missing required values', () => {
typeCheckFailRequiredValues(PropTypes.node.isRequired);
});
it('should accept empty array for required props', () => {
typeCheckPass(PropTypes.node.isRequired, []);
});
it('should warn if called manually in development', () => {
spyOn(console, 'error');
expectThrowsInDevelopment(PropTypes.node, 'node');
expectThrowsInDevelopment(PropTypes.node, {});
expectThrowsInDevelopment(PropTypes.node.isRequired, 'node');
expectThrowsInDevelopment(PropTypes.node.isRequired, undefined);
expectThrowsInDevelopment(PropTypes.node.isRequired, undefined);
});
});
describe('ObjectOf Type', () => {
it('should fail for invalid argument', () => {
typeCheckFail(
PropTypes.objectOf({foo: PropTypes.string}),
{foo: 'bar'},
'Property `testProp` of component `testComponent` has invalid PropType notation inside objectOf.',
);
});
it('should support the objectOf propTypes', () => {
typeCheckPass(PropTypes.objectOf(PropTypes.number), {a: 1, b: 2, c: 3});
typeCheckPass(PropTypes.objectOf(PropTypes.string), {
a: 'a',
b: 'b',
c: 'c',
});
typeCheckPass(PropTypes.objectOf(PropTypes.oneOf(['a', 'b'])), {
a: 'a',
b: 'b',
});
typeCheckPass(PropTypes.objectOf(PropTypes.symbol), {
a: Symbol(),
b: Symbol(),
c: Symbol(),
});
});
it('should support objectOf with complex types', () => {
typeCheckPass(
PropTypes.objectOf(PropTypes.shape({a: PropTypes.number.isRequired})),
{a: {a: 1}, b: {a: 2}},
);
function Thing() {}
typeCheckPass(PropTypes.objectOf(PropTypes.instanceOf(Thing)), {
a: new Thing(),
b: new Thing(),
});
});
it('should support objects with a null prototype', () => {
const nullObj = Object.create(null);
nullObj.test = 'a property';
typeCheckPass(PropTypes.objectOf(PropTypes.string), nullObj);
});
it('should warn with invalid items in the object', () => {
typeCheckFail(
PropTypes.objectOf(PropTypes.number),
{a: 1, b: 2, c: 'b'},
'Invalid prop `testProp.c` of type `string` supplied to `testComponent`, ' +
'expected `number`.',
);
typeCheckFail(
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>>';
typeCheckFail(
PropTypes.objectOf(PropTypes.instanceOf(Thing)),
{a: new Thing(), b: 'xyz'},
'Invalid prop `testProp.b` of type `String` supplied to ' +
'`testComponent`, expected instance of `' +
name +
'`.',
);
typeCheckFail(
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', () => {
typeCheckFail(
PropTypes.objectOf(PropTypes.number),
[1, 2],
'Invalid prop `testProp` of type `array` supplied to ' +
'`testComponent`, expected an object.',
);
typeCheckFail(
PropTypes.objectOf(PropTypes.number),
123,
'Invalid prop `testProp` of type `number` supplied to ' +
'`testComponent`, expected an object.',
);
typeCheckFail(
PropTypes.objectOf(PropTypes.number),
'string',
'Invalid prop `testProp` of type `string` supplied to ' +
'`testComponent`, expected an object.',
);
typeCheckFail(
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', () => {
typeCheckPass(PropTypes.objectOf(PropTypes.number), Object.create(null));
});
it('should not warn when passing an empty object', () => {
typeCheckPass(PropTypes.objectOf(PropTypes.number), {});
});
it('should not warn when passing an object with a hasOwnProperty property', () => {
typeCheckPass(PropTypes.objectOf(PropTypes.number), {
hasOwnProperty: 3,
});
});
it('should be implicitly optional and not warn without values', () => {
typeCheckPass(PropTypes.objectOf(PropTypes.number), null);
typeCheckPass(PropTypes.objectOf(PropTypes.number), undefined);
});
it('should warn for missing required values', () => {
typeCheckFailRequiredValues(
PropTypes.objectOf(PropTypes.number).isRequired,
);
});
it('should warn if called manually in development', () => {
spyOn(console, 'error');
expectThrowsInDevelopment(PropTypes.objectOf({foo: PropTypes.string}), {
foo: 'bar',
});
expectThrowsInDevelopment(PropTypes.objectOf(PropTypes.number), {