-
-
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.
RFC(Signals): Add selectable entity as default behaviour
- Updated withEntities to add selectedId and selectedEntity (even for named collections); - Created selectEntity helper to select an entity given its ID. - Created clearSelectedEntity helper to remove the currently selected entity without caring about its ID. - Added Unit Tests - Added documentation Issue reference: #4717
- Loading branch information
Showing
11 changed files
with
329 additions
and
74 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
modules/signals/entities/spec/updaters/clear-selected-entity.spec.ts
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, signalStore } from '@ngrx/signals'; | ||
import { | ||
addEntities, | ||
withEntities, | ||
selectEntity, | ||
clearSelectedEntity, | ||
} from '../../src'; | ||
import { User, user1, user2, user3 } from '../mocks'; | ||
|
||
describe('selectEntity', () => { | ||
it('should clear the selectedEntity and selectedEntityId if an entity is selected and exists in the state', () => { | ||
const Store = signalStore({ protectedState: false }, withEntities<User>()); | ||
const store = new Store(); | ||
|
||
patchState(store, addEntities([user1, user2, user3])); | ||
patchState(store, selectEntity(user1.id)); | ||
|
||
expect(store.selectedId()).toBe(user1.id); | ||
expect(store.selectedEntity()).toBe(user1); | ||
|
||
patchState(store, clearSelectedEntity()); | ||
|
||
expect(store.selectedId()).toBe(null); | ||
expect(store.selectedEntity()).toBe(null); | ||
}); | ||
|
||
it('should clear the selectedEntity and selectedEntityId if an entity is selected and it does not exists in the state', () => { | ||
const Store = signalStore({ protectedState: false }, withEntities<User>()); | ||
const store = new Store(); | ||
|
||
patchState(store, addEntities([user1, user2])); | ||
patchState(store, selectEntity(user3.id)); | ||
|
||
expect(store.selectedId()).toBe(user3.id); | ||
expect(store.selectedEntity()).toBe(null); | ||
|
||
patchState(store, clearSelectedEntity()); | ||
|
||
expect(store.selectedId()).toBe(null); | ||
expect(store.selectedEntity()).toBe(null); | ||
}); | ||
|
||
it('should not change the state if an entity is not selected', () => { | ||
const Store = signalStore({ protectedState: false }, withEntities<User>()); | ||
const store = new Store(); | ||
|
||
patchState(store, addEntities([user1, user2, user3])); | ||
|
||
expect(store.selectedId()).toBe(null); | ||
expect(store.selectedEntity()).toBe(null); | ||
|
||
patchState(store, clearSelectedEntity()); | ||
|
||
expect(store.selectedId()).toBe(null); | ||
expect(store.selectedEntity()).toBe(null); | ||
}); | ||
}); |
49 changes: 49 additions & 0 deletions
49
modules/signals/entities/spec/updaters/select-entity.spec.ts
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,49 @@ | ||
import { patchState, signalStore, type } from '@ngrx/signals'; | ||
import { | ||
addEntities, | ||
addEntity, | ||
withEntities, | ||
selectEntity, | ||
setEntities, | ||
} from '../../src'; | ||
import { Todo, todo1, todo2, todo3, User, user1, user2, user3 } from '../mocks'; | ||
|
||
describe('selectEntity', () => { | ||
it('should select an entity and return it if exists', () => { | ||
const Store = signalStore({ protectedState: false }, withEntities<User>()); | ||
const store = new Store(); | ||
|
||
patchState(store, addEntities([user1, user2, user3])); | ||
patchState(store, selectEntity(user1.id)); | ||
|
||
expect(store.selectedId()).toBe(user1.id); | ||
expect(store.selectedEntity()).toBe(user1); | ||
}); | ||
|
||
it('should select an entity and return null if it does not exists', () => { | ||
const Store = signalStore({ protectedState: false }, withEntities<User>()); | ||
const store = new Store(); | ||
|
||
patchState(store, addEntities([user1, user2])); | ||
patchState(store, selectEntity(user3.id)); | ||
|
||
expect(store.selectedId()).toBe(user3.id); | ||
expect(store.selectedEntity()).toBe(null); | ||
}); | ||
|
||
it('should return null if the selected entity does not exist and return the entity as soon as it is added to the state', () => { | ||
const Store = signalStore({ protectedState: false }, withEntities<User>()); | ||
const store = new Store(); | ||
|
||
patchState(store, addEntities([user1, user2])); | ||
patchState(store, selectEntity(user3.id)); | ||
|
||
expect(store.selectedId()).toBe(user3.id); | ||
expect(store.selectedEntity()).toBe(null); | ||
|
||
patchState(store, addEntity(user3)); | ||
|
||
expect(store.selectedId()).toBe(user3.id); | ||
expect(store.selectedEntity()).toBe(user3); | ||
}); | ||
}); |
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.