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

Fix componentDidUpdate no longer receives prevContext on React v16 #1139

Merged
merged 2 commits into from
Sep 26, 2017
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
7 changes: 7 additions & 0 deletions packages/enzyme-adapter-react-13/src/ReactThirteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ function instanceToTree(inst) {
}

class ReactThirteenAdapter extends EnzymeAdapter {
constructor() {
super();
this.options = {
...this.options,
supportPrevContextArgumentOfComponentDidUpdate: true,
};
}
createMountRenderer(options) {
assertDomAvailable('mount');
const domNode = options.attachTo || global.document.createElement('div');
Expand Down
7 changes: 7 additions & 0 deletions packages/enzyme-adapter-react-14/src/ReactFourteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ function instanceToTree(inst) {
}

class ReactFifteenAdapter extends EnzymeAdapter {
constructor() {
super();
this.options = {
...this.options,
supportPrevContextArgumentOfComponentDidUpdate: true,
};
}
createMountRenderer(options) {
assertDomAvailable('mount');
const domNode = options.attachTo || global.document.createElement('div');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ function instanceToTree(inst) {
}

class ReactFifteenFourAdapter extends EnzymeAdapter {
constructor() {
super();
this.options = {
...this.options,
supportPrevContextArgumentOfComponentDidUpdate: true,
};
}
createMountRenderer(options) {
assertDomAvailable('mount');
const domNode = options.attachTo || global.document.createElement('div');
Expand Down
7 changes: 7 additions & 0 deletions packages/enzyme-adapter-react-15/src/ReactFifteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ function instanceToTree(inst) {
}

class ReactFifteenAdapter extends EnzymeAdapter {
constructor() {
super();
this.options = {
...this.options,
supportPrevContextArgumentOfComponentDidUpdate: true,
};
}
createMountRenderer(options) {
assertDomAvailable('mount');
const domNode = options.attachTo || global.document.createElement('div');
Expand Down
3 changes: 1 addition & 2 deletions packages/enzyme-test-suite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,4 @@
"react": "^15.5.0",
"react-dom": "^15.5.0"
}
}

}
9 changes: 4 additions & 5 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3231,7 +3231,7 @@ describe('shallow', () => {
'componentDidUpdate',
{ foo: 'bar' }, { foo: 'baz' },
{ foo: 'state' }, { foo: 'state' },
{ foo: 'context' }, // this will be gone in 16
REACT16 ? undefined : { foo: 'context' },
],
[
'componentWillReceiveProps',
Expand All @@ -3257,7 +3257,7 @@ describe('shallow', () => {
'componentDidUpdate',
{ foo: 'baz' }, { foo: 'bax' },
{ foo: 'state' }, { foo: 'state' },
{ foo: 'context' },
REACT16 ? undefined : { foo: 'context' },
],
],
);
Expand Down Expand Up @@ -3440,7 +3440,6 @@ describe('shallow', () => {
});

context('updating state', () => {
// NOTE: There is a bug in react 16 shallow renderer where prevContext is not passed
it('should call shouldComponentUpdate, componentWillUpdate and componentDidUpdate', () => {
const spy = sinon.spy();

Expand Down Expand Up @@ -3501,7 +3500,7 @@ describe('shallow', () => {
'componentDidUpdate',
{ foo: 'props' }, { foo: 'props' },
{ foo: 'bar' }, { foo: 'baz' },
{ foo: 'context' },
REACT16 ? undefined : { foo: 'context' },
],
]);
});
Expand Down Expand Up @@ -3661,7 +3660,7 @@ describe('shallow', () => {
'componentDidUpdate',
{ foo: 'props' }, { foo: 'props' },
{ foo: 'state' }, { foo: 'state' },
{ foo: 'bar' },
REACT16 ? undefined : { foo: 'bar' },
],
]);
});
Expand Down
12 changes: 10 additions & 2 deletions packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,11 @@ class ShallowWrapper {
instance &&
typeof instance.componentDidUpdate === 'function'
) {
instance.componentDidUpdate(prevProps, state, prevContext);
if (adapter.options.supportPrevContextArgumentOfComponentDidUpdate) {
instance.componentDidUpdate(prevProps, state, prevContext);
} else {
instance.componentDidUpdate(prevProps, state);
}
}
this.update();
// If it doesn't need to rerender, update only its props.
Expand Down Expand Up @@ -401,7 +405,11 @@ class ShallowWrapper {
instance &&
typeof instance.componentDidUpdate === 'function'
) {
instance.componentDidUpdate(prevProps, prevState, prevContext);
if (adapter.options.supportPrevContextArgumentOfComponentDidUpdate) {
instance.componentDidUpdate(prevProps, prevState, prevContext);
} else {
instance.componentDidUpdate(prevProps, prevState);
}
}
this.update();
});
Expand Down