Skip to content

Commit

Permalink
[core] Use documented direct propTypes call
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon authored and oliviertassinari committed Oct 13, 2018
1 parent 522c041 commit f8be00d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
8 changes: 3 additions & 5 deletions packages/material-ui/src/utils/chainPropType.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import secret from 'prop-types/lib/ReactPropTypesSecret';

function chainPropType(propType, check) {
/* istanbul ignore if */
if (process.env.NODE_ENV === 'production') {
return () => null;
}

return (props, propName, componentName, location, propFullName) => {
const err = check(props, propName, componentName, location, propFullName, secret);
return function validate(...args) {
const err = check(...args);

if (err) {
return err;
}

return propType(props, propName, componentName, location, propFullName, secret);
return propType(...args);
};
}

Expand Down
36 changes: 22 additions & 14 deletions packages/material-ui/src/utils/chainPropType.test.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,45 @@
import { assert } from 'chai';
import PropTypes from 'prop-types';
import { mock } from 'sinon';
import chainPropType from './chainPropType';

describe('chainPropType', () => {
const props = {};
const propName = 'children';
const props = { [propName]: 'some string' };
const componentName = 'ComponentName';
const location = 'prop';
const propFullName = null;
let warning;

beforeEach(() => {
warning = mock(console).expects('error');
});

afterEach(() => {
warning.restore();
});

it('should have the right shape', () => {
assert.strictEqual(typeof chainPropType, 'function');
});

it('should return null for supported properties', () => {
const result = chainPropType(PropTypes.string, () => null)(
it('should not warn for supported types', () => {
PropTypes.checkPropTypes(
{ [propName]: chainPropType(PropTypes.string, () => null) },
props,
propName,
componentName,
location,
propFullName,
componentName,
);
assert.strictEqual(result, null);
assert.strictEqual(warning.called, false);
});

it('should return an error for unsupported properties', () => {
const result = chainPropType(PropTypes.string, () => new Error('something is wrong'))(
it('should warn for unsupported properties', () => {
PropTypes.checkPropTypes(
{ [propName]: chainPropType(PropTypes.string, () => new Error('something is wrong')) },
props,
propName,
componentName,
location,
propFullName,
componentName,
);
assert.match(result.message, /something is wrong/);
assert.strictEqual(warning.calledOnce, true);
assert.match(warning.firstCall.args[0], /something is wrong/);
});
});

0 comments on commit f8be00d

Please sign in to comment.