|
1 |
| -import { createEntityAdapter, createSlice } from '@reduxjs/toolkit' |
| 1 | +import { createEntityAdapter, createSlice } from '../..' |
2 | 2 | import type {
|
3 | 3 | PayloadAction,
|
| 4 | + Slice, |
4 | 5 | SliceCaseReducers,
|
5 |
| - ValidateSliceCaseReducers, |
| 6 | + UnknownAction, |
6 | 7 | } from '../..'
|
7 | 8 | import type { EntityId, EntityState, IdSelector } from '../models'
|
8 |
| -import { AClockworkOrange, type BookModel } from './fixtures/book' |
| 9 | +import type { BookModel } from './fixtures/book' |
9 | 10 |
|
10 | 11 | describe('Entity Slice Enhancer', () => {
|
11 |
| - let slice: ReturnType<typeof entitySliceEnhancer<BookModel, string>> |
| 12 | + let slice: Slice<EntityState<BookModel, BookModel['id']>> |
12 | 13 |
|
13 | 14 | beforeEach(() => {
|
14 |
| - slice = entitySliceEnhancer({ |
| 15 | + const indieSlice = entitySliceEnhancer({ |
15 | 16 | name: 'book',
|
16 | 17 | selectId: (book: BookModel) => book.id,
|
17 | 18 | })
|
| 19 | + slice = indieSlice |
18 | 20 | })
|
19 | 21 |
|
20 | 22 | it('exposes oneAdded', () => {
|
21 |
| - const action = slice.actions.oneAdded(AClockworkOrange) |
22 |
| - const oneAdded = slice.reducer(undefined, action) |
23 |
| - expect(oneAdded.entities[AClockworkOrange.id]).toBe(AClockworkOrange) |
| 23 | + const book = { |
| 24 | + id: '0', |
| 25 | + title: 'Der Steppenwolf', |
| 26 | + author: 'Herman Hesse', |
| 27 | + } |
| 28 | + const action = slice.actions.oneAdded(book) |
| 29 | + const oneAdded = slice.reducer(undefined, action as UnknownAction) |
| 30 | + expect(oneAdded.entities['0']).toBe(book) |
24 | 31 | })
|
25 | 32 | })
|
26 | 33 |
|
27 |
| -interface EntitySliceArgs< |
28 |
| - T, |
29 |
| - Id extends EntityId, |
30 |
| - CaseReducers extends SliceCaseReducers<EntityState<T, Id>>, |
31 |
| -> { |
| 34 | +interface EntitySliceArgs<T, Id extends EntityId> { |
32 | 35 | name: string
|
33 | 36 | selectId: IdSelector<T, Id>
|
34 |
| - modelReducer?: ValidateSliceCaseReducers<EntityState<T, Id>, CaseReducers> |
| 37 | + modelReducer?: SliceCaseReducers<T> |
35 | 38 | }
|
36 | 39 |
|
37 |
| -function entitySliceEnhancer< |
38 |
| - T, |
39 |
| - Id extends EntityId, |
40 |
| - CaseReducers extends SliceCaseReducers<EntityState<T, Id>> = {}, |
41 |
| ->({ name, selectId, modelReducer }: EntitySliceArgs<T, Id, CaseReducers>) { |
| 40 | +function entitySliceEnhancer<T, Id extends EntityId>({ |
| 41 | + name, |
| 42 | + selectId, |
| 43 | + modelReducer, |
| 44 | +}: EntitySliceArgs<T, Id>) { |
42 | 45 | const modelAdapter = createEntityAdapter({
|
43 | 46 | selectId,
|
44 | 47 | })
|
|
0 commit comments