Skip to content

Commit

Permalink
add public interface on PropTypes object
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Way committed Aug 14, 2018
1 parent 9903ce5 commit fc5f1e4
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -284,6 +284,6 @@ PropTypes.checkPropTypes(myPropTypes, props, 'prop', 'MyComponent');
// `MyComponent`, expected `number`.
```

## PropTypes.checkPropTypes.resetWarningCache
## PropTypes.resetWarningCache()

`PropTypes.checkPropTypes` only `console.error.log(...)`s a given message once. To reset the cache while testing call `PropTypes.checkPropTypes.resetWarningCache()`
`PropTypes.checkPropTypes(...)` only `console.error.log(...)`s a given message once. To reset the cache while testing call `PropTypes.resetWarningCache()`
24 changes: 24 additions & 0 deletions __tests__/PropTypesDevelopmentReact15.js
Expand Up @@ -226,6 +226,30 @@ describe('PropTypesDevelopmentReact15', () => {
});
});

describe('resetWarningCache', () => {
it("should reset warning cache", () => {
spyOn(console, 'error');
const propTypes = { foo: undefined };
const props = { foo: 'foo' };
PropTypes.checkPropTypes(
propTypes,
props,
'prop',
'testComponent',
null,
);
PropTypes.resetWarningCache();
PropTypes.checkPropTypes(
propTypes,
props,
'prop',
'testComponent',
null,
);
expect(console.error.calls.count()).toEqual(2);
});
});

describe('Primitive Types', () => {
it('should warn for invalid strings', () => {
typeCheckFail(
Expand Down
24 changes: 24 additions & 0 deletions __tests__/PropTypesDevelopmentStandalone-test.js
Expand Up @@ -222,6 +222,30 @@ describe('PropTypesDevelopmentStandalone', () => {
});
});

describe('resetWarningCache', () => {
it("should reset warning cache", () => {
spyOn(console, 'error')
const propTypes = { foo: undefined };
const props = { foo: 'foo' };
PropTypes.checkPropTypes(
propTypes,
props,
'prop',
'testComponent',
null,
);
PropTypes.resetWarningCache();
PropTypes.checkPropTypes(
propTypes,
props,
'prop',
'testComponent',
null,
);
expect(console.error.calls.count()).toEqual(2);
})
})

describe('Primitive Types', () => {
it('should warn for invalid strings', () => {
typeCheckFail(
Expand Down
9 changes: 9 additions & 0 deletions __tests__/PropTypesProductionReact15-test.js
Expand Up @@ -970,4 +970,13 @@ describe('PropTypesProductionReact15', () => {
});
});
});
describe('resetWarningCache', () => {
it("should provide empty function", () => {
spyOn(console, 'error')

var spy = jest.fn();
PropTypes.resetWarningCache();
expect(spy).not.toBeCalled();
});
});
});
12 changes: 11 additions & 1 deletion __tests__/PropTypesProductionStandalone-test.js
Expand Up @@ -236,11 +236,21 @@ describe('PropTypesProductionStandalone', function() {
describe('checkPropTypes.resetWarningCache', () => {
it("should provide empty function", () => {
spyOn(console, 'error')

var spy = jest.fn();
PropTypes.checkPropTypes.resetWarningCache();
expect(spy).not.toBeCalled();
});
});
});

describe('resetWarningCache', () => {
it("should provide empty function", () => {
spyOn(console, 'error')

var spy = jest.fn();
PropTypes.resetWarningCache();
expect(spy).not.toBeCalled();
});
});
});
1 change: 1 addition & 0 deletions factoryWithThrowingShims.js
Expand Up @@ -54,6 +54,7 @@ module.exports = function() {

ReactPropTypes.checkPropTypes = emptyFunction;
ReactPropTypes.checkPropTypes.resetWarningCache = emptyFunction;
ReactPropTypes.resetWarningCache = emptyFunction;
ReactPropTypes.PropTypes = ReactPropTypes;

return ReactPropTypes;
Expand Down
1 change: 1 addition & 0 deletions factoryWithTypeCheckers.js
Expand Up @@ -550,6 +550,7 @@ module.exports = function(isValidElement, throwOnDirectAccess) {
}

ReactPropTypes.checkPropTypes = checkPropTypes;
ReactPropTypes.resetWarningCache = checkPropTypes.resetWarningCache;
ReactPropTypes.PropTypes = ReactPropTypes;

return ReactPropTypes;
Expand Down

0 comments on commit fc5f1e4

Please sign in to comment.