Skip to content
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

Merged
merged 7 commits into from
Mar 22, 2020
Merged

Fix location state (#394) #399

merged 7 commits into from
Mar 22, 2020

Conversation

BlazPocrnja
Copy link
Contributor

Closes #394. This change deep compares location state objects to prevent incorrectly pushing to history multiple times when state is changed.

  • Ran all tests

@@ -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'
Copy link
Owner

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.

@supasate
Copy link
Owner

Hi @BlazPocrnja , would you mind adding a unit test for this behavior?

@BlazPocrnja
Copy link
Contributor Author

BlazPocrnja commented Mar 16, 2020

@supasate I added unit tests and changed the lodash function to isEqualWith to allow for custom comparator functions as per this comment #394 (comment).

@@ -37,15 +38,15 @@ const createConnectedRouter = (structure) => {
search: searchInHistory,
hash: hashInHistory,
state: stateInHistory,
} = history.location
} = history.location
Copy link
Owner

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.

@@ -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,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto: spaces

Comment on lines 108 to 109
onLocationChanged: PropTypes.func.isRequired,
stateCompareFunction: PropTypes.func,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto: spaces

@@ -18,7 +19,7 @@ const createConnectedRouter = (structure) => {
constructor(props) {
super(props)

const { store, history, onLocationChanged } = props
const { store, history, onLocationChanged, stateCompareFunction} = props
Copy link
Owner

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.

Comment on lines 184 to 186
})

expect(props.history.entries).toHaveLength(3)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Comment on lines 169 to 171
})

expect(props.history.entries).toHaveLength(3)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto: spaces

Comment on lines 188 to 237
store.dispatch({
type: LOCATION_CHANGE,
payload: {
location: {
pathname: '/',
search: '',
hash: '',
state: { foo: 'baz' }
},
action: 'PUSH',
}
})

expect(props.history.entries).toHaveLength(4)
})
Copy link
Owner

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.

},
action: 'PUSH',
}
})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

return a === undefined || (a.foo === "baz" && b.foo === 'bar') ? true : false
}}
{...props}
>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

<Provider store={store}>
<ConnectedRouter
stateCompareFunction={(a, b) => {
return a === undefined || (a.foo === "baz" && b.foo === 'bar') ? true : false
Copy link
Owner

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.

Suggested change
return a === undefined || (a.foo === "baz" && b.foo === 'bar') ? true : false
return (a.foo === 'baz' && b.foo === 'bar') ? true : false

Copy link
Owner

@supasate supasate left a 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.

@BlazPocrnja
Copy link
Contributor Author

BlazPocrnja commented Mar 21, 2020

@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!

@supasate supasate merged commit 785db5c into supasate:master Mar 22, 2020
@supasate
Copy link
Owner

LGTM. Thanks for your contribution @BlazPocrnja !

@supasate
Copy link
Owner

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.

@BlazPocrnja
Copy link
Contributor Author

My apologies! Looks like it's passing now, so that's a relief. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

V6.7.0 breaks the location state flow
2 participants