Skip to content

Commit

Permalink
Fix componentDidUpdate no longer receives prevContext on React v16
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 authored and lelandrichardson committed Sep 26, 2017
1 parent 8cf9661 commit 88d9c76
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class ReactThirteenAdapter extends EnzymeAdapter {
let isDOM = false;
let cachedNode = null;
return {
supportPrevContextArgumentOfComponentDidUpdate: true,
render(el, context) {
cachedNode = el;
/* eslint consistent-return: 0 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class ReactFifteenAdapter extends EnzymeAdapter {
let isDOM = false;
let cachedNode = null;
return {
supportPrevContextArgumentOfComponentDidUpdate: true,
render(el, context) {
cachedNode = el;
/* eslint consistent-return: 0 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ class ReactFifteenFourAdapter extends EnzymeAdapter {
let isDOM = false;
let cachedNode = null;
return {
supportPrevContextArgumentOfComponentDidUpdate: true,
render(el, context) {
cachedNode = el;
/* eslint consistent-return: 0 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ class ReactFifteenAdapter extends EnzymeAdapter {
let isDOM = false;
let cachedNode = null;
return {
supportPrevContextArgumentOfComponentDidUpdate: true,
render(el, context) {
cachedNode = el;
/* eslint consistent-return: 0 */
Expand Down
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
6 changes: 5 additions & 1 deletion 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 (this[RENDERER].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

0 comments on commit 88d9c76

Please sign in to comment.