-
-
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.
feat(signals): add
withProps
base feature (#4607)
BREAKING CHANGES: - The `computed` property in `SignalStoreFeatureResult` type is renamed to `props`. - The `EntityComputed` and `NamedEntityComputed` types in the `entities` plugin are renamed to `EntityProps` and `NamedEntityProps`. BEFORE: ```ts import { computed, Signal } from '@angular/core'; import { signalStoreFeature, SignalStoreFeature, type, withComputed, } from '@ngrx/signals'; import { EntityComputed } from '@ngrx/signals/entities'; export function withTotalEntities<Entity>(): SignalStoreFeature< { state: {}, computed: EntityComputed<Entity>, methods: {} }, { state: {}, computed: { total: Signal<number> }, methods: {} }, > { return signalStoreFeature( { computed: type<EntityComputed<Entity>>() }, withComputed(({ entities }) => ({ total: computed(() => entities().length), })), ); } ``` AFTER: ```ts import { computed, Signal } from '@angular/core'; import { signalStoreFeature, SignalStoreFeature, type, withComputed, } from '@ngrx/signals'; import { EntityProps } from '@ngrx/signals/entities'; export function withTotalEntities<Entity>(): SignalStoreFeature< { state: {}, props: EntityProps<Entity>, methods: {} }, { state: {}, props: { total: Signal<number> }, methods: {} }, > { return signalStoreFeature( { props: type<EntityProps<Entity>>() }, withComputed(({ entities }) => ({ total: computed(() => entities().length), })), ); } ```
- Loading branch information
1 parent
2528d39
commit e626082
Showing
23 changed files
with
291 additions
and
72 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
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
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
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
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,50 @@ | ||
import { signal } from '@angular/core'; | ||
import { of } from 'rxjs'; | ||
import { withMethods, withProps, withState } from '../src'; | ||
import { getInitialInnerStore } from '../src/signal-store'; | ||
|
||
describe('withProps', () => { | ||
it('adds properties to the store immutably', () => { | ||
const initialStore = getInitialInnerStore(); | ||
|
||
const store = withProps(() => ({ p1: 1, p2: 2 }))(initialStore); | ||
|
||
expect(Object.keys(store.props)).toEqual(['p1', 'p2']); | ||
expect(Object.keys(initialStore.props)).toEqual([]); | ||
|
||
expect(store.props.p1).toBe(1); | ||
expect(store.props.p2).toBe(2); | ||
}); | ||
|
||
it('logs warning if previously defined signal store members have the same name', () => { | ||
const initialStore = [ | ||
withState({ | ||
s1: 10, | ||
s2: 's2', | ||
}), | ||
withProps(() => ({ | ||
p1: of(100), | ||
p2: 10, | ||
})), | ||
withMethods(() => ({ | ||
m1() {}, | ||
m2() {}, | ||
})), | ||
].reduce((acc, feature) => feature(acc), getInitialInnerStore()); | ||
jest.spyOn(console, 'warn').mockImplementation(); | ||
|
||
withProps(() => ({ | ||
s1: { foo: 'bar' }, | ||
p: 10, | ||
p2: signal(100), | ||
m1: { ngrx: 'rocks' }, | ||
m3: of('m3'), | ||
}))(initialStore); | ||
|
||
expect(console.warn).toHaveBeenCalledWith( | ||
'@ngrx/signals: SignalStore members cannot be overridden.', | ||
'Trying to override:', | ||
's1, p2, m1' | ||
); | ||
}); | ||
}); |
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
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
Oops, something went wrong.