Skip to content

Commit

Permalink
Test that dispatches in componentWillUnmonunt are ignored by the comp…
Browse files Browse the repository at this point in the history
…onent
  • Loading branch information
gaearon committed Apr 13, 2016
1 parent 1ebdad0 commit 85982f6
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions test/components/connect.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Passthrough {...this.props} />
}
}

const div = document.createElement('div')
ReactDOM.render(
<ProviderMock store={store}>
<Container />
</ProviderMock>,
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(() => ({}))
Expand Down

0 comments on commit 85982f6

Please sign in to comment.