Skip to content

Commit 9907718

Browse files
committed
fix: make wrapReducerWithFormStateUpdate work properly if used on states that are form states themselves
1 parent 590a7b0 commit 9907718

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
## ngrx-forms Changelog
22

3+
<a name="6.3.3"></a>
4+
### 6.3.3
5+
6+
#### Bugfixes
7+
8+
* make `wrapReducerWithFormStateUpdate` work properly if used on states that are form states themselves, closes [#196](https://github.com/MrWolfZ/ngrx-forms/issues/196)
9+
310
<a name="6.3.2"></a>
411
### 6.3.2
512

src/reducer.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { Action, createReducer } from '@ngrx/store';
22
import { MarkAsDirtyAction, MarkAsTouchedAction, SetValueAction } from './actions';
3+
import { formArrayReducer } from './array/reducer';
4+
import { formControlReducer } from './control/reducer';
5+
import { formGroupReducer } from './group/reducer';
36
import { createFormStateReducerWithUpdate, formStateReducer, onNgrxForms, onNgrxFormsAction, wrapReducerWithFormStateUpdate } from './reducer';
7+
import { FormArrayState, FormControlState, FormGroupState } from './state';
48
import { FORM_CONTROL_ID, FORM_CONTROL_INNER5_ID, FORM_CONTROL_INNER_ID, FormGroupValue, INITIAL_STATE } from './update-function/test-util';
59
import { updateGroup } from './update-function/update-group';
610

@@ -294,6 +298,20 @@ describe(wrapReducerWithFormStateUpdate.name, () => {
294298
expect(resultState.control).not.toBe(INITIAL_STATE.controls.inner);
295299
});
296300

301+
it('should update a non-nested control after the reducer', () => {
302+
const wrappedReducer = wrapReducerWithFormStateUpdate<FormControlState<string>, FormControlState<string>>(
303+
formControlReducer,
304+
s => s,
305+
s => {
306+
expect(s).toBe(INITIAL_STATE.controls.inner);
307+
return ({ ...s });
308+
},
309+
);
310+
311+
const resultState = wrappedReducer(initialState.control, { type: '' });
312+
expect(resultState).not.toBe(INITIAL_STATE.controls.inner);
313+
});
314+
297315
it('should update a group after the reducer', () => {
298316
const wrappedReducer = wrapReducerWithFormStateUpdate(reducer, s => s.group, s => {
299317
expect(s).toBe(INITIAL_STATE);
@@ -304,6 +322,20 @@ describe(wrapReducerWithFormStateUpdate.name, () => {
304322
expect(resultState.group).not.toBe(INITIAL_STATE);
305323
});
306324

325+
it('should update a non-nested group after the reducer', () => {
326+
const wrappedReducer = wrapReducerWithFormStateUpdate<FormGroupState<FormGroupValue>, FormGroupState<FormGroupValue>>(
327+
formGroupReducer,
328+
s => s,
329+
s => {
330+
expect(s).toBe(INITIAL_STATE);
331+
return ({ ...s });
332+
},
333+
);
334+
335+
const resultState = wrappedReducer(initialState.group, { type: '' });
336+
expect(resultState).not.toBe(INITIAL_STATE);
337+
});
338+
307339
it('should update an array after the reducer', () => {
308340
const wrappedReducer = wrapReducerWithFormStateUpdate(reducer, s => s.array, s => {
309341
expect(s).toBe(INITIAL_STATE.controls.inner5);
@@ -314,6 +346,20 @@ describe(wrapReducerWithFormStateUpdate.name, () => {
314346
expect(resultState.array).not.toBe(INITIAL_STATE.controls.inner5);
315347
});
316348

349+
it('should update a non-nested array after the reducer', () => {
350+
const wrappedReducer = wrapReducerWithFormStateUpdate<FormArrayState<string>, FormArrayState<string>>(
351+
formArrayReducer,
352+
s => s,
353+
s => {
354+
expect(s).toBe(INITIAL_STATE.controls.inner5);
355+
return ({ ...s });
356+
},
357+
);
358+
359+
const resultState = wrappedReducer(initialState.array, { type: '' });
360+
expect(resultState).not.toBe(INITIAL_STATE.controls.inner5);
361+
});
362+
317363
it('should set the updated form state', () => {
318364
const updatedControl = { ...INITIAL_STATE.controls.inner };
319365
const wrappedReducer = wrapReducerWithFormStateUpdate(reducer, s => s.control, () => updatedControl);

src/reducer.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ export function wrapReducerWithFormStateUpdate<TState, TFormState extends Abstra
150150
const updatedState = reducer(state, action);
151151

152152
const formState = formStateLocator(updatedState);
153+
154+
// if the state itself is the form state, update it directly
155+
if (formState === updatedState as unknown) {
156+
return updateFn(formState, updatedState) as unknown as TState;
157+
}
158+
153159
const formStateKey = Object.keys(updatedState).find(key => updatedState[key as keyof TState] as any === formState)!;
154160

155161
const updatedFormState = updateFn(formState, updatedState);

0 commit comments

Comments
 (0)