| 1 |
import type { Reducer, Slice, WithSlice } from '@reduxjs/toolkit' |
| 2 |
import { combineSlices } from '@reduxjs/toolkit' |
| 3 |
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query' |
| 4 |
|
| 5 |
declare const stringSlice: Slice<string, {}, 'string'> |
| 6 |
|
| 7 |
declare const numberSlice: Slice<number, {}, 'number'> |
| 8 |
|
| 9 |
declare const booleanReducer: Reducer<boolean> |
| 10 |
|
| 11 |
const exampleApi = createApi({ |
| 12 |
baseQuery: fetchBaseQuery(), |
| 13 |
endpoints: (build) => ({ |
| 14 |
getThing: build.query({ |
| 15 |
query: () => '', |
| 16 |
}), |
| 17 |
}), |
| 18 |
}) |
| 19 |
|
| 20 |
type ExampleApiState = ReturnType<typeof exampleApi.reducer> |
| 21 |
|
| 22 |
describe('type tests', () => { |
| 23 |
test('combineSlices correctly combines static state', () => { |
| 24 |
const rootReducer = combineSlices(stringSlice, numberSlice, exampleApi, { |
| 25 |
boolean: booleanReducer, |
| 26 |
}) |
| 27 |
|
| 28 |
expectTypeOf(rootReducer(undefined, { type: '' })).toEqualTypeOf<{ |
| 29 |
string: string |
| 30 |
number: number |
| 31 |
boolean: boolean |
| 32 |
api: ExampleApiState |
| 33 |
}>() |
| 34 |
}) |
| 35 |
|
| 36 |
test('combineSlices allows passing no initial reducers', () => { |
| 37 |
const rootReducer = combineSlices() |
| 38 |
|
| 39 |
expectTypeOf(rootReducer(undefined, { type: '' })).toEqualTypeOf<{}>() |
| 40 |
|
| 41 |
const declaredLazy = |
| 42 |
combineSlices().withLazyLoadedSlices<WithSlice<typeof numberSlice>>() |
| 43 |
|
| 44 |
expectTypeOf(declaredLazy(undefined, { type: '' })).toEqualTypeOf<{ |
| 45 |
number?: number |
| 46 |
}>() |
| 47 |
}) |
| 48 |
|
| 49 |
test('withLazyLoadedSlices adds partial to state', () => { |
| 50 |
const rootReducer = combineSlices(stringSlice).withLazyLoadedSlices< |
| 51 |
WithSlice<typeof numberSlice> & WithSlice<typeof exampleApi> |
| 52 |
>() |
| 53 |
|
| 54 |
expectTypeOf(rootReducer(undefined, { type: '' }).number).toEqualTypeOf< |
| 55 |
number | undefined |
| 56 |
>() |
| 57 |
|
| 58 |
expectTypeOf(rootReducer(undefined, { type: '' }).api).toEqualTypeOf< |
| 59 |
ExampleApiState | undefined |
| 60 |
>() |
| 61 |
}) |
| 62 |
|
| 63 |
test('inject marks injected keys as required', () => { |
| 64 |
const rootReducer = combineSlices(stringSlice).withLazyLoadedSlices< |
| 65 |
WithSlice<typeof numberSlice> & |
| 66 |
WithSlice<typeof exampleApi> & { boolean: boolean } |
| 67 |
>() |
| 68 |
|
| 69 |
expectTypeOf(rootReducer(undefined, { type: '' }).number).toEqualTypeOf< |
| 70 |
number | undefined |
| 71 |
>() |
| 72 |
|
| 73 |
expectTypeOf(rootReducer(undefined, { type: '' }).boolean).toEqualTypeOf< |
| 74 |
boolean | undefined |
| 75 |
>() |
| 76 |
|
| 77 |
expectTypeOf(rootReducer(undefined, { type: '' }).api).toEqualTypeOf< |
| 78 |
ExampleApiState | undefined |
| 79 |
>() |
| 80 |
|
| 81 |
const withNumber = rootReducer.inject(numberSlice) |
| 82 |
|
| 83 |
expectTypeOf(withNumber(undefined, { type: '' }).number).toBeNumber() |
| 84 |
|
| 85 |
const withBool = rootReducer.inject({ |
| 86 |
reducerPath: 'boolean' as const, |
| 87 |
reducer: booleanReducer, |
| 88 |
}) |
| 89 |
|
| 90 |
expectTypeOf(withBool(undefined, { type: '' }).boolean).toBeBoolean() |
| 91 |
|
| 92 |
const withApi = rootReducer.inject(exampleApi) |
| 93 |
|
| 94 |
expectTypeOf( |
| 95 |
withApi(undefined, { type: '' }).api, |
| 96 |
).toEqualTypeOf<ExampleApiState>() |
| 97 |
}) |
| 98 |
|
| 99 |
test('selector() allows defining selectors with injected reducers defined', () => { |
| 100 |
const rootReducer = combineSlices(stringSlice).withLazyLoadedSlices< |
| 101 |
WithSlice<typeof numberSlice> & { boolean: boolean } |
| 102 |
>() |
| 103 |
|
| 104 |
type RootState = ReturnType<typeof rootReducer> |
| 105 |
|
| 106 |
const withoutInjection = rootReducer.selector( |
| 107 |
(state: RootState) => state.number, |
| 108 |
) |
| 109 |
|
| 110 |
expectTypeOf( |
| 111 |
withoutInjection(rootReducer(undefined, { type: '' })), |
| 112 |
).toEqualTypeOf<number | undefined>() |
| 113 |
|
| 114 |
const withInjection = rootReducer |
| 115 |
.inject(numberSlice) |
| 116 |
.selector((state) => state.number) |
| 117 |
|
| 118 |
expectTypeOf( |
| 119 |
withInjection(rootReducer(undefined, { type: '' })), |
| 120 |
).toBeNumber() |
| 121 |
}) |
| 122 |
|
| 123 |
test('selector() passes arguments through', () => { |
| 124 |
const rootReducer = combineSlices(stringSlice).withLazyLoadedSlices< |
| 125 |
WithSlice<typeof numberSlice> & { boolean: boolean } |
| 126 |
>() |
| 127 |
|
| 128 |
const selector = rootReducer |
| 129 |
.inject(numberSlice) |
| 130 |
.selector((state, num: number) => state.number) |
| 131 |
|
| 132 |
const state = rootReducer(undefined, { type: '' }) |
| 133 |
|
| 134 |
expectTypeOf(selector).toBeCallableWith(state, 0) |
| 135 |
|
| 136 |
// required argument |
| 137 |
expectTypeOf(selector).parameters.not.toMatchTypeOf([state]) |
| 138 |
|
| 139 |
// number not string |
| 140 |
expectTypeOf(selector).parameters.not.toMatchTypeOf([state, '']) |
| 141 |
}) |
| 142 |
|
| 143 |
test('nested calls inferred correctly', () => { |
| 144 |
const innerReducer = |
| 145 |
combineSlices(stringSlice).withLazyLoadedSlices< |
| 146 |
WithSlice<typeof numberSlice> |
| 147 |
>() |
| 148 |
|
| 149 |
const innerSelector = innerReducer.inject(numberSlice).selector( |
| 150 |
(state) => state.number, |
| 151 |
(rootState: RootState) => rootState.inner, |
| 152 |
) |
| 153 |
|
| 154 |
const outerReducer = combineSlices({ inner: innerReducer }) |
| 155 |
|
| 156 |
type RootState = ReturnType<typeof outerReducer> |
| 157 |
|
| 158 |
expectTypeOf(outerReducer(undefined, { type: '' })).toMatchTypeOf<{ |
| 159 |
inner: { string: string } |
| 160 |
}>() |
| 161 |
|
| 162 |
expectTypeOf( |
| 163 |
innerSelector(outerReducer(undefined, { type: '' })), |
| 164 |
).toBeNumber() |
| 165 |
}) |
| 166 |
|
| 167 |
test('selector errors if selectorFn and selectState are mismatched', () => { |
| 168 |
const combinedReducer = |
| 169 |
combineSlices(stringSlice).withLazyLoadedSlices< |
| 170 |
WithSlice<typeof numberSlice> |
| 171 |
>() |
| 172 |
|
| 173 |
const outerReducer = combineSlices({ inner: combinedReducer }) |
| 174 |
|
| 175 |
type RootState = ReturnType<typeof outerReducer> |
| 176 |
|
| 177 |
combinedReducer.selector( |
| 178 |
(state) => state.number, |
| 179 |
// @ts-expect-error wrong state returned |
| 180 |
(rootState: RootState) => rootState.inner.number, |
| 181 |
) |
| 182 |
|
| 183 |
combinedReducer.selector( |
| 184 |
(state, num: number) => state.number, |
| 185 |
// @ts-expect-error wrong arguments |
| 186 |
(rootState: RootState, str: string) => rootState.inner, |
| 187 |
) |
| 188 |
|
| 189 |
combinedReducer.selector( |
| 190 |
(state, num: number) => state.number, |
| 191 |
(rootState: RootState) => rootState.inner, |
| 192 |
) |
| 193 |
|
| 194 |
// TODO: see if there's a way of making this work |
| 195 |
// probably a rare case so not the end of the world if not |
| 196 |
combinedReducer.selector( |
| 197 |
(state) => state.number, |
| 198 |
// @ts-ignore |
| 199 |
(rootState: RootState, num: number) => rootState.inner, |
| 200 |
) |
| 201 |
}) |
| 202 |
|
| 203 |
test('correct type of state is inferred when not declared via `withLazyLoadedSlices`', () => { |
| 204 |
// Related to https://github.com/reduxjs/redux-toolkit/issues/4171 |
| 205 |
|
| 206 |
const combinedReducer = combineSlices(stringSlice) |
| 207 |
|
| 208 |
const withNumber = combinedReducer.inject(numberSlice) |
| 209 |
|
| 210 |
expectTypeOf(withNumber).returns.toEqualTypeOf<{ |
| 211 |
string: string |
| 212 |
number: number |
| 213 |
}>() |
| 214 |
|
| 215 |
expectTypeOf( |
| 216 |
withNumber(undefined, { type: '' }).number, |
| 217 |
).toMatchTypeOf<number>() |
| 218 |
}) |
| 219 |
}) |