Skip to content

Commit

Permalink
✨ handle forceUpdate for react
Browse files Browse the repository at this point in the history
  • Loading branch information
FBerthelot committed May 6, 2019
1 parent ec4e57f commit 5cfabe0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/component-test-utils-react/src/shallow.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ShallowRender {
this._render();
}

_render(customProps) {
_render(customProps, forceUpdate) {
let firstRender = false;

const prevDispatcher = ReactCurrentDispatcher.current;
Expand Down Expand Up @@ -65,6 +65,7 @@ class ShallowRender {
this._instance.props = props;

if (
!forceUpdate &&
!firstRender &&
typeof this._instance.shouldComponentUpdate === 'function' &&
!this._instance.shouldComponentUpdate(
Expand Down
23 changes: 22 additions & 1 deletion packages/component-test-utils-react/src/shallow.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,28 @@ describe('react shallow render', () => {
describe('componentDidCatch', () => {
// TODO
});
describe('forceUpdate', () => {});
describe('forceUpdate', () => {
it('should trigger a render whenever shouldComponentUpdate return false', () => {
class Component extends React.Component {
shouldComponentUpdate() {
return false;
}

render() {
return (
<div onClick={() => this.forceUpdate()}>{this.props.value}</div>
);
}
}

const cmp = shallow(<Component value={1}/>);

cmp.setProps({value: 42});
cmp.dispatchEvent('Click');

expect(cmp.html()).toEqual('<div onClick="[onClick]">42</div>');
});
});

describe('displayName', () => {
it('should ovewrite the name displayed by html method', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/component-test-utils-react/src/updater/updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class Updater {

enqueueForceUpdate(publicInstance, callback) {
this._enqueueCallback(callback, publicInstance);
this._shallow._render();
this._shallow._render(undefined, true);
}

enqueueReplaceState(publicInstance, completeState, callback) {
Expand Down

0 comments on commit 5cfabe0

Please sign in to comment.