-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract
deepFreeze
tests to separate file
- Loading branch information
1 parent
adf5b1e
commit 5a854c1
Showing
2 changed files
with
57 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
); | ||
}); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters