-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
tests and fixes #457 #472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tests and fixes #457 #472
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -910,6 +910,86 @@ describe('React', () => { | |
expect(mapStateToPropsCalls).toBe(1) | ||
}) | ||
|
||
it('should not attempt to set state after unmounting nested components', () => { | ||
const store = createStore(() => ({})) | ||
let mapStateToPropsCalls = 0 | ||
|
||
let linkA, linkB | ||
|
||
let App = ({ children, setLocation }) => { | ||
const onClick = to => event => { | ||
event.preventDefault() | ||
setLocation(to) | ||
} | ||
/* eslint-disable react/jsx-no-bind */ | ||
return ( | ||
<div> | ||
<a href="#" onClick={onClick('a')} ref={c => { linkA = c }}>A</a> | ||
<a href="#" onClick={onClick('b')} ref={c => { linkB = c }}>B</a> | ||
{children} | ||
</div> | ||
) | ||
/* eslint-enable react/jsx-no-bind */ | ||
} | ||
App = connect(() => ({}))(App) | ||
|
||
|
||
let A = () => (<h1>A</h1>) | ||
A = connect(() => ({ calls: ++mapStateToPropsCalls }))(A) | ||
|
||
|
||
const B = () => (<h1>B</h1>) | ||
|
||
|
||
class RouterMock extends React.Component { | ||
constructor(...args) { | ||
super(...args) | ||
this.state = { location: { pathname: 'a' } } | ||
this.setLocation = this.setLocation.bind(this) | ||
} | ||
|
||
setLocation(pathname) { | ||
this.setState({ location: { pathname } }) | ||
store.dispatch({ type: 'TEST' }) | ||
} | ||
|
||
getChildComponent(location) { | ||
switch (location) { | ||
case 'a': return <A /> | ||
case 'b': return <B /> | ||
default: throw new Error('Unknown location: ' + location) | ||
} | ||
} | ||
|
||
render() { | ||
return (<App setLocation={this.setLocation}> | ||
{this.getChildComponent(this.state.location.pathname)} | ||
</App>) | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From one of the react-router maintainers, this is a top quality mock router 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. Easy to extend as well for the next person who wants to hoist it and have it take a map of route to component. |
||
|
||
|
||
const div = document.createElement('div') | ||
document.body.appendChild(div) | ||
ReactDOM.render( | ||
(<ProviderMock store={store}> | ||
<RouterMock /> | ||
</ProviderMock>), | ||
div | ||
) | ||
|
||
const spy = expect.spyOn(console, 'error') | ||
|
||
linkA.click() | ||
linkB.click() | ||
linkB.click() | ||
|
||
spy.destroy() | ||
document.body.removeChild(div) | ||
expect(mapStateToPropsCalls).toBe(3) | ||
expect(spy.calls.length).toBe(0) | ||
}) | ||
|
||
it('should not attempt to set state when dispatching in componentWillUnmount', () => { | ||
const store = createStore(stringBuilder) | ||
let mapStateToPropsCalls = 0 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is a bit out of scope, but perhaps we can mention the whole
current
/next
setup below and why it's structured that way, now that all the code is in one spot right below this. It's not super-obvious why you would do that just reading through (do we have tests forSubscription
? That would be another way of documenting it.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The next/current pattern is straight out of the redux source. My hope is to eventually refactor that code into something reusable by the main store or Subscription.js
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know @acdlite wanted to do something like that in reduxjs/redux#1729, but it's gone inactive (although, @acdlite did just merge in some stuff on the upstream repo just a few hours ago...). Might be good to focus efforts there so we can use the exact same code in both places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. My thought was to wait until these changes have landed, take care of any outstanding issues and let the dust settle before further tinkering.