|
| 1 | +import { Action, createReducer } from '@ngrx/store'; |
1 | 2 | import { MarkAsTouchedAction, SetValueAction } from './actions'; |
2 | | -import { createFormStateReducerWithUpdate, formStateReducer } from './reducer'; |
| 3 | +import { createFormStateReducerWithUpdate, formStateReducer, onNgrxForms, wrapReducerWithFormStateUpdate } from './reducer'; |
3 | 4 | import { FORM_CONTROL_ID, FORM_CONTROL_INNER5_ID, FORM_CONTROL_INNER_ID, FormGroupValue, INITIAL_STATE } from './update-function/test-util'; |
4 | 5 | import { updateGroup } from './update-function/update-group'; |
5 | 6 |
|
@@ -125,3 +126,139 @@ describe(createFormStateReducerWithUpdate.name, () => { |
125 | 126 | expect(() => createFormStateReducerWithUpdate<any>(s => s)(undefined, { type: '' })).toThrowError(); |
126 | 127 | }); |
127 | 128 | }); |
| 129 | + |
| 130 | +describe(onNgrxForms.name, () => { |
| 131 | + it('should call the reducer for controls', () => { |
| 132 | + const state = { |
| 133 | + prop: 'value', |
| 134 | + form: INITIAL_STATE.controls.inner, |
| 135 | + }; |
| 136 | + |
| 137 | + const resultState = onNgrxForms<typeof state>().reducer(state, new MarkAsTouchedAction(FORM_CONTROL_INNER_ID)); |
| 138 | + expect(resultState.form).not.toBe(INITIAL_STATE.controls.inner); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should call the reducer for groups', () => { |
| 142 | + const state = { |
| 143 | + prop: 'value', |
| 144 | + form: INITIAL_STATE, |
| 145 | + }; |
| 146 | + |
| 147 | + const resultState = onNgrxForms<typeof state>().reducer(state, new MarkAsTouchedAction(FORM_CONTROL_ID)); |
| 148 | + expect(resultState.form).not.toBe(INITIAL_STATE); |
| 149 | + }); |
| 150 | + |
| 151 | + it('should call the reducer for arrays', () => { |
| 152 | + const state = { |
| 153 | + prop: 'value', |
| 154 | + form: INITIAL_STATE.controls.inner5, |
| 155 | + }; |
| 156 | + |
| 157 | + const resultState = onNgrxForms<typeof state>().reducer(state, new MarkAsTouchedAction(FORM_CONTROL_INNER5_ID)); |
| 158 | + expect(resultState.form).not.toBe(INITIAL_STATE.controls.inner5); |
| 159 | + }); |
| 160 | + |
| 161 | + it('should work with createReducer', () => { |
| 162 | + const state = { |
| 163 | + prop: 'value', |
| 164 | + control: INITIAL_STATE.controls.inner, |
| 165 | + group: INITIAL_STATE, |
| 166 | + array: INITIAL_STATE.controls.inner5, |
| 167 | + }; |
| 168 | + |
| 169 | + const reducer = createReducer( |
| 170 | + state, |
| 171 | + onNgrxForms(), |
| 172 | + ); |
| 173 | + |
| 174 | + let resultState = reducer(state, new MarkAsTouchedAction(FORM_CONTROL_INNER_ID)); |
| 175 | + expect(resultState.control).not.toBe(INITIAL_STATE.controls.inner); |
| 176 | + |
| 177 | + resultState = reducer(state, new MarkAsTouchedAction(FORM_CONTROL_ID)); |
| 178 | + expect(resultState.group).not.toBe(INITIAL_STATE); |
| 179 | + |
| 180 | + resultState = reducer(state, new MarkAsTouchedAction(FORM_CONTROL_INNER5_ID)); |
| 181 | + expect(resultState.array).not.toBe(INITIAL_STATE.controls.inner5); |
| 182 | + }); |
| 183 | +}); |
| 184 | + |
| 185 | +describe(wrapReducerWithFormStateUpdate.name, () => { |
| 186 | + const initialState = { |
| 187 | + prop: 'value', |
| 188 | + control: INITIAL_STATE.controls.inner, |
| 189 | + group: INITIAL_STATE, |
| 190 | + array: INITIAL_STATE.controls.inner5, |
| 191 | + }; |
| 192 | + |
| 193 | + function reducer(state = initialState, _: Action) { |
| 194 | + return state; |
| 195 | + } |
| 196 | + |
| 197 | + it('should update a control after the reducer', () => { |
| 198 | + const wrappedReducer = wrapReducerWithFormStateUpdate(reducer, s => s.control, s => { |
| 199 | + expect(s).toBe(INITIAL_STATE.controls.inner); |
| 200 | + return ({ ...s }); |
| 201 | + }); |
| 202 | + |
| 203 | + const resultState = wrappedReducer(undefined, { type: '' }); |
| 204 | + expect(resultState.control).not.toBe(INITIAL_STATE.controls.inner); |
| 205 | + }); |
| 206 | + |
| 207 | + it('should update a group after the reducer', () => { |
| 208 | + const wrappedReducer = wrapReducerWithFormStateUpdate(reducer, s => s.group, s => { |
| 209 | + expect(s).toBe(INITIAL_STATE); |
| 210 | + return ({ ...s }); |
| 211 | + }); |
| 212 | + |
| 213 | + const resultState = wrappedReducer(undefined, { type: '' }); |
| 214 | + expect(resultState.group).not.toBe(INITIAL_STATE); |
| 215 | + }); |
| 216 | + |
| 217 | + it('should update an array after the reducer', () => { |
| 218 | + const wrappedReducer = wrapReducerWithFormStateUpdate(reducer, s => s.array, s => { |
| 219 | + expect(s).toBe(INITIAL_STATE.controls.inner5); |
| 220 | + return ({ ...s }); |
| 221 | + }); |
| 222 | + |
| 223 | + const resultState = wrappedReducer(undefined, { type: '' }); |
| 224 | + expect(resultState.array).not.toBe(INITIAL_STATE.controls.inner5); |
| 225 | + }); |
| 226 | + |
| 227 | + it('should set the updated form state', () => { |
| 228 | + const updatedControl = { ...INITIAL_STATE.controls.inner }; |
| 229 | + const wrappedReducer = wrapReducerWithFormStateUpdate(reducer, s => s.control, () => updatedControl); |
| 230 | + const resultState = wrappedReducer(undefined, { type: '' }); |
| 231 | + expect(resultState.control).toBe(updatedControl); |
| 232 | + }); |
| 233 | + |
| 234 | + it('should not update the state if the form state is not updated', () => { |
| 235 | + const wrappedReducer = wrapReducerWithFormStateUpdate(reducer, s => s.control, s => s); |
| 236 | + const resultState = wrappedReducer(undefined, { type: '' }); |
| 237 | + expect(resultState).toBe(initialState); |
| 238 | + }); |
| 239 | + |
| 240 | + it('should work with createReducer', () => { |
| 241 | + const state = { |
| 242 | + prop: 'value', |
| 243 | + control: INITIAL_STATE.controls.inner, |
| 244 | + group: INITIAL_STATE, |
| 245 | + array: INITIAL_STATE.controls.inner5, |
| 246 | + }; |
| 247 | + |
| 248 | + const reducer = createReducer( |
| 249 | + state, |
| 250 | + onNgrxForms(), |
| 251 | + ); |
| 252 | + |
| 253 | + const wrappedReducer = wrapReducerWithFormStateUpdate(reducer, s => s.control, s => ({ ...s })); |
| 254 | + |
| 255 | + let resultState = wrappedReducer(state, new MarkAsTouchedAction(FORM_CONTROL_INNER_ID)); |
| 256 | + expect(resultState.control).not.toBe(INITIAL_STATE.controls.inner); |
| 257 | + |
| 258 | + resultState = wrappedReducer(state, new MarkAsTouchedAction(FORM_CONTROL_ID)); |
| 259 | + expect(resultState.group).not.toBe(INITIAL_STATE); |
| 260 | + |
| 261 | + resultState = wrappedReducer(state, new MarkAsTouchedAction(FORM_CONTROL_INNER5_ID)); |
| 262 | + expect(resultState.array).not.toBe(INITIAL_STATE.controls.inner5); |
| 263 | + }); |
| 264 | +}); |
0 commit comments