-
-
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.
- Loading branch information
1 parent
eeadd3f
commit f2514ba
Showing
6 changed files
with
196 additions
and
188 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,77 @@ | ||
import { patchState, signalState } from '../src'; | ||
|
||
describe('patchState', () => { | ||
const initialState = { | ||
user: { | ||
firstName: 'John', | ||
lastName: 'Smith', | ||
}, | ||
foo: 'bar', | ||
numbers: [1, 2, 3], | ||
ngrx: 'signals', | ||
}; | ||
|
||
it('patches state via partial state object', () => { | ||
const state = signalState(initialState); | ||
|
||
patchState(state, { | ||
user: { firstName: 'Johannes', lastName: 'Schmidt' }, | ||
foo: 'baz', | ||
}); | ||
|
||
expect(state()).toEqual({ | ||
...initialState, | ||
user: { firstName: 'Johannes', lastName: 'Schmidt' }, | ||
foo: 'baz', | ||
}); | ||
}); | ||
|
||
it('patches state via updater function', () => { | ||
const state = signalState(initialState); | ||
|
||
patchState(state, (state) => ({ | ||
numbers: [...state.numbers, 4], | ||
ngrx: 'rocks', | ||
})); | ||
|
||
expect(state()).toEqual({ | ||
...initialState, | ||
numbers: [1, 2, 3, 4], | ||
ngrx: 'rocks', | ||
}); | ||
}); | ||
|
||
it('patches state via sequence of partial state objects and updater functions', () => { | ||
const state = signalState(initialState); | ||
|
||
patchState( | ||
state, | ||
{ user: { firstName: 'Johannes', lastName: 'Schmidt' } }, | ||
(state) => ({ numbers: [...state.numbers, 4], foo: 'baz' }), | ||
(state) => ({ user: { ...state.user, firstName: 'Jovan' } }), | ||
{ foo: 'foo' } | ||
); | ||
|
||
expect(state()).toEqual({ | ||
...initialState, | ||
user: { firstName: 'Jovan', lastName: 'Schmidt' }, | ||
foo: 'foo', | ||
numbers: [1, 2, 3, 4], | ||
}); | ||
}); | ||
|
||
it('patches state immutably', () => { | ||
const state = signalState(initialState); | ||
|
||
patchState(state, { | ||
foo: 'bar', | ||
numbers: [3, 2, 1], | ||
ngrx: 'rocks', | ||
}); | ||
|
||
expect(state.user()).toBe(initialState.user); | ||
expect(state.foo()).toBe(initialState.foo); | ||
expect(state.numbers()).not.toBe(initialState.numbers); | ||
expect(state.ngrx()).not.toBe(initialState.ngrx); | ||
}); | ||
}); |
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
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export { PartialStateUpdater, patchState } from './patch-state'; | ||
export { selectSignal } from './select-signal'; | ||
export { signalState } from './signal-state'; | ||
export { SignalStateUpdater } from './signal-state-models'; |
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,20 @@ | ||
import { SIGNAL_STATE_META_KEY, SignalStateMeta } from './signal-state'; | ||
|
||
export type PartialStateUpdater<State extends Record<string, unknown>> = | ||
| Partial<State> | ||
| ((state: State) => Partial<State>); | ||
|
||
export function patchState<State extends Record<string, unknown>>( | ||
signalState: SignalStateMeta<State>, | ||
...updaters: PartialStateUpdater<State>[] | ||
): void { | ||
signalState[SIGNAL_STATE_META_KEY].update((currentState) => | ||
updaters.reduce( | ||
(nextState: State, updater) => ({ | ||
...nextState, | ||
...(typeof updater === 'function' ? updater(nextState) : updater), | ||
}), | ||
currentState | ||
) | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.