Skip to content

Commit

Permalink
[enzyme, enzyme-adapter-react-16*] shallow: only skip-cDU-on-nullis…
Browse files Browse the repository at this point in the history
…h when adapters opt in.
  • Loading branch information
ljharb committed Aug 22, 2018
1 parent cba495f commit b88b425
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ class ReactSixteenOneAdapter extends EnzymeAdapter {
componentDidUpdate: {
onSetState: true,
},
setState: {
skipsComponentDidUpdateOnNullish: true,
},
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ class ReactSixteenTwoAdapter extends EnzymeAdapter {
componentDidUpdate: {
onSetState: true,
},
setState: {
skipsComponentDidUpdateOnNullish: true,
},
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ class ReactSixteenThreeAdapter extends EnzymeAdapter {
onSetState: true,
},
getSnapshotBeforeUpdate: true,
setState: {
skipsComponentDidUpdateOnNullish: true,
},
},
};
}
Expand Down
3 changes: 3 additions & 0 deletions packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ class ReactSixteenAdapter extends EnzymeAdapter {
onSetState: true,
},
getSnapshotBeforeUpdate: true,
setState: {
skipsComponentDidUpdateOnNullish: true,
},
},
};
}
Expand Down
8 changes: 6 additions & 2 deletions packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ function getAdapterLifecycles({ options }) {

return {
...lifecycles,
setState: {
...lifecycles.setState,
},
...(componentDidUpdate && { componentDidUpdate }),
};
}
Expand Down Expand Up @@ -452,9 +455,10 @@ class ShallowWrapper {
? state.call(instance, prevState, prevProps)
: state;

// returning null or undefined prevents the update
// returning null or undefined prevents the update in React 16+
// https://github.com/facebook/react/pull/12756
const maybeHasUpdate = statePayload != null;
const maybeHasUpdate = !lifecycles.setState.skipsComponentDidUpdateOnNullish
|| statePayload != null;

// When shouldComponentUpdate returns false we shouldn't call componentDidUpdate.
// so we spy shouldComponentUpdate to get the result.
Expand Down

0 comments on commit b88b425

Please sign in to comment.