Skip to content

Commit

Permalink
[Fix] fix crash when a custom propType return lacks .data; call `ha…
Browse files Browse the repository at this point in the history
…sOwnProperty` properly

Fixes #369
  • Loading branch information
ljharb committed Jan 5, 2022
1 parent b93deca commit 90c8c47
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 1 deletion.
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
31 changes: 31 additions & 0 deletions __tests__/PropTypesDevelopmentStandalone-test.js
Expand Up @@ -1456,6 +1456,37 @@ describe('PropTypesDevelopmentStandalone', () => {
);
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', () => {
Expand Down
22 changes: 22 additions & 0 deletions __tests__/PropTypesProductionReact15-test.js
Expand Up @@ -1112,6 +1112,28 @@ describe('PropTypesProductionReact15', () => {
);
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', () => {
Expand Down
31 changes: 31 additions & 0 deletions __tests__/PropTypesProductionStandalone-test.js
Expand Up @@ -326,6 +326,37 @@ describe('PropTypesProductionStandalone', () => {
PropTypes.exact({key: PropTypes.number}).isRequired,
);
});

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' }
);
expectThrowsInProduction(
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', () => {
expectThrowsInProduction(
PropTypes.oneOfType([() => new Error('hi')]),
{},
'Warning: Failed prop type: Invalid prop `testProp` supplied to `testComponent`.'
)
});
});

describe('Symbol Type', () => {
Expand Down
2 changes: 1 addition & 1 deletion factoryWithTypeCheckers.js
Expand Up @@ -390,7 +390,7 @@ module.exports = function(isValidElement, throwOnDirectAccess) {
if (checkerResult == null) {
return null;
}
if (checkerResult.data.hasOwnProperty('expectedType')) {
if (checkerResult.data && has(checkerResult.data, 'expectedType')) {
expectedTypes.push(checkerResult.data.expectedType);
}
}
Expand Down

0 comments on commit 90c8c47

Please sign in to comment.