Skip to content

Commit 5a854c1

Browse files
refactor: extract deepFreeze tests to separate file
1 parent adf5b1e commit 5a854c1

File tree

2 files changed

+57
-24
lines changed

2 files changed

+57
-24
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { 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+
});
56+
}
57+
});

modules/signals/spec/signal-state.spec.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -194,28 +194,4 @@ describe('signalState', () => {
194194
expect(stateCounter).toBe(3);
195195
expect(userCounter).toBe(1);
196196
}));
197-
198-
describe('freezeInDevMode', () => {
199-
it('throws on a mutable change', () => {
200-
const userState = signalState(initialState);
201-
expect(() =>
202-
patchState(userState, (state) => {
203-
state.ngrx = 'mutable change';
204-
return state;
205-
})
206-
).toThrowError("Cannot assign to read only property 'ngrx' of object");
207-
});
208-
209-
it('throws on a mutable change', () => {
210-
const userState = signalState(initialState);
211-
expect(() =>
212-
patchState(userState, (state) => {
213-
state.user.firstName = 'mutable change';
214-
return state;
215-
})
216-
).toThrowError(
217-
"Cannot assign to read only property 'firstName' of object"
218-
);
219-
});
220-
});
221197
});

0 commit comments

Comments
 (0)