-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: unit test, and small bug fixes found in unit test
Missing unit test, and fixed bug in entitiesSelected() of withEntitiesMultiSelection
- Loading branch information
1 parent
061a9b1
commit 6138fd1
Showing
6 changed files
with
209 additions
and
8 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
Empty file.
120 changes: 120 additions & 0 deletions
120
...ngrx-traits/signals/src/lib/with-entities-selection/with-entities-multi-selection.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,120 @@ | ||
import { withEntitiesMultiSelection } from '@ngrx-traits/signals'; | ||
import { patchState, signalStore, type } from '@ngrx/signals'; | ||
import { setAllEntities, withEntities } from '@ngrx/signals/entities'; | ||
|
||
import { mockProducts } from '../test.mocks'; | ||
import { Product } from '../test.model'; | ||
|
||
describe('withEntitiesMultiSelection', () => { | ||
const entity = type<Product>(); | ||
|
||
describe('without collection', () => { | ||
const Store = signalStore( | ||
withEntities({ entity }), | ||
withEntitiesMultiSelection({ entity }), | ||
); | ||
it('selectEntities should select the entity', () => { | ||
const store = new Store(); | ||
patchState(store, setAllEntities(mockProducts)); | ||
store.selectEntities({ ids: [mockProducts[4].id, mockProducts[8].id] }); | ||
expect(store.entitiesSelected()).toEqual([ | ||
mockProducts[4], | ||
mockProducts[8], | ||
]); | ||
}); | ||
|
||
it('deselectEntities should deselect the entity', () => { | ||
const store = new Store(); | ||
patchState(store, setAllEntities(mockProducts)); | ||
store.selectEntities({ ids: [mockProducts[4].id, mockProducts[8].id] }); | ||
store.deselectEntities({ ids: [mockProducts[4].id, mockProducts[8].id] }); | ||
expect(store.entitiesSelected()).toEqual([]); | ||
}); | ||
|
||
it('toggleSelectEntities should toggle selection of the entity', () => { | ||
const store = new Store(); | ||
patchState(store, setAllEntities(mockProducts)); | ||
store.toggleSelectEntities({ id: mockProducts[4].id }); | ||
expect(store.entitiesSelected()).toEqual([mockProducts[4]]); | ||
store.toggleSelectEntities({ id: mockProducts[4].id }); | ||
expect(store.entitiesSelected()).toEqual([]); | ||
}); | ||
|
||
it('toggleSelectAllEntities should toggle selection of the entity', () => { | ||
const store = new Store(); | ||
patchState(store, setAllEntities(mockProducts)); | ||
store.toggleSelectAllEntities(); | ||
expect(store.entitiesSelected().length).toEqual(mockProducts.length); | ||
store.toggleSelectAllEntities(); | ||
expect(store.entitiesSelected()).toEqual([]); | ||
}); | ||
|
||
it('isAllEntitiesSelected should toggle selection of the entity', () => { | ||
const store = new Store(); | ||
patchState(store, setAllEntities(mockProducts)); | ||
store.toggleSelectAllEntities(); | ||
expect(store.isAllEntitiesSelected()).toEqual('all'); | ||
store.toggleSelectAllEntities(); | ||
expect(store.isAllEntitiesSelected()).toEqual('none'); | ||
store.toggleSelectEntities({ ids: mockProducts.map((p) => p.id) }); | ||
expect(store.isAllEntitiesSelected()).toEqual('all'); | ||
store.toggleSelectEntities({ id: mockProducts[4].id }); | ||
expect(store.isAllEntitiesSelected()).toEqual('some'); | ||
}); | ||
}); | ||
|
||
describe('with collection', () => { | ||
const collection = 'products'; | ||
const Store = signalStore( | ||
withEntities({ entity, collection }), | ||
withEntitiesMultiSelection({ entity, collection }), | ||
); | ||
it('select[Collection]Entities should select the entity', () => { | ||
const store = new Store(); | ||
patchState(store, setAllEntities(mockProducts, { collection })); | ||
store.selectProductsEntities({ | ||
ids: [mockProducts[4].id, mockProducts[8].id], | ||
}); | ||
expect(store.productsSelectedEntities()).toEqual([ | ||
mockProducts[4], | ||
mockProducts[8], | ||
]); | ||
}); | ||
|
||
it('deselect[Collection]Entities should deselect the entity', () => { | ||
const store = new Store(); | ||
patchState(store, setAllEntities(mockProducts, { collection })); | ||
store.selectProductsEntities({ | ||
ids: [mockProducts[4].id, mockProducts[8].id], | ||
}); | ||
store.deselectProductsEntities({ | ||
ids: [mockProducts[4].id, mockProducts[8].id], | ||
}); | ||
expect(store.productsSelectedEntities()).toEqual([]); | ||
}); | ||
|
||
it('toggle[Collection]Entities should toggle selection of the entity', () => { | ||
const store = new Store(); | ||
patchState(store, setAllEntities(mockProducts, { collection })); | ||
store.toggleSelectProductsEntities({ id: mockProducts[4].id }); | ||
expect(store.productsSelectedEntities()).toEqual([mockProducts[4]]); | ||
store.toggleSelectProductsEntities({ id: mockProducts[4].id }); | ||
expect(store.productsSelectedEntities()).toEqual([]); | ||
}); | ||
|
||
it('isAll[Collection]Selected should toggle selection of the entity', () => { | ||
const store = new Store(); | ||
patchState(store, setAllEntities(mockProducts, { collection })); | ||
store.toggleSelectAllProductsEntities(); | ||
expect(store.isAllProductsSelected()).toEqual('all'); | ||
store.toggleSelectAllProductsEntities(); | ||
expect(store.isAllProductsSelected()).toEqual('none'); | ||
store.toggleSelectProductsEntities({ | ||
ids: mockProducts.map((p) => p.id), | ||
}); | ||
expect(store.isAllProductsSelected()).toEqual('all'); | ||
store.toggleSelectProductsEntities({ id: mockProducts[4].id }); | ||
expect(store.isAllProductsSelected()).toEqual('some'); | ||
}); | ||
}); | ||
}); |
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
71 changes: 71 additions & 0 deletions
71
...grx-traits/signals/src/lib/with-entities-selection/with-entities-single-selection.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,71 @@ | ||
import { withEntitiesSingleSelection } from '@ngrx-traits/signals'; | ||
import { patchState, signalStore, type } from '@ngrx/signals'; | ||
import { setAllEntities, withEntities } from '@ngrx/signals/entities'; | ||
|
||
import { mockProducts } from '../test.mocks'; | ||
import { Product } from '../test.model'; | ||
|
||
describe('withEntitiesSingleSelection', () => { | ||
const entity = type<Product>(); | ||
|
||
describe('without collection', () => { | ||
const Store = signalStore( | ||
withEntities({ entity }), | ||
withEntitiesSingleSelection({ entity }), | ||
); | ||
it('selectEntity should select the entity', () => { | ||
const store = new Store(); | ||
patchState(store, setAllEntities(mockProducts)); | ||
store.selectEntity({ id: mockProducts[4].id }); | ||
expect(store.entitiesSelectedEntity()).toEqual(mockProducts[4]); | ||
}); | ||
|
||
it('deselectEntity should deselect the entity', () => { | ||
const store = new Store(); | ||
patchState(store, setAllEntities(mockProducts)); | ||
store.selectEntity({ id: mockProducts[4].id }); | ||
store.deselectEntity({ id: mockProducts[4].id }); | ||
expect(store.entitiesSelectedEntity()).toEqual(undefined); | ||
}); | ||
|
||
it('toggleEntity should toggle selection of the entity', () => { | ||
const store = new Store(); | ||
patchState(store, setAllEntities(mockProducts)); | ||
store.toggleEntity({ id: mockProducts[4].id }); | ||
expect(store.entitiesSelectedEntity()).toEqual(mockProducts[4]); | ||
store.toggleEntity({ id: mockProducts[4].id }); | ||
expect(store.entitiesSelectedEntity()).toEqual(undefined); | ||
}); | ||
}); | ||
|
||
describe('with collection', () => { | ||
const collection = 'products'; | ||
const Store = signalStore( | ||
withEntities({ entity, collection }), | ||
withEntitiesSingleSelection({ entity, collection }), | ||
); | ||
it('selectEntity should select the entity', () => { | ||
const store = new Store(); | ||
patchState(store, setAllEntities(mockProducts, { collection })); | ||
store.selectProductsEntity({ id: mockProducts[4].id }); | ||
expect(store.productsSelectedEntity()).toEqual(mockProducts[4]); | ||
}); | ||
|
||
it('deselectEntity should deselect the entity', () => { | ||
const store = new Store(); | ||
patchState(store, setAllEntities(mockProducts, { collection })); | ||
store.selectProductsEntity({ id: mockProducts[4].id }); | ||
store.deselectProductsEntity({ id: mockProducts[4].id }); | ||
expect(store.productsSelectedEntity()).toEqual(undefined); | ||
}); | ||
|
||
it('toggleEntity should toggle selection of the entity', () => { | ||
const store = new Store(); | ||
patchState(store, setAllEntities(mockProducts, { collection })); | ||
store.toggleProductsEntity({ id: mockProducts[4].id }); | ||
expect(store.productsSelectedEntity()).toEqual(mockProducts[4]); | ||
store.toggleProductsEntity({ id: mockProducts[4].id }); | ||
expect(store.productsSelectedEntity()).toEqual(undefined); | ||
}); | ||
}); | ||
}); |
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