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

Commit

Permalink
[Tests] add missing test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jan 5, 2022
1 parent 88e1b3d commit b93deca
Show file tree
Hide file tree
Showing 3 changed files with 312 additions and 0 deletions.
104 changes: 104 additions & 0 deletions __tests__/PropTypesDevelopmentStandalone-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,110 @@ describe('PropTypesDevelopmentStandalone', () => {
});
});

describe('Exact Types', () => {
it('should warn for non objects', () => {
spyOn(console, 'error');
expectThrowsInDevelopment(
PropTypes.exact({}),
'some string'
);
expectThrowsInDevelopment(
PropTypes.exact({}),
['array']
);
});

it('should not warn for empty values', () => {
typeCheckPass(PropTypes.exact({}), undefined);
typeCheckPass(PropTypes.exact({}), null);
typeCheckPass(PropTypes.exact({}), {});
});

it('should not warn for an empty object', () => {
typeCheckPass(PropTypes.exact({}).isRequired, {});
});

it('should warn for non specified types', () => {
typeCheckFail(
PropTypes.exact({}),
{key: 1},
'Warning: Failed prop type: Invalid prop `testProp` key `key` supplied to `testComponent`.' +
'\nBad object: {' +
'\n \"key\": 1' +
'\n}' +
'\nValid keys: []'
);
});

it('should not warn for valid types', () => {
typeCheckPass(PropTypes.exact({key: PropTypes.number}), {key: 1});
});

it('should warn for required valid types', () => {
typeCheckFail(
PropTypes.exact({key: PropTypes.number.isRequired}),
{},
'The prop `testProp.key` is marked as required in `testComponent`, ' +
'but its value is `undefined`.',
);
});

it('should warn for the first required type', () => {
typeCheckFail(
PropTypes.exact({
key: PropTypes.number.isRequired,
secondKey: PropTypes.number.isRequired,
}),
{},
'The prop `testProp.key` is marked as required in `testComponent`, ' +
'but its value is `undefined`.',
);
});

it('should warn for invalid key types', () => {
typeCheckFail(
PropTypes.exact({key: PropTypes.number}),
{key: 'abc'},
'Invalid prop `testProp.key` of type `string` supplied to `testComponent`, ' +
'expected `number`.',
);
});

it('should be implicitly optional and not warn without values', () => {
typeCheckPass(
PropTypes.exact(PropTypes.exact({key: PropTypes.number})),
null,
);
typeCheckPass(
PropTypes.exact(PropTypes.exact({key: PropTypes.number})),
undefined,
);
});

it('should warn for missing required values', () => {
typeCheckFailRequiredValues(
PropTypes.exact({key: PropTypes.number}).isRequired,
);
});

it('should warn if called manually in development', () => {
spyOn(console, 'error');
expectThrowsInDevelopment(PropTypes.exact({}), 'some string');
expectThrowsInDevelopment(PropTypes.exact({foo: PropTypes.number}), {
foo: 42,
});
expectThrowsInDevelopment(
PropTypes.exact({key: PropTypes.number}).isRequired,
null,
);
expectThrowsInDevelopment(
PropTypes.exact({key: PropTypes.number}).isRequired,
undefined,
);
expectThrowsInDevelopment(PropTypes.element, <div />);
});
});

