Skip to content

Commit

Permalink
Shallow renderer: pass component instance to setState updater as `thi…
Browse files Browse the repository at this point in the history
…s` (facebook#12784)

* Shallow renderer: pass component instance to setState updater as `this`

* Run prettier
  • Loading branch information
Hypnosphi authored and gaearon committed May 11, 2018
1 parent b0726e9 commit 4f459bb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/react-test-renderer/src/ReactShallowRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,11 @@ class Updater {
const currentState = this._renderer._newState || publicInstance.state;

if (typeof partialState === 'function') {
partialState = partialState(currentState, publicInstance.props);
partialState = partialState.call(
publicInstance,
currentState,
publicInstance.props,
);
}

// Null and undefined are treated as no-ops.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,27 @@ describe('ReactShallowRenderer', () => {
expect(result.props.children).toEqual(2);
});

it('can access component instance from setState updater function', done => {
let instance;

class SimpleComponent extends React.Component {
state = {};

render() {
instance = this;
return null;
}
}

const shallowRenderer = createRenderer();
shallowRenderer.render(<SimpleComponent />);

instance.setState(function updater(state, props) {
expect(this).toBe(instance);
done();
});
});

it('can setState with a callback', () => {
let instance;

Expand Down

0 comments on commit 4f459bb

Please sign in to comment.