|
| 1 | +import { getState, patchState } from '../src/state-source'; |
| 2 | +import { signalState } from '../src/signal-state'; |
| 3 | +import { signalStore } from '../src/signal-store'; |
| 4 | +import { TestBed } from '@angular/core/testing'; |
| 5 | +import { withState } from '../src/with-state'; |
| 6 | + |
| 7 | +describe('deepFreeze', () => { |
| 8 | + const initialState = { |
| 9 | + user: { |
| 10 | + firstName: 'John', |
| 11 | + lastName: 'Smith', |
| 12 | + }, |
| 13 | + foo: 'bar', |
| 14 | + numbers: [1, 2, 3], |
| 15 | + ngrx: 'signals', |
| 16 | + }; |
| 17 | + |
| 18 | + for (const { stateFactory, name } of [ |
| 19 | + { |
| 20 | + name: 'signalStore', |
| 21 | + stateFactory: () => { |
| 22 | + const Store = signalStore( |
| 23 | + { protectedState: false }, |
| 24 | + withState(initialState) |
| 25 | + ); |
| 26 | + return TestBed.configureTestingModule({ providers: [Store] }).inject( |
| 27 | + Store |
| 28 | + ); |
| 29 | + }, |
| 30 | + }, |
| 31 | + { name: 'signalState', stateFactory: () => signalState(initialState) }, |
| 32 | + ]) { |
| 33 | + describe(name, () => { |
| 34 | + it(`throws on a mutable change`, () => { |
| 35 | + const state = stateFactory(); |
| 36 | + expect(() => |
| 37 | + patchState(state, (state) => { |
| 38 | + state.ngrx = 'mutable change'; |
| 39 | + return state; |
| 40 | + }) |
| 41 | + ).toThrowError("Cannot assign to read only property 'ngrx' of object"); |
| 42 | + }); |
| 43 | + |
| 44 | + it('throws on a nested mutable change', () => { |
| 45 | + const state = stateFactory(); |
| 46 | + expect(() => |
| 47 | + patchState(state, (state) => { |
| 48 | + state.user.firstName = 'mutable change'; |
| 49 | + return state; |
| 50 | + }) |
| 51 | + ).toThrowError( |
| 52 | + "Cannot assign to read only property 'firstName' of object" |
| 53 | + ); |
| 54 | + }); |
| 55 | + describe('mutable changes outside of patchState', () => { |
| 56 | + it('throws on reassigned a property of the exposed state', () => { |
| 57 | + const state = stateFactory(); |
| 58 | + expect(() => { |
| 59 | + state.user().firstName = 'mutable change 1'; |
| 60 | + }).toThrowError( |
| 61 | + "Cannot assign to read only property 'firstName' of object" |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it('throws when exposed state via getState is mutated', () => { |
| 66 | + const state = stateFactory(); |
| 67 | + const s = getState(state); |
| 68 | + |
| 69 | + expect(() => (s.ngrx = 'mutable change 2')).toThrowError( |
| 70 | + "Cannot assign to read only property 'ngrx' of object" |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + it('throws when mutable change happens for', () => { |
| 75 | + const state = stateFactory(); |
| 76 | + const s = { user: { firstName: 'M', lastName: 'S' } }; |
| 77 | + patchState(state, s); |
| 78 | + |
| 79 | + expect(() => { |
| 80 | + s.user.firstName = 'mutable change 3'; |
| 81 | + }).toThrowError( |
| 82 | + "Cannot assign to read only property 'firstName' of object" |
| 83 | + ); |
| 84 | + }); |
| 85 | + }); |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + describe('special tests', () => { |
| 90 | + for (const { name, mutationFn } of [ |
| 91 | + { |
| 92 | + name: 'location', |
| 93 | + mutationFn: (state: { location: { city: string } }) => |
| 94 | + (state.location.city = 'Paris'), |
| 95 | + }, |
| 96 | + { |
| 97 | + name: 'user', |
| 98 | + mutationFn: (state: { user: { firstName: string } }) => |
| 99 | + (state.user.firstName = 'Jane'), |
| 100 | + }, |
| 101 | + ]) { |
| 102 | + it(`throws on concatenated state (${name})`, () => { |
| 103 | + const UserStore = signalStore( |
| 104 | + { providedIn: 'root' }, |
| 105 | + withState(initialState), |
| 106 | + withState({ location: { city: 'London' } }) |
| 107 | + ); |
| 108 | + const store = TestBed.inject(UserStore); |
| 109 | + const state = getState(store); |
| 110 | + |
| 111 | + expect(() => mutationFn(state)).toThrowError(); |
| 112 | + }); |
| 113 | + } |
| 114 | + }); |
| 115 | +}); |
0 commit comments