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

Commit

Permalink
Preventing load intent on reset if no value was loaded to field. Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkpiano committed Dec 10, 2016
1 parent b8bc7ee commit a688548
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 42 deletions.
22 changes: 12 additions & 10 deletions src/reducers/form-actions-reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,28 @@ import i from 'icepick';
const resetFieldState = (field, key) => {
if (!isPlainObject(field)) return field;

const intents = [{ type: 'validate' }];
let resetValue = field.initialValue;

if ('loadedValue' in field && field.initialValue !== field.loadedValue) {
intents.push({ type: 'load', value: field.loadedValue });
resetValue = field.loadedValue;
}

if (key === '$form') {
return i.assign(initialFieldState, {
value: field.initialValue,
value: resetValue,
model: field.model,
intents: [
{ type: 'validate' },
{ type: 'load', value: field.initialValue },
],
intents,
});
}

if (field.$form) return mapValues(field, resetFieldState);

return i.assign(initialFieldState, {
value: field.initialValue,
value: resetValue,
model: field.model,
intents: [
{ type: 'validate' },
{ type: 'load', value: field.initialValue },
],
intents,
});
};

Expand Down
16 changes: 10 additions & 6 deletions src/reducers/form/change-action-reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ function updateFieldValue(field, action, parentModel = undefined) {
pristine: silent
? fieldState.pristine
: false,
initialValue: load
loadedValue: load
? value
: fieldState.initialValue,
: fieldState.loadedValue,
};

if (shallowEqual(field.value, value)) {
Expand Down Expand Up @@ -95,9 +95,9 @@ function updateFieldValue(field, action, parentModel = undefined) {

return i.merge(subField, i.assign(changedFieldProps, {
value: subValue,
initialValue: load
loadedValue: load
? subValue
: subField.initialValue,
: subField.loadedValue,
}));
});

Expand All @@ -111,7 +111,11 @@ function updateFieldValue(field, action, parentModel = undefined) {
}

function getFormValue(form) {
if (form && !form.$form) return form.initialValue;
if (form && !form.$form) {
return typeof form.loadedValue !== 'undefined'
? form.loadedValue
: form.initialValue;
}

const result = mapValues(form, (field, key) => {
if (key === '$form') return undefined;
Expand Down Expand Up @@ -141,7 +145,7 @@ export default function changeActionReducer(state, action, localPath) {

return {
value: formValue,
initialValue: formValue,
loadedValue: formValue,
};
});
}
Expand Down
3 changes: 1 addition & 2 deletions test/control-component-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -927,9 +927,8 @@ Object.keys(testContexts).forEach((testKey) => {

describe('initial value after reset', () => {
const initialState = getInitialState({ foo: '' });
const reducer = formReducer('test');
const store = testCreateStore({
testForm: reducer,
testForm: formReducer('test', initialState),
test: modelReducer('test', initialState),
});

Expand Down
65 changes: 41 additions & 24 deletions test/form-reducer-actions-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('formReducer() (V1)', () => {
expectedField: {
pristine: true,
value: 'string',
initialValue: 'string',
loadedValue: 'string',
},
expectedForm: {
pristine: true,
Expand All @@ -75,7 +75,7 @@ describe('formReducer() (V1)', () => {
expectedField: {
pristine: true,
value: 42,
initialValue: 42,
loadedValue: 42,
},
expectedForm: {
pristine: true,
Expand All @@ -88,7 +88,7 @@ describe('formReducer() (V1)', () => {
$form: {
pristine: true,
value: { foo: 'bar' },
initialValue: { foo: 'bar' },
loadedValue: { foo: 'bar' },
},
foo: {
pristine: true,
Expand Down Expand Up @@ -660,24 +660,41 @@ describe('formReducer() (V1)', () => {
});

describe('deep resetting', () => {
const reducer = formReducer('test', {
foo: '',
meta: {
bar: '',
},
});

const changedState = reducer(undefined, actions.change('test', {
foo: 'changed foo',
meta: { bar: 'changed bar' },
}));

it('resetting a parent field should reset child fields in form', () => {
const reducer = formReducer('test', {
foo: '',
meta: {
bar: '',
},
});

const changedState = reducer(undefined, actions.change('test', {
foo: 'changed foo',
meta: { bar: 'changed bar' },
}));
const resetState = reducer(changedState, actions.reset('test'));

assert.equal(resetState.foo.value, '');
assert.equal(resetState.meta.bar.value, '');
});

it('resetting a parent field should reset complex child fields', () => {
const reducer = formReducer('test', {
foo: [],
meta: {
bar: [],
},
});

const changedState = reducer(undefined, actions.change('test', {
foo: [1, 2, 3],
meta: { bar: [1, 2, 3] },
}));
const resetState = reducer(changedState, actions.reset('test'));

assert.deepEqual(resetState.foo.$form.value, []);
assert.deepEqual(resetState.meta.bar.$form.value, []);
});
});

describe('resetting after load', () => {
Expand All @@ -686,23 +703,23 @@ describe('formReducer() (V1)', () => {
});

const loadedState = reducer(undefined,
actions.load('test.foo', 'new initial'));
actions.load('test.foo', 'new loaded'));

it('should change the initial value for the field', () => {
assert.equal(loadedState.foo.initialValue, 'new initial');
assert.equal(loadedState.foo.value, 'new initial');
it('should change the loaded value for the field', () => {
assert.equal(loadedState.foo.loadedValue, 'new loaded');
assert.equal(loadedState.foo.value, 'new loaded');
});

it('should change the initial value for the form', () => {
assert.deepEqual(loadedState.$form.initialValue, { foo: 'new initial' });
assert.deepEqual(loadedState.$form.value, { foo: 'new initial' });
it('should change the loaded value for the form', () => {
assert.deepEqual(loadedState.$form.loadedValue, { foo: 'new loaded' });
assert.deepEqual(loadedState.$form.value, { foo: 'new loaded' });
});

it('resetting a parent field should reset child fields in form', () => {
const resetState = reducer(loadedState, actions.reset('test'));

assert.deepEqual(resetState.$form.value, { foo: 'new initial' });
assert.equal(resetState.foo.value, 'new initial');
assert.deepEqual(resetState.$form.value, { foo: 'new loaded' });
assert.equal(resetState.foo.value, 'new loaded');
});
});

Expand Down
21 changes: 21 additions & 0 deletions test/model-reducer-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,25 @@ describe('modelReducer()', () => {
{},
'should only change when base path is equal');
});

it('should reset a model to its initial state', () => {
const initialState = {
simple: 123,
foo: [],
meta: { bar: [] },
};

const reducer = modelReducer('test', initialState);

const changedState = reducer(undefined, actions.change('test', {
simple: 999,
foo: [1, 2, 3],
meta: { bar: [1, 2, 3], baz: 'new field' },
}));

assert.deepEqual(
reducer(changedState, actions.reset('test')),
initialState
);
});
});

0 comments on commit a688548

Please sign in to comment.