Skip to content

Commit

Permalink
fix: fix updateGroup throwing an error if an empty update object wa…
Browse files Browse the repository at this point in the history
…s provided in curried as well as uncurried version
  • Loading branch information
MrWolfZ committed Jan 30, 2018
1 parent 3a59937 commit bee4d54
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#### Bugfixes

* fix missing union case in typing of `updateArray` update function that causes a compile error if used inside an `updateGroup` ([fa7dccc](https://github.com/MrWolfZ/ngrx-forms/commit/fa7dccc))
* fix `updateGroup` throwing an error if an empty update object was provided in curried as well as uncurried version

<a name="2.1.2"></a>
### 2.1.2
Expand Down
44 changes: 43 additions & 1 deletion src/state.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cast, createFormArrayState, createFormControlState, createFormGroupState } from './state';
import { cast, createFormArrayState, createFormControlState, createFormGroupState, isGroupState, isArrayState } from './state';

describe('state', () => {
const FORM_CONTROL_ID = 'test ID';
Expand Down Expand Up @@ -213,4 +213,46 @@ describe('state', () => {
expect(initialState.controls).toEqual([]);
});
});

describe(isArrayState.name, () => {
it('should return true for array state', () => {
const INITIAL_STATE = createFormArrayState<string>(FORM_CONTROL_ID, ['abc']);
expect(isArrayState(INITIAL_STATE)).toBe(true);
});

it('should return false for group state', () => {
const INITIAL_STATE = createFormGroupState<any>(FORM_CONTROL_ID, { control: 'abc' });
expect(isArrayState(INITIAL_STATE)).toBe(false);
});

it('should return false for control state', () => {
const INITIAL_STATE = createFormControlState<string>(FORM_CONTROL_ID, 'abc');
expect(isArrayState(INITIAL_STATE)).toBe(false);
});

it('should return false for update object', () => {
expect(isArrayState({ controls: () => void 0 } as any)).toBe(false);
});
});

describe(isGroupState.name, () => {
it('should return true for group state', () => {
const INITIAL_STATE = createFormGroupState<any>(FORM_CONTROL_ID, { control: 'abc' });
expect(isGroupState(INITIAL_STATE)).toBe(true);
});

it('should return false for control state', () => {
const INITIAL_STATE = createFormControlState<string>(FORM_CONTROL_ID, 'abc');
expect(isGroupState(INITIAL_STATE)).toBe(false);
});

it('should return false for array state', () => {
const INITIAL_STATE = createFormArrayState<string>(FORM_CONTROL_ID, ['abc']);
expect(isGroupState(INITIAL_STATE)).toBe(false);
});

it('should return false for update object', () => {
expect(isGroupState({ controls: () => void 0 } as any)).toBe(false);
});
});
});
6 changes: 4 additions & 2 deletions src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,14 +499,16 @@ export interface FormArrayState<TValue> extends AbstractControlState<TValue[]> {
* This function determines if a form state is an array state.
*/
export function isArrayState(state: AbstractControlState<any>): state is FormArrayState<any> {
return state.hasOwnProperty('controls') && Array.isArray((state as any).controls);
const controls = (state as any).controls;
return state.hasOwnProperty('controls') && Array.isArray(controls) && typeof controls !== 'function';
}

/**
* This function determines if a form state is a group state.
*/
export function isGroupState(state: AbstractControlState<any>): state is FormGroupState<any> {
return state.hasOwnProperty('controls') && !Array.isArray((state as any).controls);
const controls = (state as any).controls;
return state.hasOwnProperty('controls') && !Array.isArray(controls) && typeof controls !== 'function';
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/update-function/update-group.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ describe(updateGroup.name, () => {
expect(resultState.controls.inner5).toBe(expected);
});

it('should not change the state with empty update object', () => {
const resultState = updateGroup<FormGroupValue>({})(INITIAL_STATE);
expect(resultState).toBe(resultState);
});

it('should apply the provided functions to control children uncurried', () => {
const expected = { ...INITIAL_STATE.controls.inner, value: 'A' };
const resultState = updateGroup<FormGroupValue>(
Expand Down Expand Up @@ -61,6 +66,11 @@ describe(updateGroup.name, () => {
expect(resultState.controls.inner5).toBe(expected);
});

it('should not change the state with empty update object uncurried', () => {
const resultState = updateGroup<FormGroupValue>(INITIAL_STATE, {});
expect(resultState).toBe(resultState);
});

it('should apply multiple provided function objects one after another', () => {
const updatedInner1 = { ...INITIAL_STATE.controls.inner, value: 'A' };
const expectedInner1 = { ...INITIAL_STATE.controls.inner, value: 'B' };
Expand Down
5 changes: 2 additions & 3 deletions src/update-function/update-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Action } from '@ngrx/store';

import { formGroupReducer } from '../group/reducer';
import { computeGroupState } from '../group/reducer/util';
import { AbstractControlState, FormGroupControls, FormGroupState, KeyValue } from '../state';
import { AbstractControlState, FormGroupControls, FormGroupState, KeyValue, isGroupState } from '../state';
import { ProjectFn2 } from './util';

export type StateUpdateFns<TValue extends KeyValue> =
Expand Down Expand Up @@ -108,8 +108,7 @@ export function updateGroup<TValue extends KeyValue>(
stateOrFunction: FormGroupState<TValue> | StateUpdateFns<TValue>,
...updateFnsArr: Array<StateUpdateFns<TValue>>,
) {
const isUpdateFunctionObject = Object.keys(stateOrFunction).some(key => typeof (stateOrFunction as any)[key] === 'function');
if (!isUpdateFunctionObject) {
if (isGroupState(stateOrFunction as any)) {
const [first, ...rest] = updateFnsArr;
return updateGroup(first, ...rest)(stateOrFunction as any);
}
Expand Down

0 comments on commit bee4d54

Please sign in to comment.