Skip to content

Commit 2c48641

Browse files
Brandon DailBrandon Dail
Brandon Dail
authored and
Brandon Dail
committed
Only warn for unexpected key once per key
1 parent 4b63737 commit 2c48641

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

src/combineReducers.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { ActionTypes } from './createStore'
22
import isPlainObject from 'lodash/isPlainObject'
33
import warning from './utils/warning'
44

5+
var unexpectedKeyCache = {}
6+
57
function getUndefinedStateErrorMessage(key, action) {
68
var actionType = action && action.type
79
var actionName = actionType && `"${actionType.toString()}"` || 'an action'
@@ -34,7 +36,14 @@ function getUnexpectedStateShapeWarningMessage(inputState, reducers, action) {
3436
)
3537
}
3638

37-
var unexpectedKeys = Object.keys(inputState).filter(key => !reducers.hasOwnProperty(key))
39+
var unexpectedKeys = Object.keys(inputState).filter(key =>
40+
!reducers.hasOwnProperty(key) &&
41+
!unexpectedKeyCache[key]
42+
)
43+
44+
unexpectedKeys.forEach(key => {
45+
unexpectedKeyCache[key] = true
46+
})
3847

3948
if (unexpectedKeys.length > 0) {
4049
return (

test/combineReducers.spec.js

+27-6
Original file line numberDiff line numberDiff line change
@@ -210,24 +210,24 @@ describe('Utils', () => {
210210
/Unexpected key "bar".*createStore.*instead: "foo", "baz"/
211211
)
212212

213-
createStore(reducer, { bar: 2, qux: 4 })
213+
createStore(reducer, { bar: 2, qux: 4, thud: 5 })
214214
expect(spy.calls[1].arguments[0]).toMatch(
215-
/Unexpected keys "bar", "qux".*createStore.*instead: "foo", "baz"/
215+
/Unexpected keys "qux", "thud".*createStore.*instead: "foo", "baz"/
216216
)
217217

218218
createStore(reducer, 1)
219219
expect(spy.calls[2].arguments[0]).toMatch(
220220
/createStore has unexpected type of "Number".*keys: "foo", "baz"/
221221
)
222222

223-
reducer({ bar: 2 })
223+
reducer({ corge: 2 })
224224
expect(spy.calls[3].arguments[0]).toMatch(
225-
/Unexpected key "bar".*reducer.*instead: "foo", "baz"/
225+
/Unexpected key "corge".*reducer.*instead: "foo", "baz"/
226226
)
227227

228-
reducer({ bar: 2, qux: 4 })
228+
reducer({ fred: 2, grault: 4 })
229229
expect(spy.calls[4].arguments[0]).toMatch(
230-
/Unexpected keys "bar", "qux".*reducer.*instead: "foo", "baz"/
230+
/Unexpected keys "fred", "grault".*reducer.*instead: "foo", "baz"/
231231
)
232232

233233
reducer(1)
@@ -237,5 +237,26 @@ describe('Utils', () => {
237237

238238
spy.restore()
239239
})
240+
241+
it('only warns for unexpected keys once', () => {
242+
const spy = expect.spyOn(console, 'error')
243+
const foo = (state = { foo: 1 }) => state
244+
const bar = (state = { bar: 2 }) => state
245+
246+
expect(spy.calls.length).toBe(0)
247+
const reducer = combineReducers({ foo, bar })
248+
const state = { foo: 1, bar: 2, unexpected: 3 }
249+
reducer(state, {})
250+
reducer(state, {})
251+
reducer(state, {})
252+
reducer(state, {})
253+
expect(spy.calls.length).toBe(1)
254+
reducer({ ...state, baz: 5 }, {})
255+
reducer({ ...state, baz: 5 }, {})
256+
reducer({ ...state, baz: 5 }, {})
257+
reducer({ ...state, baz: 5 }, {})
258+
expect(spy.calls.length).toBe(2)
259+
spy.restore()
260+
})
240261
})
241262
})

0 commit comments

Comments
 (0)