Skip to content

Commit

Permalink
Don't create empty object if new initial value is an empty array and …
Browse files Browse the repository at this point in the history
…keepDirty is set (redux-form#3095)
  • Loading branch information
percevalw committed Jun 24, 2017
1 parent 994e46f commit 0e3fee0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
46 changes: 46 additions & 0 deletions src/__tests__/reducer.initialize.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,52 @@ const describeInitialize = (reducer, expect, { fromJS }) => () => {
})
})

it('should not create empty object if new initial value is an empty array and keepDirty is set', () => {
const before = {
myForm: {
registeredFields: {
myList: { name: 'myList', type: 'Field', count: 0 },
'myList.0.name': { name: 'myList.0.name', type: 'Field', count: 0 }
},
values: {
myList: []
},
initial: {
myList: [{ name: '' }]
}
}
}

const actionInitialValues = {
myList: []
}
const actionKeepDirty = true

const state = reducer(
fromJS(before),
initialize('myForm', actionInitialValues, actionKeepDirty)
)

expect(state).toEqualMap(
{
myForm: {
registeredFields: {
myList: { name: 'myList', type: 'Field', count: 0 },
'myList.0.name': { name: 'myList.0.name', type: 'Field', count: 0 }
},
values: {
myList: []
},
initial: {
myList: []
}
}
},
null,
2
)
})

it('should add new pristine values at the root level', () => {
const newInitial = {
oldField: 'oldValue',
Expand Down
8 changes: 7 additions & 1 deletion src/createReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,13 @@ const createReducer = structure => {
if (deepEqual(previousValue, previousInitialValue)) {
// Overwrite the old pristine value with the new pristine value
const newInitialValue = getIn(newInitialValues, name)
newValues = setIn(newValues, name, newInitialValue)

// This check prevents any 'setIn' call that would create useless
// nested objects, since the path to the new field value would
// evaluate to the same (especially for undefined values)
if (getIn(newValues, name) !== newInitialValue) {
newValues = setIn(newValues, name, newInitialValue)
}
}
})

Expand Down

0 comments on commit 0e3fee0

Please sign in to comment.