Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Commit aa5456d

Browse files
authored
Merge pull request #1115 from bugzpodder/master
Handle empty keys in redux state
2 parents d5758f4 + dab2f90 commit aa5456d

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/utils/get-form.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ export function getFormStateKey(state, model, s = defaultStrategy, currentPath =
1717
let result = null;
1818

1919
s.keys(state).some((key) => {
20+
if (key === '') {
21+
console.warn('react-redux-form skipped over an empty property key: %s', currentPath);
22+
return false;
23+
}
2024
const subState = s.get(state, key);
2125

2226
if (subState && s.get(subState, '$form')) {

test/utils-spec.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,64 @@ Object.keys(testContexts).forEach((testKey) => {
273273
'forms.array.simple');
274274
});
275275
});
276+
277+
context('state with empty key', () => {
278+
const emptyReducer = function () { return {}; };
279+
const store = createStore(combineReducers({
280+
'': emptyReducer,
281+
firstForm: formReducer('first'),
282+
deep: combineReducers({
283+
secondForm: formReducer('second', getInitialState({
284+
nested: { foo: 'bar' },
285+
})),
286+
deeper: combineReducers({
287+
thirdForm: formReducer('third'),
288+
}),
289+
}),
290+
}));
291+
292+
it('should find a shallow form reducer state key', () => {
293+
assert.equal(
294+
getFormStateKey(store.getState(), 'first'),
295+
'firstForm');
296+
});
297+
298+
it('should find a shallow form reducer state key with deep model', () => {
299+
assert.equal(
300+
getFormStateKey(store.getState(), 'first.anything'),
301+
'firstForm');
302+
});
303+
304+
it('should find a deep form reducer state key', () => {
305+
assert.equal(
306+
getFormStateKey(store.getState(), 'second'),
307+
'deep.secondForm');
308+
});
309+
310+
it('should find a deep form reducer state key with deep model', () => {
311+
assert.equal(
312+
getFormStateKey(store.getState(), 'second.anything'),
313+
'deep.secondForm');
314+
});
315+
316+
it('should find a deeper form reducer state key', () => {
317+
assert.equal(
318+
getFormStateKey(store.getState(), 'third'),
319+
'deep.deeper.thirdForm');
320+
});
321+
322+
it('should find a deeper form reducer state key with deep model', () => {
323+
assert.equal(
324+
getFormStateKey(store.getState(), 'third.anything'),
325+
'deep.deeper.thirdForm');
326+
});
327+
328+
it('should find a nested form reducer', () => {
329+
assert.equal(
330+
getFormStateKey(store.getState(), 'second.nested.foo'),
331+
'deep.secondForm.nested');
332+
});
333+
});
276334
});
277335

278336
describe('getField()', () => {

0 commit comments

Comments
 (0)