-
Notifications
You must be signed in to change notification settings - Fork 594
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
Fix location state (#394) #399
Conversation
src/ConnectedRouter.js
Outdated
@@ -2,6 +2,7 @@ import React, { PureComponent } from 'react' | |||
import PropTypes from 'prop-types' | |||
import { connect, ReactReduxContext } from 'react-redux' | |||
import { Router } from 'react-router' | |||
import { isEqual } from 'lodash' |
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.
import isEqual from 'lodash.isequal'
to avoid loading the whole lodash module.
Please run yarn add -P lodash.isequal
to update package.json and yarn.lock as peerDependencies.
Hi @BlazPocrnja , would you mind adding a unit test for this behavior? |
@supasate I added unit tests and changed the lodash function to isEqualWith to allow for custom comparator functions as per this comment #394 (comment). |
src/ConnectedRouter.js
Outdated
@@ -37,15 +38,15 @@ const createConnectedRouter = (structure) => { | |||
search: searchInHistory, | |||
hash: hashInHistory, | |||
state: stateInHistory, | |||
} = history.location | |||
} = history.location |
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.
nit: use spaces, not tabs.
src/ConnectedRouter.js
Outdated
@@ -104,7 +105,8 @@ const createConnectedRouter = (structure) => { | |||
}).isRequired, | |||
basename: PropTypes.string, | |||
children: PropTypes.oneOfType([ PropTypes.func, PropTypes.node ]), | |||
onLocationChanged: PropTypes.func.isRequired, | |||
onLocationChanged: PropTypes.func.isRequired, |
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.
ditto: spaces
src/ConnectedRouter.js
Outdated
onLocationChanged: PropTypes.func.isRequired, | ||
stateCompareFunction: PropTypes.func, |
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.
ditto: spaces
src/ConnectedRouter.js
Outdated
@@ -18,7 +19,7 @@ const createConnectedRouter = (structure) => { | |||
constructor(props) { | |||
super(props) | |||
|
|||
const { store, history, onLocationChanged } = props | |||
const { store, history, onLocationChanged, stateCompareFunction} = props |
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.
nit: add a space before the closing curling brace.
test/ConnectedRouter.test.js
Outdated
}) | ||
|
||
expect(props.history.entries).toHaveLength(3) |
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.
ditto
test/ConnectedRouter.test.js
Outdated
}) | ||
|
||
expect(props.history.entries).toHaveLength(3) |
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.
ditto: spaces
test/ConnectedRouter.test.js
Outdated
store.dispatch({ | ||
type: LOCATION_CHANGE, | ||
payload: { | ||
location: { | ||
pathname: '/', | ||
search: '', | ||
hash: '', | ||
state: { foo: 'baz' } | ||
}, | ||
action: 'PUSH', | ||
} | ||
}) | ||
|
||
expect(props.history.entries).toHaveLength(4) | ||
}) |
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.
Can you split this into another test? So, we have one for testing that it doesn't update if the location state structure doesn't change, and another one for testing that it updates if the location state structure changes.
test/ConnectedRouter.test.js
Outdated
}, | ||
action: 'PUSH', | ||
} | ||
}) |
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.
ditto
test/ConnectedRouter.test.js
Outdated
return a === undefined || (a.foo === "baz" && b.foo === 'bar') ? true : false | ||
}} | ||
{...props} | ||
> |
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.
ditto
test/ConnectedRouter.test.js
Outdated
<Provider store={store}> | ||
<ConnectedRouter | ||
stateCompareFunction={(a, b) => { | ||
return a === undefined || (a.foo === "baz" && b.foo === 'bar') ? true : false |
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.
No need to add a === undefined
which we doesn't test it. Only the second comparison is enough to test this comparison feature.
return a === undefined || (a.foo === "baz" && b.foo === 'bar') ? true : false | |
return (a.foo === 'baz' && b.foo === 'bar') ? true : false |
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.
Looks good to me. Please rebase and fix some nits.
…ion test + spacing
@supasate Sorry for the delay! I made the changes you requested. Except when testing the custom state comparator function, there needs to be a check when the states are undefined (which is the case when you first push to history, or the store). I rewrote the test to make it a bit clearer. It's rebased now too! |
LGTM. Thanks for your contribution @BlazPocrnja ! |
I just noticed that it breaks the test because lodash.isequalwith is not listed in devDependencies. (https://travis-ci.org/github/supasate/connected-react-router/builds/665324580?utm_medium=notification&utm_source=github_status). I'm not sure why Travis didn't show up here. I'm fixing in #408. |
My apologies! Looks like it's passing now, so that's a relief. Thanks! |
Closes #394. This change deep compares location state objects to prevent incorrectly pushing to history multiple times when state is changed.