Skip to content

Commit 9c9a4d2

Browse files
anubhavguptaguptatimdorr
committed
Fixed combineReducers changeDetection logic(#3488) (#3490)
* Fixed combineReducers changeDetection logic(#3488) * Cleaning up these and some other tests while I'm here Co-authored-by: gupta <guptanub@amazon.com> Co-authored-by: Tim Dorr <timdorr@users.noreply.github.com>
1 parent 0ac73b5 commit 9c9a4d2

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/combineReducers.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ export default function combineReducers(reducers) {
173173
nextState[key] = nextStateForKey
174174
hasChanged = hasChanged || nextStateForKey !== previousStateForKey
175175
}
176+
hasChanged =
177+
hasChanged || finalReducerKeys.length !== Object.keys(state).length
176178
return hasChanged ? nextState : state
177179
}
178180
}

test/combineReducers.spec.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ describe('Utils', () => {
192192
expect(spy.mock.calls[0][0]).toMatch(
193193
/Store does not have a valid reducer/
194194
)
195+
195196
spy.mockClear()
196197
console.error = preSpy
197198
})
@@ -265,13 +266,16 @@ describe('Utils', () => {
265266
const bar = (state = { bar: 2 }) => state
266267

267268
expect(spy.mock.calls.length).toBe(0)
269+
268270
const reducer = combineReducers({ foo, bar })
269271
const state = { foo: 1, bar: 2, qux: 3 }
272+
270273
reducer(state, {})
271274
reducer(state, {})
272275
reducer(state, {})
273276
reducer(state, {})
274277
expect(spy.mock.calls.length).toBe(1)
278+
275279
reducer({ ...state, baz: 5 }, {})
276280
reducer({ ...state, baz: 5 }, {})
277281
reducer({ ...state, baz: 5 }, {})
@@ -281,5 +285,72 @@ describe('Utils', () => {
281285
spy.mockClear()
282286
console.error = preSpy
283287
})
288+
289+
describe('With Replace Reducers', function() {
290+
const foo = (state = {}) => state
291+
const bar = (state = {}) => state
292+
const ACTION = { type: 'ACTION' }
293+
294+
it('should return an updated state when additional reducers are passed to combineReducers', function() {
295+
const originalCompositeReducer = combineReducers({ foo })
296+
const store = createStore(originalCompositeReducer)
297+
298+
store.dispatch(ACTION)
299+
300+
const initialState = store.getState()
301+
302+
store.replaceReducer(combineReducers({ foo, bar }))
303+
store.dispatch(ACTION)
304+
305+
const nextState = store.getState()
306+
expect(nextState).not.toBe(initialState)
307+
})
308+
309+
it('should return an updated state when reducers passed to combineReducers are changed', function() {
310+
const baz = (state = {}) => state
311+
312+
const originalCompositeReducer = combineReducers({ foo, bar })
313+
const store = createStore(originalCompositeReducer)
314+
315+
store.dispatch(ACTION)
316+
317+
const initialState = store.getState()
318+
319+
store.replaceReducer(combineReducers({ baz, bar }))
320+
store.dispatch(ACTION)
321+
322+
const nextState = store.getState()
323+
expect(nextState).not.toBe(initialState)
324+
})
325+
326+
it('should return the same state when reducers passed to combineReducers not changed', function() {
327+
const originalCompositeReducer = combineReducers({ foo, bar })
328+
const store = createStore(originalCompositeReducer)
329+
330+
store.dispatch(ACTION)
331+
332+
const initialState = store.getState()
333+
334+
store.replaceReducer(combineReducers({ foo, bar }))
335+
store.dispatch(ACTION)
336+
337+
const nextState = store.getState()
338+
expect(nextState).toBe(initialState)
339+
})
340+
341+
it('should return an updated state when one of more reducers passed to the combineReducers are removed', function() {
342+
const originalCompositeReducer = combineReducers({ foo, bar })
343+
const store = createStore(originalCompositeReducer)
344+
345+
store.dispatch(ACTION)
346+
347+
const initialState = store.getState()
348+
349+
store.replaceReducer(combineReducers({ bar }))
350+
351+
const nextState = store.getState()
352+
expect(nextState).not.toBe(initialState)
353+
})
354+
})
284355
})
285356
})

0 commit comments

Comments
 (0)