Skip to content

Commit

Permalink
ignoreDefaultProps should work for forwardRef components as well (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradbarrow authored Jan 31, 2020
1 parent d1f86ee commit b3eb5c7
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
},
"license": "MIT",
"dependencies": {
"lodash": "^4.17.15"
"lodash": "^4.17.15",
"react-is": "^16.12.0"
},
"peerDependencies": {
"enzyme": "^3.4.0"
Expand Down
3 changes: 2 additions & 1 deletion src/shallow.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import omitBy from 'lodash/omitBy';
import isNil from 'lodash/isNil';
import {isValidElementType} from 'react-is';

import {childrenOfNode, propsOfNode} from 'enzyme/build/RSTTraversal';

Expand All @@ -25,7 +26,7 @@ function getProps(node, options) {

if (
options.ignoreDefaultProps === true &&
typeof node.type === 'function' &&
isValidElementType(node.type) &&
node.type.defaultProps &&
key in node.type.defaultProps &&
node.type.defaultProps[key] === val
Expand Down
23 changes: 23 additions & 0 deletions tests/__snapshots__/shallow.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,15 @@ exports[`should bleed default props from class child component into snapshot wit
</span>
`;

exports[`should bleed default props from forwardRef child component into snapshot without the option 1`] = `
<span>
<ForwardRef
falsyValue={false}
value="hi mum"
/>
</span>
`;

exports[`should bleed default props from functional child component into snapshot without the option 1`] = `
<span>
<WithDefaultProps
Expand All @@ -308,6 +317,12 @@ exports[`should not bleed default props from class child component into snapshot
</span>
`;

exports[`should not bleed default props from forwardRef child component into snapshot 1`] = `
<span>
<ForwardRef />
</span>
`;

exports[`should not bleed default props from functional child component into snapshot 1`] = `
<span>
<WithDefaultProps />
Expand All @@ -322,6 +337,14 @@ exports[`should set prop that has a different value from default prop values of
</span>
`;

exports[`should set prop that has a different value from default prop values of forwardRef component 1`] = `
<span>
<ForwardRef
value="yeah, man"
/>
</span>
`;

exports[`should set prop that has a different value from default prop values of functional component 1`] = `
<span>
<WithDefaultProps
Expand Down
19 changes: 19 additions & 0 deletions tests/fixtures/forwardRef.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';

export const ForwardRefWithDefaultProps = React.forwardRef((props, ref) => (
<div
className={`basic-class ${props.className}`}
onClick={function handleOnClick() {}}
ref={ref}
>
<div id="group-id" className="group">
<span>{props.children}</span>
<span className="empty" />
</div>
</div>
));

ForwardRefWithDefaultProps.defaultProps = {
value: 'hi mum',
falsyValue: false,
};
34 changes: 34 additions & 0 deletions tests/shallow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import {
ClassWithDefaultProps,
} from './fixtures/class';

import {
ForwardRefWithDefaultProps,
} from './fixtures/forwardRef';

Enzyme.configure({adapter: new Adapter()});

function WrapperComponent(props) {
Expand Down Expand Up @@ -352,3 +356,33 @@ it('should set prop that has a different value from default prop values of class

expect(shallowToJson(wrapper, { ignoreDefaultProps: true })).toMatchSnapshot();
});

it('should bleed default props from forwardRef child component into snapshot without the option', () => {
const wrapper = shallow(
<ComponentWithChildren>
<ForwardRefWithDefaultProps />
</ComponentWithChildren>,
);

expect(shallowToJson(wrapper)).toMatchSnapshot();
});

it('should not bleed default props from forwardRef child component into snapshot', () => {
const wrapper = shallow(
<ComponentWithChildren>
<ForwardRefWithDefaultProps />
</ComponentWithChildren>,
);

expect(shallowToJson(wrapper, { ignoreDefaultProps: true })).toMatchSnapshot();
});

it('should set prop that has a different value from default prop values of forwardRef component', () => {
const wrapper = shallow(
<ComponentWithChildren>
<ForwardRefWithDefaultProps value="yeah, man" />
</ComponentWithChildren>,
);

expect(shallowToJson(wrapper, { ignoreDefaultProps: true })).toMatchSnapshot();
});
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4361,6 +4361,11 @@ react-is@^16.10.2, react-is@^16.8.1, react-is@^16.8.4, react-is@^16.8.6, react-i
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.11.0.tgz#b85dfecd48ad1ce469ff558a882ca8e8313928fa"
integrity sha512-gbBVYR2p8mnriqAwWx9LbuUrShnAuSCNnuPGyc7GJrMVQtPDAh8iLpv7FRuMPFb56KkaVZIYSz1PrjI9q0QPCw==

react-is@^16.12.0:
version "16.12.0"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.12.0.tgz#2cc0fe0fba742d97fd527c42a13bec4eeb06241c"
integrity sha512-rPCkf/mWBtKc97aLL9/txD8DZdemK0vkA3JMLShjlJB3Pj3s+lpf1KaBzMfQrAmhMQB0n1cU/SUGgKKBCe837Q==

react-test-renderer@^16.0.0-0, react-test-renderer@^16.6.0:
version "16.11.0"
resolved "https://registry.yarnpkg.com/react-test-renderer/-/react-test-renderer-16.11.0.tgz#72574566496462c808ac449b0287a4c0a1a7d8f8"
Expand Down

0 comments on commit b3eb5c7

Please sign in to comment.