Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaking: lifecycleExperimental by default #1140

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 21 additions & 24 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3197,7 +3197,6 @@ describe('shallow', () => {
<Foo foo="bar" />,
{
context: { foo: 'context' },
lifecycleExperimental: true,
},
);
wrapper.setProps({ foo: 'baz' });
Expand Down Expand Up @@ -3287,7 +3286,7 @@ describe('shallow', () => {
}
}

const wrapper = shallow(<Foo a="a" b="b" />, { lifecycleExperimental: true });
const wrapper = shallow(<Foo a="a" b="b" />);

wrapper.setProps({ b: 'c', d: 'e' });

Expand Down Expand Up @@ -3338,7 +3337,7 @@ describe('shallow', () => {
}
}

const wrapper = shallow(<Foo foo="bar" />, { lifecycleExperimental: true });
const wrapper = shallow(<Foo foo="bar" />);
expect(wrapper.instance().props.foo).to.equal('bar');
wrapper.setProps({ foo: 'baz' });
expect(wrapper.instance().props.foo).to.equal('baz');
Expand Down Expand Up @@ -3371,7 +3370,7 @@ describe('shallow', () => {
return <div>{this.props.foo}</div>;
}
}
const result = shallow(<Foo />, { lifecycleExperimental: true });
const result = shallow(<Foo />);
expect(spy).to.have.property('callCount', 1);
result.setProps({ name: 'bar' });
expect(spy).to.have.property('callCount', 2);
Expand Down Expand Up @@ -3400,7 +3399,7 @@ describe('shallow', () => {
return <div>{this.props.foo}</div>;
}
}
const result = shallow(<Foo />, { lifecycleExperimental: true });
const result = shallow(<Foo />);
expect(spy).to.have.property('callCount', 1);
result.setProps({ name: 'bar' });
expect(spy).to.have.property('callCount', 3);
Expand Down Expand Up @@ -3431,7 +3430,7 @@ describe('shallow', () => {
return <div>{this.props.foo}</div>;
}
}
const result = shallow(<Foo />, { lifecycleExperimental: true });
const result = shallow(<Foo />);
expect(spy).to.have.property('callCount', 1);
result.setProps({ name: 'bar' });
expect(spy).to.have.property('callCount', 3);
Expand Down Expand Up @@ -3473,11 +3472,10 @@ describe('shallow', () => {
<Foo foo="props" />,
{
context: { foo: 'context' },
lifecycleExperimental: true,
},
);
wrapper.setState({ foo: 'baz' });
expect(spy.args).to.deep.equal([
const expected = [
[
'render',
],
Expand All @@ -3496,13 +3494,16 @@ describe('shallow', () => {
[
'render',
],
[
];
if (!REACT16) {
expected.push([
'componentDidUpdate',
{ foo: 'props' }, { foo: 'props' },
{ foo: 'bar' }, { foo: 'baz' },
REACT16 ? undefined : { foo: 'context' },
],
]);
{ foo: 'context' },
]);
}
expect(spy.args).to.deep.equal(expected);
});

it('should cancel rendering when Component returns false in shouldComponentUpdate', () => {
Expand All @@ -3529,7 +3530,7 @@ describe('shallow', () => {
return <div>foo</div>;
}
}
const wrapper = shallow(<Foo />, { lifecycleExperimental: true });
const wrapper = shallow(<Foo />);
expect(wrapper.instance().state.foo).to.equal('bar');
wrapper.setState({ foo: 'baz' });
expect(wrapper.instance().state.foo).to.equal('baz');
Expand Down Expand Up @@ -3559,7 +3560,7 @@ describe('shallow', () => {
return <div>{this.state.name}</div>;
}
}
const result = shallow(<Foo />, { lifecycleExperimental: true });
const result = shallow(<Foo />);
expect(spy).to.have.property('callCount', 1);
result.setState({ name: 'bar' });
expect(spy).to.have.property('callCount', 3);
Expand Down Expand Up @@ -3591,7 +3592,7 @@ describe('shallow', () => {
return <div>{this.state.name}</div>;
}
}
const result = shallow(<Foo />, { lifecycleExperimental: true });
const result = shallow(<Foo />);
expect(spy).to.have.property('callCount', 1);
result.setState({ name: 'bar' });
expect(spy).to.have.property('callCount', 3);
Expand Down Expand Up @@ -3631,7 +3632,6 @@ describe('shallow', () => {
<Foo foo="props" />,
{
context: { foo: 'bar' },
lifecycleExperimental: true,
},
);
expect(wrapper.instance().context.foo).to.equal('bar');
Expand Down Expand Up @@ -3690,7 +3690,6 @@ describe('shallow', () => {
<Foo />,
{
context: { foo: 'bar' },
lifecycleExperimental: true,
},
);
wrapper.setContext({ foo: 'baz' });
Expand Down Expand Up @@ -3723,7 +3722,6 @@ describe('shallow', () => {
<Foo />,
{
context: { foo: 'bar' },
lifecycleExperimental: true,
},
);
expect(spy).to.have.property('callCount', 1);
Expand Down Expand Up @@ -3760,7 +3758,6 @@ describe('shallow', () => {
<Foo />,
{
context: { foo: 'bar' },
lifecycleExperimental: true,
},
);
expect(spy).to.have.property('callCount', 1);
Expand All @@ -3781,13 +3778,13 @@ describe('shallow', () => {
return <div>foo</div>;
}
}
const wrapper = shallow(<Foo />, { lifecycleExperimental: true });
const wrapper = shallow(<Foo />);
wrapper.unmount();
expect(spy).to.have.property('callCount', 1);
});
});

it('should not call when lifecycleExperimental flag is false', () => {
it('should not call when disableLifecycleMethods flag is true', () => {
const spy = sinon.spy();
class Foo extends React.Component {
componentDidMount() {
Expand All @@ -3797,11 +3794,11 @@ describe('shallow', () => {
return <div>foo</div>;
}
}
shallow(<Foo />, { lifecycleExperimental: false });
shallow(<Foo />, { disableLifecycleMethods: true });
expect(spy).to.have.property('callCount', 0);
});

it('should call shouldComponentUpdate when lifecycleExperimental flag is false', () => {
it('should call shouldComponentUpdate when disableLifecycleMethods flag is true', () => {
const spy = sinon.spy();
class Foo extends React.Component {
constructor(props) {
Expand All @@ -3822,7 +3819,7 @@ describe('shallow', () => {
<Foo foo="foo" />,
{
context: { foo: 'foo' },
lifecycleExperimental: false,
disableLifecycleMethods: true,
},
);
expect(spy).to.have.property('callCount', 0);
Expand Down
4 changes: 3 additions & 1 deletion packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
nodeEqual,
nodeMatches,
getAdapter,
makeOptions,
sym,
privateSet,
cloneElement,
Expand Down Expand Up @@ -66,12 +67,13 @@ function filterWhereUnwrapped(wrapper, predicate) {
* @class ReactWrapper
*/
class ReactWrapper {
constructor(nodes, root, options = {}) {
constructor(nodes, root, passedOptions = {}) {
if (!global.window && !global.document) {
throw new Error(
'It looks like you called `mount()` without a global document being loaded.',
);
}
const options = makeOptions(passedOptions);

if (!root) {
privateSet(this, UNRENDERED, nodes);
Expand Down
14 changes: 8 additions & 6 deletions packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
isCustomComponentElement,
ITERATOR_SYMBOL,
getAdapter,
makeOptions,
sym,
privateSet,
cloneElement,
Expand Down Expand Up @@ -108,8 +109,9 @@ function getRootNode(node) {
* @class ShallowWrapper
*/
class ShallowWrapper {
constructor(nodes, root, options = {}) {
validateOptions(options);
constructor(nodes, root, passedOptions = {}) {
validateOptions(passedOptions);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come ReactWrapper doesn't validate its options?

const options = makeOptions(passedOptions);
if (!root) {
privateSet(this, ROOT, this);
privateSet(this, UNRENDERED, nodes);
Expand All @@ -118,7 +120,7 @@ class ShallowWrapper {
this[RENDERER].render(nodes, options.context);
const instance = this[RENDERER].getNode().instance;
if (
options.lifecycleExperimental &&
!options.disableLifecycleMethods &&
instance &&
typeof instance.componentDidMount === 'function'
) {
Expand Down Expand Up @@ -277,7 +279,7 @@ class ShallowWrapper {
// make sure that componentWillReceiveProps is called before shouldComponentUpdate
let originalComponentWillReceiveProps;
if (
this[OPTIONS].lifecycleExperimental &&
!this[OPTIONS].disableLifecycleMethods &&
instance &&
typeof instance.componentWillReceiveProps === 'function'
) {
Expand All @@ -288,7 +290,7 @@ class ShallowWrapper {
// dirty hack: avoid calling shouldComponentUpdate twice
let originalShouldComponentUpdate;
if (
this[OPTIONS].lifecycleExperimental &&
!this[OPTIONS].disableLifecycleMethods &&
instance &&
typeof instance.shouldComponentUpdate === 'function'
) {
Expand All @@ -307,7 +309,7 @@ class ShallowWrapper {
instance.shouldComponentUpdate = originalShouldComponentUpdate;
}
if (
this[OPTIONS].lifecycleExperimental &&
!this[OPTIONS].disableLifecycleMethods &&
instance &&
typeof instance.componentDidUpdate === 'function'
) {
Expand Down
7 changes: 7 additions & 0 deletions packages/enzyme/src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ export function getAdapter(options = {}) {
return adapter;
}

export function makeOptions(options) {
return {
...configuration.get(),
...options,
};
}

export function isCustomComponentElement(inst, adapter) {
return !!inst && adapter.isValidElement(inst) && typeof inst.type === 'function';
}
Expand Down