Skip to content

Commit

Permalink
Fixed combineReducers changeDetection logic(reduxjs#3488)
Browse files Browse the repository at this point in the history
  • Loading branch information
gupta committed Jul 31, 2019
1 parent 59cc278 commit 001a197
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/combineReducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ export default function combineReducers(reducers) {
nextState[key] = nextStateForKey
hasChanged = hasChanged || nextStateForKey !== previousStateForKey
}
hasChanged =
hasChanged || finalReducerKeys.length !== Object.keys(state).length
return hasChanged ? nextState : state
}
}
51 changes: 51 additions & 0 deletions test/combineReducers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,5 +281,56 @@ describe('Utils', () => {
spy.mockClear()
console.error = preSpy
})

describe('With Replace Reducers', function() {
const foo = (state = {}) => state
const bar = (state = {}) => state
const ACTION = { type: 'ACTION' }

it('should return an updated state when additional reducers are passed to combineReducers', function() {
const originalCompositeReducer = combineReducers({ foo })
const store = createStore(originalCompositeReducer)
store.dispatch(ACTION)
const initialState = store.getState()
store.replaceReducer(combineReducers({ foo, bar }))
store.dispatch(ACTION)
const nextState = store.getState()
expect(nextState).not.toBe(initialState)
})

it('should return an updated state when reducers passed to combineReducers are changed', function() {
const baz = (state = {}) => state

const originalCompositeReducer = combineReducers({ foo, bar })
const store = createStore(originalCompositeReducer)
store.dispatch(ACTION)
const initialState = store.getState()
store.replaceReducer(combineReducers({ baz, bar }))
store.dispatch(ACTION)
const nextState = store.getState()
expect(nextState).not.toBe(initialState)
})

it('should return the same state when reducers passed to combineReducers not changed', function() {
const originalCompositeReducer = combineReducers({ foo, bar })
const store = createStore(originalCompositeReducer)
store.dispatch(ACTION)
const initialState = store.getState()
store.replaceReducer(combineReducers({ foo, bar }))
store.dispatch(ACTION)
const nextState = store.getState()
expect(nextState).toBe(initialState)
})

it('should return an updated state when one of more reducers passed to the combineReducers are removed', function() {
const originalCompositeReducer = combineReducers({ foo, bar })
const store = createStore(originalCompositeReducer)
store.dispatch(ACTION)
const initialState = store.getState()
store.replaceReducer(combineReducers({ bar }))
const nextState = store.getState()
expect(nextState).not.toBe(initialState)
})
})
})
})

0 comments on commit 001a197

Please sign in to comment.