| 1 |
import type { |
| 2 |
ActionCreatorWithPayload, |
| 3 |
ActionCreatorWithoutPayload, |
| 4 |
EntityAdapter, |
| 5 |
EntityId, |
| 6 |
EntityStateAdapter, |
| 7 |
Update, |
| 8 |
} from '@reduxjs/toolkit' |
| 9 |
import { createEntityAdapter, createSlice } from '@reduxjs/toolkit' |
| 10 |
|
| 11 |
function extractReducers<T, Id extends EntityId>( |
| 12 |
adapter: EntityAdapter<T, Id>, |
| 13 |
): EntityStateAdapter<T, Id> { |
| 14 |
const { selectId, sortComparer, getInitialState, getSelectors, ...rest } = |
| 15 |
adapter |
| 16 |
return rest |
| 17 |
} |
| 18 |
|
| 19 |
describe('type tests', () => { |
| 20 |
test('should be usable in a slice, with all the "reducer-like" functions', () => { |
| 21 |
type Id = string & { readonly __tag: unique symbol } |
| 22 |
type Entity = { |
| 23 |
id: Id |
| 24 |
} |
| 25 |
const adapter = createEntityAdapter<Entity>() |
| 26 |
const slice = createSlice({ |
| 27 |
name: 'test', |
| 28 |
initialState: adapter.getInitialState(), |
| 29 |
reducers: { |
| 30 |
...extractReducers(adapter), |
| 31 |
}, |
| 32 |
}) |
| 33 |
|
| 34 |
expectTypeOf(slice.actions.addOne).toMatchTypeOf< |
| 35 |
ActionCreatorWithPayload<Entity> |
| 36 |
>() |
| 37 |
|
| 38 |
expectTypeOf(slice.actions.addMany).toMatchTypeOf< |
| 39 |
ActionCreatorWithPayload<ReadonlyArray<Entity> | Record<string, Entity>> |
| 40 |
>() |
| 41 |
|
| 42 |
expectTypeOf(slice.actions.setAll).toMatchTypeOf< |
| 43 |
ActionCreatorWithPayload<ReadonlyArray<Entity> | Record<string, Entity>> |
| 44 |
>() |
| 45 |
|
| 46 |
expectTypeOf(slice.actions.removeOne).toMatchTypeOf< |
| 47 |
ActionCreatorWithPayload<Id> |
| 48 |
>() |
| 49 |
|
| 50 |
expectTypeOf(slice.actions.addMany).not.toMatchTypeOf< |
| 51 |
ActionCreatorWithPayload<Entity[] | Record<string, Entity>> |
| 52 |
>() |
| 53 |
|
| 54 |
expectTypeOf(slice.actions.setAll).not.toMatchTypeOf< |
| 55 |
ActionCreatorWithPayload<ReadonlyArray<Id>> |
| 56 |
>() |
| 57 |
|
| 58 |
expectTypeOf(slice.actions.removeOne).toMatchTypeOf< |
| 59 |
ActionCreatorWithPayload<Id> |
| 60 |
>() |
| 61 |
|
| 62 |
expectTypeOf(slice.actions.removeMany).toMatchTypeOf< |
| 63 |
ActionCreatorWithPayload<ReadonlyArray<Id>> |
| 64 |
>() |
| 65 |
|
| 66 |
expectTypeOf(slice.actions.removeMany).not.toMatchTypeOf< |
| 67 |
ActionCreatorWithPayload<EntityId[]> |
| 68 |
>() |
| 69 |
|
| 70 |
expectTypeOf( |
| 71 |
slice.actions.removeAll, |
| 72 |
).toMatchTypeOf<ActionCreatorWithoutPayload>() |
| 73 |
|
| 74 |
expectTypeOf(slice.actions.updateOne).toMatchTypeOf< |
| 75 |
ActionCreatorWithPayload<Update<Entity, Id>> |
| 76 |
>() |
| 77 |
|
| 78 |
expectTypeOf(slice.actions.updateMany).not.toMatchTypeOf< |
| 79 |
ActionCreatorWithPayload<Update<Entity, Id>[]> |
| 80 |
>() |
| 81 |
|
| 82 |
expectTypeOf(slice.actions.upsertOne).toMatchTypeOf< |
| 83 |
ActionCreatorWithPayload<Entity> |
| 84 |
>() |
| 85 |
|
| 86 |
expectTypeOf(slice.actions.updateMany).toMatchTypeOf< |
| 87 |
ActionCreatorWithPayload<ReadonlyArray<Update<Entity, Id>>> |
| 88 |
>() |
| 89 |
|
| 90 |
expectTypeOf(slice.actions.upsertOne).toMatchTypeOf< |
| 91 |
ActionCreatorWithPayload<Entity> |
| 92 |
>() |
| 93 |
|
| 94 |
expectTypeOf(slice.actions.upsertMany).toMatchTypeOf< |
| 95 |
ActionCreatorWithPayload<ReadonlyArray<Entity> | Record<string, Entity>> |
| 96 |
>() |
| 97 |
|
| 98 |
expectTypeOf(slice.actions.upsertMany).not.toMatchTypeOf< |
| 99 |
ActionCreatorWithPayload<Entity[] | Record<string, Entity>> |
| 100 |
>() |
| 101 |
}) |
| 102 |
|
| 103 |
test('should not be able to mix with a different EntityAdapter', () => { |
| 104 |
type Entity = { |
| 105 |
id: EntityId |
| 106 |
value: string |
| 107 |
} |
| 108 |
type Entity2 = { |
| 109 |
id: EntityId |
| 110 |
value2: string |
| 111 |
} |
| 112 |
const adapter = createEntityAdapter<Entity>() |
| 113 |
const adapter2 = createEntityAdapter<Entity2>() |
| 114 |
createSlice({ |
| 115 |
name: 'test', |
| 116 |
initialState: adapter.getInitialState(), |
| 117 |
reducers: { |
| 118 |
addOne: adapter.addOne, |
| 119 |
// @ts-expect-error |
| 120 |
addOne2: adapter2.addOne, |
| 121 |
}, |
| 122 |
}) |
| 123 |
}) |
| 124 |
|
| 125 |
test('should be usable in a slice with extra properties', () => { |
| 126 |
type Entity = { id: EntityId; value: string } |
| 127 |
const adapter = createEntityAdapter<Entity>() |
| 128 |
createSlice({ |
| 129 |
name: 'test', |
| 130 |
initialState: adapter.getInitialState({ extraData: 'test' }), |
| 131 |
reducers: { |
| 132 |
addOne: adapter.addOne, |
| 133 |
}, |
| 134 |
}) |
| 135 |
}) |
| 136 |
|
| 137 |
test('should not be usable in a slice with an unfitting state', () => { |
| 138 |
type Entity = { id: EntityId; value: string } |
| 139 |
const adapter = createEntityAdapter<Entity>() |
| 140 |
createSlice({ |
| 141 |
name: 'test', |
| 142 |
initialState: { somethingElse: '' }, |
| 143 |
reducers: { |
| 144 |
// @ts-expect-error |
| 145 |
addOne: adapter.addOne, |
| 146 |
}, |
| 147 |
}) |
| 148 |
}) |
| 149 |
|
| 150 |
test('should not be able to create an adapter unless the type has an Id or an idSelector is provided', () => { |
| 151 |
type Entity = { |
| 152 |
value: string |
| 153 |
} |
| 154 |
// @ts-expect-error |
| 155 |
const adapter = createEntityAdapter<Entity>() |
| 156 |
const adapter2: EntityAdapter<Entity, Entity['value']> = |
| 157 |
createEntityAdapter({ |
| 158 |
selectId: (e: Entity) => e.value, |
| 159 |
}) |
| 160 |
}) |
| 161 |
}) |