Skip to content

Commit

Permalink
Fixed combineReducers changeDetection logic(#3488) (#3490)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people committed Aug 12, 2019
1 parent 0ac73b5 commit 9c9a4d2
Show file tree
Hide file tree
Showing 2 changed files with 73 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
}
}
71 changes: 71 additions & 0 deletions test/combineReducers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ describe('Utils', () => {
expect(spy.mock.calls[0][0]).toMatch(
/Store does not have a valid reducer/
)

spy.mockClear()
console.error = preSpy
})
Expand Down Expand Up @@ -265,13 +266,16 @@ describe('Utils', () => {
const bar = (state = { bar: 2 }) => state

expect(spy.mock.calls.length).toBe(0)

const reducer = combineReducers({ foo, bar })
const state = { foo: 1, bar: 2, qux: 3 }

reducer(state, {})
reducer(state, {})
reducer(state, {})
reducer(state, {})
expect(spy.mock.calls.length).toBe(1)

reducer({ ...state, baz: 5 }, {})
reducer({ ...state, baz: 5 }, {})
reducer({ ...state, baz: 5 }, {})
Expand All @@ -281,5 +285,72 @@ 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 9c9a4d2

Please sign in to comment.