Skip to content

Commit

Permalink
[enzyme-adapter-utils] [fix] shallow/mount: renderProp: avoid w…
Browse files Browse the repository at this point in the history
…arning when render prop returns `null`
  • Loading branch information
bscharm authored and ljharb committed Mar 30, 2019
1 parent 03ef729 commit 6ad2165
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/enzyme-adapter-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"author": "Leland Richardson <leland.richardson@airbnb.com>",
"license": "MIT",
"dependencies": {
"airbnb-prop-types": "^2.12.0",
"function.prototype.name": "^1.1.0",
"object.assign": "^4.1.0",
"object.fromentries": "^2.0.0",
Expand Down
10 changes: 8 additions & 2 deletions packages/enzyme-adapter-utils/src/wrapWithSimpleWrapper.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import React from 'react';
import { intersects } from 'semver';
import { or, explicitNull } from 'airbnb-prop-types';
import PropTypes from 'prop-types';

const propTypes = {
children: PropTypes.node.isRequired,
children: or([explicitNull().isRequired, PropTypes.node.isRequired]),
};

const defaultProps = {
children: undefined,
};

const Wrapper = (intersects('>= 0.14', React.version)
// eslint-disable-next-line prefer-arrow-callback
? () => Object.assign(function SimpleSFCWrapper({ children }) {
return children;
}, { propTypes })
}, { propTypes, defaultProps })
: () => {
class SimpleClassWrapper extends React.Component {
render() {
Expand All @@ -19,6 +24,7 @@ const Wrapper = (intersects('>= 0.14', React.version)
}
}
SimpleClassWrapper.propTypes = propTypes;
SimpleClassWrapper.defaultProps = defaultProps;
return SimpleClassWrapper;
}
)();
Expand Down
13 changes: 13 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5611,6 +5611,19 @@ describeWithDOM('mount', () => {
wrapper.find(ComponentWithRenderProp).renderProp('r')(NaN);
});

it('works with null', () => {
const wrapper = mount(<MyComponent val={null} />);

wrapper.find(ComponentWithRenderProp).renderProp('r')(null);
});

// FIXME: figure out how to test this reliably
it.skip('throws with undefined', () => {
const wrapper = mount(<MyComponent val="" />);

expect(() => wrapper.find(ComponentWithRenderProp).renderProp('r')(undefined)).to.throw();
});

it('works with arrays', () => {
const wrapper = mount(<MyComponent val={[]} />);

Expand Down
12 changes: 12 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5792,6 +5792,18 @@ describe('shallow', () => {
wrapper.find(ComponentWithRenderProp).renderProp('r')(NaN);
});

it('works with null', () => {
const wrapper = shallow(<MyComponent val={null} />);

wrapper.find(ComponentWithRenderProp).renderProp('r')(null);
});

it('throws with undefined', () => {
const wrapper = shallow(<MyComponent val="" />);

expect(() => wrapper.find(ComponentWithRenderProp).renderProp('r')(undefined).shallow()).to.throw();
});

it('works with arrays', () => {
const wrapper = shallow(<MyComponent val={[]} />);

Expand Down

0 comments on commit 6ad2165

Please sign in to comment.