Skip to content

Commit

Permalink
[enzyme-adapter-react-16] [Fix] avoid propType warnings in FakeSuspen…
Browse files Browse the repository at this point in the history
…se wrapper

Fixes #2445.
  • Loading branch information
ljharb committed Sep 4, 2020
1 parent 679bf84 commit c4b0a99
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 21 deletions.
52 changes: 31 additions & 21 deletions packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,32 +132,42 @@ function transformSuspense(renderedEl, prerenderEl, { suspenseFallback }) {
children = replaceLazyWithFallback(children, fallback);
}

if (isStateful(prerenderEl.type)) {
class FakeSuspense extends prerenderEl.type {
render() {
const {
propTypes,
defaultProps,
contextTypes,
contextType,
childContextTypes,
} = renderedEl.type;

const FakeSuspense = Object.assign(
isStateful(prerenderEl.type)
? class FakeSuspense extends prerenderEl.type {
render() {
const { type, props } = prerenderEl;
return React.createElement(
type,
{ ...props, ...this.props },
children,
);
}
}
: function FakeSuspense(props) { // eslint-disable-line prefer-arrow-callback
return React.createElement(
prerenderEl.type,
{ ...prerenderEl.props, ...this.props },
renderedEl.type,
{ ...renderedEl.props, ...props },
children,
);
}
}

return React.createElement(FakeSuspense, null, children);
}

return React.createElement(
// eslint-disable-next-line prefer-arrow-callback
function FakeSuspense(props) {
return React.createElement(
renderedEl.type,
{ ...renderedEl.props, ...props },
children,
);
},
{
propTypes,
defaultProps,
contextTypes,
contextType,
childContextTypes,
},
null,
children,
);
return React.createElement(FakeSuspense, null, children);
}

function elementToTree(el) {
Expand Down
52 changes: 52 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1948,6 +1948,58 @@ describe('shallow', () => {

expect(wrapper.instance()).to.instanceOf(Bar);
});

describe('propType errors', () => {
class MyComponent extends React.Component {
render() {
const { fallback, requiredString } = this.props;
return (
<Suspense fallback={fallback}>
hello world {requiredString}
</Suspense>
);
}
}
MyComponent.propTypes = {
fallback: PropTypes.node.isRequired,
requiredString: PropTypes.string.isRequired,
};

function MySFC({ fallback, requiredString }) {
return (
<Suspense fallback={fallback}>
hello world {requiredString}
</Suspense>
);
}
MySFC.propTypes = MyComponent.propTypes;

wrap()
.withConsoleThrows()
.it('renders with no propType errors with a string fallback', () => {
shallow(<MyComponent requiredString="abc" fallback="loading..." />);
});

wrap()
.withConsoleThrows()
.it('renders with no propType errors with a component fallback', () => {
shallow(<MyComponent requiredString="abc" fallback={<Fallback />} />);
});

describeIf(is('> 0.13'), 'stateless function components (SFCs)', () => {
wrap()
.withConsoleThrows()
.it('renders with no propType errors with a string fallback', () => {
shallow(<MySFC requiredString="abc" fallback="loading..." />);
});

wrap()
.withConsoleThrows()
.it('renders with no propType errors with a component fallback', () => {
shallow(<MySFC requiredString="abc" fallback={<Fallback />} />);
});
});
});
});

describe('lifecycle methods', () => {
Expand Down

0 comments on commit c4b0a99

Please sign in to comment.