Skip to content
This repository was archived by the owner on Sep 1, 2024. It is now read-only.

Commit b93deca

Browse files
committed
[Tests] add missing test coverage
1 parent 88e1b3d commit b93deca

File tree

3 files changed

+312
-0
lines changed

3 files changed

+312
-0
lines changed

__tests__/PropTypesDevelopmentStandalone-test.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,110 @@ describe('PropTypesDevelopmentStandalone', () => {
13541354
});
13551355
});
13561356

1357+
describe('Exact Types', () => {
1358+
it('should warn for non objects', () => {
1359+
spyOn(console, 'error');
1360+
expectThrowsInDevelopment(
1361+
PropTypes.exact({}),
1362+
'some string'
1363+
);
1364+
expectThrowsInDevelopment(
1365+
PropTypes.exact({}),
1366+
['array']
1367+
);
1368+
});
1369+
1370+
it('should not warn for empty values', () => {
1371+
typeCheckPass(PropTypes.exact({}), undefined);
1372+
typeCheckPass(PropTypes.exact({}), null);
1373+
typeCheckPass(PropTypes.exact({}), {});
1374+
});
1375+
1376+
it('should not warn for an empty object', () => {
1377+
typeCheckPass(PropTypes.exact({}).isRequired, {});
1378+
});
1379+
1380+
it('should warn for non specified types', () => {
1381+
typeCheckFail(
1382+
PropTypes.exact({}),
1383+
{key: 1},
1384+
'Warning: Failed prop type: Invalid prop `testProp` key `key` supplied to `testComponent`.' +
1385+
'\nBad object: {' +
1386+
'\n \"key\": 1' +
1387+
'\n}' +
1388+
'\nValid keys: []'
1389+
);
1390+
});
1391+
1392+
it('should not warn for valid types', () => {
1393+
typeCheckPass(PropTypes.exact({key: PropTypes.number}), {key: 1});
1394+
});
1395+
1396+
it('should warn for required valid types', () => {
1397+
typeCheckFail(
1398+
PropTypes.exact({key: PropTypes.number.isRequired}),
1399+
{},
1400+
'The prop `testProp.key` is marked as required in `testComponent`, ' +
1401+
'but its value is `undefined`.',
1402+
);
1403+
});
1404+
1405+
it('should warn for the first required type', () => {
1406+
typeCheckFail(
1407+
PropTypes.exact({
1408+
key: PropTypes.number.isRequired,
1409+
secondKey: PropTypes.number.isRequired,
1410+
}),
1411+
{},
1412+
'The prop `testProp.key` is marked as required in `testComponent`, ' +
1413+
'but its value is `undefined`.',
1414+
);
1415+
});
1416+
1417+
it('should warn for invalid key types', () => {
1418+
typeCheckFail(
1419+
PropTypes.exact({key: PropTypes.number}),
1420+
{key: 'abc'},
1421+
'Invalid prop `testProp.key` of type `string` supplied to `testComponent`, ' +
1422+
'expected `number`.',
1423+
);
1424+
});
1425+
1426+
it('should be implicitly optional and not warn without values', () => {
1427+
typeCheckPass(
1428+
PropTypes.exact(PropTypes.exact({key: PropTypes.number})),
1429+
null,
1430+
);
1431+
typeCheckPass(
1432+
PropTypes.exact(PropTypes.exact({key: PropTypes.number})),
1433+
undefined,
1434+
);
1435+
});
1436+
1437+
it('should warn for missing required values', () => {
1438+
typeCheckFailRequiredValues(
1439+
PropTypes.exact({key: PropTypes.number}).isRequired,
1440+
);
1441+
});
1442+
1443+
it('should warn if called manually in development', () => {
1444+
spyOn(console, 'error');
1445+
expectThrowsInDevelopment(PropTypes.exact({}), 'some string');
1446+
expectThrowsInDevelopment(PropTypes.exact({foo: PropTypes.number}), {
1447+
foo: 42,
1448+
});
1449+
expectThrowsInDevelopment(
1450+
PropTypes.exact({key: PropTypes.number}).isRequired,
1451+
null,
1452+
);
1453+
expectThrowsInDevelopment(
1454+
PropTypes.exact({key: PropTypes.number}).isRequired,
1455+
undefined,
1456+
);
1457+
expectThrowsInDevelopment(PropTypes.element, <div />);
1458+
});
1459+
});
1460+
13571461
describe('Symbol Type', () => {
13581462
it('should warn for non-symbol', () => {
13591463
typeCheckFail(

__tests__/PropTypesProductionReact15-test.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,97 @@ describe('PropTypesProductionReact15', () => {
10231023
});
10241024
});
10251025

1026+
describe('Exact Types', () => {
1027+
it('should warn for non objects', () => {
1028+
expectNoop(
1029+
PropTypes.exact({}),
1030+
'some string'
1031+
);
1032+
expectNoop(
1033+
PropTypes.exact({}),
1034+
['array']
1035+
);
1036+
});
1037+
1038+
it('should not warn for empty values', () => {
1039+
expectNoop(PropTypes.exact({}), undefined);
1040+
expectNoop(PropTypes.exact({}), null);
1041+
expectNoop(PropTypes.exact({}), {});
1042+
});
1043+
1044+
it('should not warn for an empty object', () => {
1045+
expectNoop(PropTypes.exact({}).isRequired, {});
1046+
});
1047+
1048+
it('expectNoop warn for non specified types', () => {
1049+
expectNoop(
1050+
PropTypes.exact({}),
1051+
{key: 1}
1052+
);
1053+
});
1054+
1055+
it('should not warn for valid types', () => {
1056+
expectNoop(PropTypes.exact({key: PropTypes.number}), {key: 1});
1057+
});
1058+
1059+
it('should warn for required valid types', () => {
1060+
expectNoop(
1061+
PropTypes.exact({key: PropTypes.number.isRequired}),
1062+
{}
1063+
);
1064+
});
1065+
1066+
it('should warn for the first required type', () => {
1067+
expectNoop(
1068+
PropTypes.exact({
1069+
key: PropTypes.number.isRequired,
1070+
secondKey: PropTypes.number.isRequired,
1071+
}),
1072+
{}
1073+
);
1074+
});
1075+
1076+
it('should warn for invalid key types', () => {
1077+
expectNoop(
1078+
PropTypes.exact({key: PropTypes.number}),
1079+
{key: 'abc'}
1080+
);
1081+
});
1082+
1083+
it('should be implicitly optional and not warn without values', () => {
1084+
expectNoop(
1085+
PropTypes.exact(PropTypes.exact({key: PropTypes.number})),
1086+
null,
1087+
);
1088+
expectNoop(
1089+
PropTypes.exact(PropTypes.exact({key: PropTypes.number})),
1090+
undefined,
1091+
);
1092+
});
1093+
1094+
it('should warn for missing required values', () => {
1095+
expectNoop(
1096+
PropTypes.exact({key: PropTypes.number}).isRequired,
1097+
);
1098+
});
1099+
1100+
it('should warn if called manually in development', () => {
1101+
expectNoop(PropTypes.exact({}), 'some string');
1102+
expectNoop(PropTypes.exact({foo: PropTypes.number}), {
1103+
foo: 42,
1104+
});
1105+
expectNoop(
1106+
PropTypes.exact({key: PropTypes.number}).isRequired,
1107+
null,
1108+
);
1109+
expectNoop(
1110+
PropTypes.exact({key: PropTypes.number}).isRequired,
1111+
undefined,
1112+
);
1113+
expectNoop(PropTypes.element, <div />);
1114+
});
1115+
});
1116+
10261117
describe('Symbol Type', () => {
10271118
it('should warn for non-symbol', () => {
10281119
expectNoop(

__tests__/PropTypesProductionStandalone-test.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,123 @@ describe('PropTypesProductionStandalone', () => {
241241
});
242242
});
243243

244+
describe('Exact Types', () => {
245+
it('should warn for non objects', () => {
246+
spyOn(console, 'error');
247+
expectThrowsInProduction(
248+
PropTypes.exact({}),
249+
'some string'
250+
);
251+
expectThrowsInProduction(
252+
PropTypes.exact({}),
253+
['array']
254+
);
255+
});
256+
257+
it('should not warn for empty values', () => {
258+
typeCheckPass(PropTypes.exact({}), undefined);
259+
typeCheckPass(PropTypes.exact({}), null);
260+
typeCheckPass(PropTypes.exact({}), {});
261+
});
262+
263+
it('should not warn for an empty object', () => {
264+
typeCheckPass(PropTypes.exact({}).isRequired, {});
265+
});
266+
267+
it('should warn for non specified types', () => {
268+
expectThrowsInProduction(
269+
PropTypes.exact({}),
270+
{key: 1},
271+
'Warning: Failed prop type: Invalid prop `testProp` key `key` supplied to `testComponent`.' +
272+
'\nBad object: {' +
273+
'\n \"key\": 1' +
274+
'\n}' +
275+
'\nValid keys: []'
276+
);
277+
});
278+
279+
it('should not warn for valid types', () => {
280+
typeCheckPass(PropTypes.exact({key: PropTypes.number}), {key: 1});
281+
});
282+
283+
it('should warn for required valid types', () => {
284+
expectThrowsInProduction(
285+
PropTypes.exact({key: PropTypes.number.isRequired}),
286+
{},
287+
'The prop `testProp.key` is marked as required in `testComponent`, ' +
288+
'but its value is `undefined`.',
289+
);
290+
});
291+
292+
it('should warn for the first required type', () => {
293+
expectThrowsInProduction(
294+
PropTypes.exact({
295+
key: PropTypes.number.isRequired,
296+
secondKey: PropTypes.number.isRequired,
297+
}),
298+
{},
299+
'The prop `testProp.key` is marked as required in `testComponent`, ' +
300+
'but its value is `undefined`.',
301+
);
302+
});
303+
304+
it('should warn for invalid key types', () => {
305+
expectThrowsInProduction(
306+
PropTypes.exact({key: PropTypes.number}),
307+
{key: 'abc'},
308+
'Invalid prop `testProp.key` of type `string` supplied to `testComponent`, ' +
309+
'expected `number`.',
310+
);
311+
});
312+
313+
it('should be implicitly optional and not warn without values', () => {
314+
typeCheckPass(
315+
PropTypes.exact(PropTypes.exact({key: PropTypes.number})),
316+
null,
317+
);
318+
typeCheckPass(
319+
PropTypes.exact(PropTypes.exact({key: PropTypes.number})),
320+
undefined,
321+
);
322+
});
323+
324+
it('should warn for missing required values', () => {
325+
expectThrowsInProduction(
326+
PropTypes.exact({key: PropTypes.number}).isRequired,
327+
);
328+
});
329+
});
330+
331+
describe('Symbol Type', () => {
332+
it('should warn for non-symbol', () => {
333+
expectThrowsInProduction(
334+
PropTypes.symbol,
335+
'hello',
336+
'Invalid prop `testProp` of type `string` supplied to ' +
337+
'`testComponent`, expected `symbol`.',
338+
);
339+
expectThrowsInProduction(
340+
PropTypes.symbol,
341+
function() {},
342+
'Invalid prop `testProp` of type `function` supplied to ' +
343+
'`testComponent`, expected `symbol`.',
344+
);
345+
expectThrowsInProduction(
346+
PropTypes.symbol,
347+
{
348+
'@@toStringTag': 'Katana',
349+
},
350+
'Invalid prop `testProp` of type `object` supplied to ' +
351+
'`testComponent`, expected `symbol`.',
352+
);
353+
});
354+
355+
it('should not warn for a polyfilled Symbol', () => {
356+
const CoreSymbol = require('core-js/library/es6/symbol');
357+
typeCheckPass(PropTypes.symbol, CoreSymbol('core-js'));
358+
});
359+
});
360+
244361
describe('checkPropTypes', () => {
245362
it('does not call validators', () => {
246363
spyOn(console, 'error');

0 commit comments

Comments
 (0)