From 66eda0c80242830ecbd12cb6848813a82c523481 Mon Sep 17 00:00:00 2001 From: Drew McMillan Date: Sat, 14 Mar 2020 17:22:45 +0000 Subject: [PATCH] Do not override query if one already exists (#396) --- src/reducer.js | 5 +++++ test/reducer.test.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/reducer.js b/src/reducer.js index f769783c..a9ea9c58 100644 --- a/src/reducer.js +++ b/src/reducer.js @@ -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) { diff --git a/test/reducer.test.js b/test/reducer.test.js index 650271d4..502aae3e 100644 --- a/test/reducer.test.js +++ b/test/reducer.test.js @@ -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', () => {