Skip to content

Commit

Permalink
refactor: extract deepFreeze tests to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
rainerhahnekamp committed Nov 15, 2024
1 parent adf5b1e commit 5a854c1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 24 deletions.
57 changes: 57 additions & 0 deletions modules/signals/spec/deep-freeze.spec.ts
Original file line number Diff line number Diff line change
@@ -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"
);
});
});
}
});
24 changes: 0 additions & 24 deletions modules/signals/spec/signal-state.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
});
});
});

0 comments on commit 5a854c1

Please sign in to comment.