Skip to content

Commit

Permalink
Do not override query if one already exists (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
dr3 authored Mar 14, 2020
1 parent 63598b0 commit 66eda0c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { LOCATION_CHANGE } from './actions'
* Utilises the search prop of location to construct query.
*/
const injectQuery = (location) => {
if (location && location.query) {
// Don't inject query if it already exists in history
return location
}

const searchQuery = location && location.search

if (typeof searchQuery !== 'string' || searchQuery.length === 0) {
Expand Down
43 changes: 43 additions & 0 deletions test/reducer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,49 @@ describe('connectRouter', () => {
const nextState = rootReducer(currentState, action)
expect(nextState).toBe(currentState)
})

it('does not replace query if it already exists in location', () => {
const mockReducer = (state = {}, action) => {
switch (action.type) {
default:
return state
}
}
const rootReducer = combineReducers({
mock: mockReducer,
router: connectRouter(mockHistory)
})

const currentState = {
mock: {},
router: {
location: {
pathname: '/',
search: '',
hash: ''
},
action: 'POP'
}
}
const action = {
type: LOCATION_CHANGE,
payload: {
location: {
pathname: '/path/to/somewhere',
search: '?query=%7Bvalue%3A%20%27foobar%27%7D',
hash: '',
query: { query: { value: 'foobar' } }
},
action: 'PUSH'
}
}
const nextState = rootReducer(currentState, action)
const expectedState = {
mock: {},
router: action.payload
}
expect(nextState).toEqual(expectedState)
})
})

describe('with immutable structure', () => {
Expand Down

0 comments on commit 66eda0c

Please sign in to comment.