Skip to content

Commit

Permalink
Merge pull request #1139 from koba04/fix-prev-context-arg-of-cdu
Browse files Browse the repository at this point in the history
Fix componentDidUpdate no longer receives prevContext on React v16
  • Loading branch information
lelandrichardson committed Sep 26, 2017
2 parents 8cf9661 + a08df67 commit 92da2b3
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 9 deletions.
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

0 comments on commit 92da2b3

Please sign in to comment.