From 85982f64ed891c6e191e46a1d076edf569670f2e Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Wed, 13 Apr 2016 16:53:45 +0100 Subject: [PATCH] Test that dispatches in componentWillUnmonunt are ignored by the component --- test/components/connect.spec.js | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/test/components/connect.spec.js b/test/components/connect.spec.js index d2dff98ac..71ce2beb7 100644 --- a/test/components/connect.spec.js +++ b/test/components/connect.spec.js @@ -859,6 +859,41 @@ describe('React', () => { expect(mapStateToPropsCalls).toBe(1) }) + it('should not attempt to set state when dispatching in componentWillUnmount', () => { + const store = createStore(stringBuilder) + let mapStateToPropsCalls = 0 + + /*eslint-disable no-unused-vars */ + @connect( + (state) => ({ calls: mapStateToPropsCalls++ }), + dispatch => ({ dispatch }) + ) + /*eslint-enable no-unused-vars */ + class Container extends Component { + componentWillUnmount() { + this.props.dispatch({ type: 'APPEND', body: 'a' }) + } + render() { + return + } + } + + const div = document.createElement('div') + ReactDOM.render( + + + , + div + ) + expect(mapStateToPropsCalls).toBe(1) + + const spy = expect.spyOn(console, 'error') + ReactDOM.unmountComponentAtNode(div) + spy.destroy() + expect(spy.calls.length).toBe(0) + expect(mapStateToPropsCalls).toBe(1) + }) + it('should shallowly compare the selected state to prevent unnecessary updates', () => { const store = createStore(stringBuilder) const spy = expect.createSpy(() => ({}))