Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] fix crash when a custom propType return lacks .data; call hasOwnProperty properly #370

Merged
merged 3 commits into from Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions __tests__/PropTypesDevelopmentReact15.js
Expand Up @@ -1385,6 +1385,37 @@ describe('PropTypesDevelopmentReact15', () => {
);
expectWarningInDevelopment(PropTypes.element, <div />);
});

it('works with oneOfType', () => {
typeCheckPass(
PropTypes.exact({ foo: PropTypes.oneOfType([PropTypes.number, PropTypes.string]) }),
{ foo: 42 }
);
typeCheckPass(
PropTypes.exact({ foo: PropTypes.oneOfType([PropTypes.number, PropTypes.string]) }),
{ foo: '42' }
);
typeCheckFail(
PropTypes.exact({ foo: PropTypes.oneOfType([PropTypes.number, PropTypes.string]) }),
{ foo: 42, bar: 'what is 6 * 7' },
`Warning: Failed prop type: Invalid prop \`testProp\` key \`bar\` supplied to \`testComponent\`.
Bad object: {
"foo": 42,
"bar": "what is 6 * 7"
}
Valid keys: [
"foo"
]`
);
});

it('works with a custom propType', () => {
typeCheckFail(
PropTypes.oneOfType([() => new Error('hi')]),
{},
'Warning: Failed prop type: Invalid prop `testProp` supplied to `testComponent`.'
)
});
});

describe('Symbol Type', () => {
Expand Down
135 changes: 135 additions & 0 deletions __tests__/PropTypesDevelopmentStandalone-test.js
Expand Up @@ -1354,6 +1354,141 @@ 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 />);
});

it('works with oneOfType', () => {
typeCheckPass(
PropTypes.exact({ foo: PropTypes.oneOfType([PropTypes.number, PropTypes.string]) }),
{ foo: 42 }
);
typeCheckPass(
PropTypes.exact({ foo: PropTypes.oneOfType([PropTypes.number, PropTypes.string]) }),
{ foo: '42' }
);
typeCheckFail(
PropTypes.exact({ foo: PropTypes.oneOfType([PropTypes.number, PropTypes.string]) }),
{ foo: 42, bar: 'what is 6 * 7' },
`Warning: Failed prop type: Invalid prop \`testProp\` key \`bar\` supplied to \`testComponent\`.
Bad object: {
"foo": 42,
"bar": "what is 6 * 7"
}
Valid keys: [
"foo"
]`
);
});

it('works with a custom propType', () => {
typeCheckFail(
PropTypes.oneOfType([() => new Error('hi')]),
{},
'Warning: Failed prop type: Invalid prop `testProp` supplied to `testComponent`.'
)
});
});

describe('Symbol Type', () => {
it('should warn for non-symbol', () => {
typeCheckFail(
Expand Down
113 changes: 113 additions & 0 deletions __tests__/PropTypesProductionReact15-test.js
Expand Up @@ -1023,6 +1023,119 @@ 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 />);
});

it('works with oneOfType', () => {
expectNoop(
PropTypes.exact({ foo: PropTypes.oneOfType([PropTypes.number, PropTypes.string]) }),
{ foo: 42 }
);
expectNoop(
PropTypes.exact({ foo: PropTypes.oneOfType([PropTypes.number, PropTypes.string]) }),
{ foo: '42' }
);
expectNoop(
PropTypes.exact({ foo: PropTypes.oneOfType([PropTypes.number, PropTypes.string]) }),
{ foo: 42, bar: 'what is 6 * 7' }
);
});

it('works with a custom propType', () => {
expectNoop(
PropTypes.oneOfType([() => new Error('hi')]),
{}
)
});
});

describe('Symbol Type', () => {
it('should warn for non-symbol', () => {
expectNoop(
Expand Down