From 5a854c11d42711dd2aa476d04b06cdceef0b56c8 Mon Sep 17 00:00:00 2001 From: Rainer Hahnekamp Date: Fri, 15 Nov 2024 13:53:35 +0100 Subject: [PATCH] refactor: extract `deepFreeze` tests to separate file --- modules/signals/spec/deep-freeze.spec.ts | 57 +++++++++++++++++++++++ modules/signals/spec/signal-state.spec.ts | 24 ---------- 2 files changed, 57 insertions(+), 24 deletions(-) create mode 100644 modules/signals/spec/deep-freeze.spec.ts diff --git a/modules/signals/spec/deep-freeze.spec.ts b/modules/signals/spec/deep-freeze.spec.ts new file mode 100644 index 0000000000..2355b7fc3d --- /dev/null +++ b/modules/signals/spec/deep-freeze.spec.ts @@ -0,0 +1,57 @@ +import { patchState } from '../src/state-source'; +import { signalState } from '../src/signal-state'; +import { signalStore } from '../src/signal-store'; +import { TestBed } from '@angular/core/testing'; +import { withState } from '../src/with-state'; + +describe('deepFreeze', () => { + const initialState = { + user: { + firstName: 'John', + lastName: 'Smith', + }, + foo: 'bar', + numbers: [1, 2, 3], + ngrx: 'signals', + }; + + for (const { stateFactory, name } of [ + { + name: 'signalStore', + stateFactory: () => { + const Store = signalStore( + { protectedState: false }, + withState(initialState) + ); + return TestBed.configureTestingModule({ providers: [Store] }).inject( + Store + ); + }, + }, + { name: 'signalState', stateFactory: () => signalState(initialState) }, + ]) { + describe(name, () => { + it(`throws on a mutable change`, () => { + const state = stateFactory(); + expect(() => + patchState(state, (state) => { + state.ngrx = 'mutable change'; + return state; + }) + ).toThrowError("Cannot assign to read only property 'ngrx' of object"); + }); + + it('throws on a nested mutable change', () => { + const state = stateFactory(); + expect(() => + patchState(state, (state) => { + state.user.firstName = 'mutable change'; + return state; + }) + ).toThrowError( + "Cannot assign to read only property 'firstName' of object" + ); + }); + }); + } +}); diff --git a/modules/signals/spec/signal-state.spec.ts b/modules/signals/spec/signal-state.spec.ts index e0be46b790..c509d07f04 100644 --- a/modules/signals/spec/signal-state.spec.ts +++ b/modules/signals/spec/signal-state.spec.ts @@ -194,28 +194,4 @@ describe('signalState', () => { expect(stateCounter).toBe(3); expect(userCounter).toBe(1); })); - - describe('freezeInDevMode', () => { - it('throws on a mutable change', () => { - const userState = signalState(initialState); - expect(() => - patchState(userState, (state) => { - state.ngrx = 'mutable change'; - return state; - }) - ).toThrowError("Cannot assign to read only property 'ngrx' of object"); - }); - - it('throws on a mutable change', () => { - const userState = signalState(initialState); - expect(() => - patchState(userState, (state) => { - state.user.firstName = 'mutable change'; - return state; - }) - ).toThrowError( - "Cannot assign to read only property 'firstName' of object" - ); - }); - }); });