describe('Symbol Type', () => {
it('should warn for non-symbol', () => {
typeCheckFail(
Expand Down
91 changes: 91 additions & 0 deletions __tests__/PropTypesProductionReact15-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,97 @@ describe('PropTypesProductionReact15', () => {
});
});

describe('Exact Types', () => {
it('should warn for non objects', () => {
expectNoop(
PropTypes.exact({}),
'some string'
);
expectNoop(
PropTypes.exact({}),
['array']
);
});

it('should not warn for empty values', () => {
expectNoop(PropTypes.exact({}), undefined);
expectNoop(PropTypes.exact({}), null);
expectNoop(PropTypes.exact({}), {});
});

it('should not warn for an empty object', () => {
expectNoop(PropTypes.exact({}).isRequired, {});
});

it('expectNoop warn for non specified types', () => {
expectNoop(
PropTypes.exact({}),
{key: 1}
);
});

it('should not warn for valid types', () => {
expectNoop(PropTypes.exact({key: PropTypes.number}), {key: 1});
});

it('should warn for required valid types', () => {
expectNoop(
PropTypes.exact({key: PropTypes.number.isRequired}),
{}
);
});

it('should warn for the first required type', () => {
expectNoop(
PropTypes.exact({
key: PropTypes.number.isRequired,
secondKey: PropTypes.number.isRequired,
}),
{}
);
});

it('should warn for invalid key types', () => {
expectNoop(
PropTypes.exact({key: PropTypes.number}),
{key: 'abc'}
);
});

it('should be implicitly optional and not warn without values', () => {
expectNoop(
PropTypes.exact(PropTypes.exact({key: PropTypes.number})),
null,
);
expectNoop(
PropTypes.exact(PropTypes.exact({key: PropTypes.number})),
undefined,
);
});

it('should warn for missing required values', () => {
expectNoop(
PropTypes.exact({key: PropTypes.number}).isRequired,
);
});

it('should warn if called manually in development', () => {
expectNoop(PropTypes.exact({}), 'some string');
expectNoop(PropTypes.exact({foo: PropTypes.number}), {
foo: 42,
});
expectNoop(
PropTypes.exact({key: PropTypes.number}).isRequired,
null,
);
expectNoop(
PropTypes.exact({key: PropTypes.number}).isRequired,
undefined,
);
expectNoop(PropTypes.element, <div />);
});
});

describe('Symbol Type', () => {
it('should warn for non-symbol', () => {
expectNoop(
Expand Down
117 changes: 117 additions & 0 deletions __tests__/PropTypesProductionStandalone-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,123 @@ describe('PropTypesProductionStandalone', () => {
});
});

describe('Exact Types', () => {
it('should warn for non objects', () => {
spyOn(console, 'error');
expectThrowsInProduction(
PropTypes.exact({}),
'some string'
);
expectThrowsInProduction(
PropTypes.exact({}),
['array']
);
});

it('should not warn for empty values', () => {
typeCheckPass(PropTypes.exact({}), undefined);
typeCheckPass(PropTypes.exact({}), null);
typeCheckPass(PropTypes.exact({}), {});
});

it('should not warn for an empty object', () => {
typeCheckPass(PropTypes.exact({}).isRequired, {});
});

it('should warn for non specified types', () => {
expectThrowsInProduction(
PropTypes.exact({}),
{key: 1},
'Warning: Failed prop type: Invalid prop `testProp` key `key` supplied to `testComponent`.' +
'\nBad object: {' +
'\n \"key\": 1' +
'\n}' +
'\nValid keys: []'
);
});

it('should not warn for valid types', () => {
typeCheckPass(PropTypes.exact({key: PropTypes.number}), {key: 1});
});

it('should warn for required valid types', () => {
expectThrowsInProduction(
PropTypes.exact({key: PropTypes.number.isRequired}),
{},
'The prop `testProp.key` is marked as required in `testComponent`, ' +
'but its value is `undefined`.',
);
});

it('should warn for the first required type', () => {
expectThrowsInProduction(
PropTypes.exact({
key: PropTypes.number.isRequired,
secondKey: PropTypes.number.isRequired,
}),
{},
'The prop `testProp.key` is marked as required in `testComponent`, ' +
'but its value is `undefined`.',
);
});

it('should warn for invalid key types', () => {
expectThrowsInProduction(
PropTypes.exact({key: PropTypes.number}),
{key: 'abc'},
'Invalid prop `testProp.key` of type `string` supplied to `testComponent`, ' +
'expected `number`.',
);
});

it('should be implicitly optional and not warn without values', () => {
typeCheckPass(
PropTypes.exact(PropTypes.exact({key: PropTypes.number})),
null,
);
typeCheckPass(
PropTypes.exact(PropTypes.exact({key: PropTypes.number})),
undefined,
);
});

it('should warn for missing required values', () => {
expectThrowsInProduction(
PropTypes.exact({key: PropTypes.number}).isRequired,
);
});
});

describe('Symbol Type', () => {
it('should warn for non-symbol', () => {
expectThrowsInProduction(
PropTypes.symbol,
'hello',
'Invalid prop `testProp` of type `string` supplied to ' +
'`testComponent`, expected `symbol`.',
);
expectThrowsInProduction(
PropTypes.symbol,
function() {},
'Invalid prop `testProp` of type `function` supplied to ' +
'`testComponent`, expected `symbol`.',
);
expectThrowsInProduction(
PropTypes.symbol,
{
'@@toStringTag': 'Katana',
},
'Invalid prop `testProp` of type `object` supplied to ' +
'`testComponent`, expected `symbol`.',
);
});

it('should not warn for a polyfilled Symbol', () => {
const CoreSymbol = require('core-js/library/es6/symbol');
typeCheckPass(PropTypes.symbol, CoreSymbol('core-js'));
});
});

describe('checkPropTypes', () => {
it('does not call validators', () => {
spyOn(console, 'error');
Expand Down

0 comments on commit b93deca

Please sign in to comment.