Skip to content

Commit 85982f6

Browse files
committed
Test that dispatches in componentWillUnmonunt are ignored by the component
1 parent 1ebdad0 commit 85982f6

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

test/components/connect.spec.js

+35
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,41 @@ describe('React', () => {
859859
expect(mapStateToPropsCalls).toBe(1)
860860
})
861861

862+
it('should not attempt to set state when dispatching in componentWillUnmount', () => {
863+
const store = createStore(stringBuilder)
864+
let mapStateToPropsCalls = 0
865+
866+
/*eslint-disable no-unused-vars */
867+
@connect(
868+
(state) => ({ calls: mapStateToPropsCalls++ }),
869+
dispatch => ({ dispatch })
870+
)
871+
/*eslint-enable no-unused-vars */
872+
class Container extends Component {
873+
componentWillUnmount() {
874+
this.props.dispatch({ type: 'APPEND', body: 'a' })
875+
}
876+
render() {
877+
return <Passthrough {...this.props} />
878+
}
879+
}
880+
881+
const div = document.createElement('div')
882+
ReactDOM.render(
883+
<ProviderMock store={store}>
884+
<Container />
885+
</ProviderMock>,
886+
div
887+
)
888+
expect(mapStateToPropsCalls).toBe(1)
889+
890+
const spy = expect.spyOn(console, 'error')
891+
ReactDOM.unmountComponentAtNode(div)
892+
spy.destroy()
893+
expect(spy.calls.length).toBe(0)
894+
expect(mapStateToPropsCalls).toBe(1)
895+
})
896+
862897
it('should shallowly compare the selected state to prevent unnecessary updates', () => {
863898
const store = createStore(stringBuilder)
864899
const spy = expect.createSpy(() => ({}))

0 commit comments

Comments
 (0)