Skip to content

Commit

Permalink
fix: forward move and swap array control actions to children in form …
Browse files Browse the repository at this point in the history
…group reducer
  • Loading branch information
MrWolfZ committed Mar 13, 2021
1 parent f743b8b commit f682561
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/group/reducer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
MarkAsTouchedAction,
MarkAsUnsubmittedAction,
MarkAsUntouchedAction,
MoveArrayControlAction,
RemoveArrayControlAction,
RemoveGroupControlAction,
ResetAction,
Expand All @@ -19,6 +20,7 @@ import {
SetUserDefinedPropertyAction,
SetValueAction,
StartAsyncValidationAction,
SwapArrayControlAction,
UnfocusAction,
} from '../actions';
import { createFormGroupState } from '../state';
Expand Down Expand Up @@ -76,6 +78,22 @@ describe('form group reducer', () => {
expect(resultState.controls.inner5!.controls[0]).toBeUndefined();
});

it(`should forward add ${MoveArrayControlAction.name}s to children`, () => {
const value: FormGroupValue = { inner: '', inner2: '', inner3: { inner4: '' }, inner5: ['a', 'b', 'c'] };
const state = createFormGroupState(FORM_CONTROL_ID, value);
const resultState = formGroupReducer<FormGroupValue>(state, new MoveArrayControlAction(FORM_CONTROL_INNER5_ID, 0, 1));
expect(resultState.controls.inner5!.controls[0].value).toBe('b');
expect(resultState.controls.inner5!.controls[1].value).toBe('a');
});

it(`should forward remove ${SwapArrayControlAction.name}s to children`, () => {
const value: FormGroupValue = { inner: '', inner2: '', inner3: { inner4: '' }, inner5: ['a', 'b', 'c'] };
const state = createFormGroupState(FORM_CONTROL_ID, value);
const resultState = formGroupReducer(state, new SwapArrayControlAction(FORM_CONTROL_INNER5_ID, 0, 1));
expect(resultState.controls.inner5!.controls[0].value).toBe('b');
expect(resultState.controls.inner5!.controls[1].value).toBe('a');
});

it('should not update state if no child was updated', () => {
const resultState = formGroupReducer(INITIAL_STATE, new SetValueAction(FORM_CONTROL_INNER_ID, '') as any);
expect(resultState).toBe(INITIAL_STATE);
Expand Down
4 changes: 4 additions & 0 deletions src/group/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
AddArrayControlAction,
FocusAction,
isNgrxFormsAction,
MoveArrayControlAction,
RemoveArrayControlAction,
SwapArrayControlAction,
UnfocusAction,
} from '../actions';
import { FormGroupState, isGroupState, KeyValue } from '../state';
Expand Down Expand Up @@ -46,6 +48,8 @@ export function formGroupReducerInternal<TValue extends KeyValue>(state: FormGro
case UnfocusAction.TYPE:
case AddArrayControlAction.TYPE:
case RemoveArrayControlAction.TYPE:
case MoveArrayControlAction.TYPE:
case SwapArrayControlAction.TYPE:
return childReducer(state, action);

default:
Expand Down

0 comments on commit f682561

Please sign in to comment.