Skip to content

Commit

Permalink
[Fix] shallow: avoid a crash with malformed lifecycle options
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Mar 4, 2020
1 parent c077fcd commit 440c062
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
40 changes: 40 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Expand Up @@ -2222,6 +2222,46 @@ describe('shallow', () => {
});
});

describe('malformed lifecycle options', () => {
it('throws on invalid `enableComponentDidUpdateOnSetState` lifecycle config', () => {
const adapter = getAdapter();
const { lifecycles = {} } = adapter;
const options = {
enableComponentDidUpdateOnSetState: !!adapter.enableComponentDidUpdateOnSetState,
lifecycles: {
...lifecycles,
componentDidUpdate: {
...lifecycles.componentDidUpdate,
onSetState: !adapter.enableComponentDidUpdateOnSetState,
},
},
};
expect(() => shallow(<div />, options)).to.throw(
TypeError,
'the legacy enableComponentDidUpdateOnSetState option should be matched by `lifecycles: { componentDidUpdate: { onSetState: true } }`, for compatibility',
);
});

it('throws on invalid `supportPrevContextArgumentOfComponentDidUpdate` lifecycle config', () => {
const adapter = getAdapter();
const { lifecycles = {} } = adapter;
const options = {
supportPrevContextArgumentOfComponentDidUpdate: !!adapter.supportPrevContextArgumentOfComponentDidUpdate,
lifecycles: {
...lifecycles,
componentDidUpdate: {
...lifecycles.componentDidUpdate,
prevContext: !adapter.supportPrevContextArgumentOfComponentDidUpdate,
},
},
};
expect(() => shallow(<div />, options)).to.throw(
TypeError,
'the legacy supportPrevContextArgumentOfComponentDidUpdate option should be matched by `lifecycles: { componentDidUpdate: { prevContext: true } }`, for compatibility',
);
});
});

describeIf(is('>= 16.3'), 'getDerivedStateFromProps', () => {
let spy;

Expand Down
2 changes: 1 addition & 1 deletion packages/enzyme/src/ShallowWrapper.js
Expand Up @@ -83,7 +83,7 @@ function validateOptions(options) {
disableLifecycleMethods,
enableComponentDidUpdateOnSetState,
supportPrevContextArgumentOfComponentDidUpdate,
lifecycles,
lifecycles = {},
} = options;
if (typeof lifecycleExperimental !== 'undefined' && typeof lifecycleExperimental !== 'boolean') {
throw new Error('lifecycleExperimental must be either true or false if provided');
Expand Down

0 comments on commit 440c062

Please sign in to comment.