File size: 8,022 Bytes
c2b7eb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import type {
  Action,
  Reducer,
  Slice,
  WithSlice,
  WithSlicePreloadedState,
} from '@reduxjs/toolkit'
import { combineSlices } from '@reduxjs/toolkit'
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query'

declare const stringSlice: Slice<string, {}, 'string'>

declare const numberSlice: Slice<number, {}, 'number'>

declare const booleanReducer: Reducer<boolean>

declare const mixedReducer: Reducer<string, Action, number>

declare const mixedSliceLike: {
  reducerPath: 'mixedSlice'
  reducer: typeof mixedReducer
}

const exampleApi = createApi({
  baseQuery: fetchBaseQuery(),
  endpoints: (build) => ({
    getThing: build.query({
      query: () => '',
    }),
  }),
})

type ExampleApiState = ReturnType<typeof exampleApi.reducer>

describe('type tests', () => {
  test('combineSlices correctly combines static state', () => {
    const rootReducer = combineSlices(
      stringSlice,
      numberSlice,
      exampleApi,
      {
        boolean: booleanReducer,
        mixed: mixedReducer,
      },
      mixedSliceLike,
    )

    expectTypeOf(rootReducer(undefined, { type: '' })).toEqualTypeOf<{
      string: string
      number: number
      boolean: boolean
      api: ExampleApiState
      mixed: string
      mixedSlice: string
    }>()

    // test for correct preloaded state handling
    expectTypeOf(rootReducer).toBeCallableWith(
      { mixed: 9, mixedSlice: 9 },
      { type: '' },
    )
  })

  test('combineSlices allows passing no initial reducers', () => {
    const rootReducer = combineSlices()

    expectTypeOf(rootReducer(undefined, { type: '' })).toEqualTypeOf<{}>()

    const declaredLazy =
      combineSlices().withLazyLoadedSlices<WithSlice<typeof numberSlice>>()

    expectTypeOf(declaredLazy(undefined, { type: '' })).toEqualTypeOf<{
      number?: number
    }>()
  })

  test('withLazyLoadedSlices adds partial to state', () => {
    const rootReducer = combineSlices(stringSlice).withLazyLoadedSlices<
      WithSlice<typeof numberSlice> & WithSlice<typeof exampleApi>
    >()

    expectTypeOf(rootReducer(undefined, { type: '' }).number).toEqualTypeOf<
      number | undefined
    >()

    expectTypeOf(rootReducer(undefined, { type: '' }).api).toEqualTypeOf<
      ExampleApiState | undefined
    >()
  })

  test('inject marks injected keys as required', () => {
    const rootReducer = combineSlices(stringSlice).withLazyLoadedSlices<
      WithSlice<typeof numberSlice> &
        WithSlice<typeof exampleApi> & { boolean: boolean } & WithSlice<
          typeof mixedSliceLike
        > &
        WithSlice<{
          reducerPath: 'mixedReducer'
          reducer: typeof mixedReducer
        }>,
      WithSlicePreloadedState<typeof numberSlice> &
        WithSlicePreloadedState<typeof exampleApi> & {
          boolean: boolean
        } & WithSlicePreloadedState<typeof mixedSliceLike> &
        WithSlicePreloadedState<{
          reducerPath: 'mixedReducer'
          reducer: typeof mixedReducer
        }>
    >()

    expectTypeOf(rootReducer(undefined, { type: '' }).number).toEqualTypeOf<
      number | undefined
    >()

    expectTypeOf(rootReducer(undefined, { type: '' }).boolean).toEqualTypeOf<
      boolean | undefined
    >()

    expectTypeOf(rootReducer(undefined, { type: '' }).api).toEqualTypeOf<
      ExampleApiState | undefined
    >()

    expectTypeOf(rootReducer(undefined, { type: '' }).mixedSlice).toEqualTypeOf<
      string | undefined
    >()

    expectTypeOf(
      rootReducer(undefined, { type: '' }).mixedReducer,
    ).toEqualTypeOf<string | undefined>()

    const withNumber = rootReducer.inject(numberSlice)

    expectTypeOf(withNumber(undefined, { type: '' }).number).toBeNumber()

    const withBool = rootReducer.inject({
      reducerPath: 'boolean' as const,
      reducer: booleanReducer,
    })

    expectTypeOf(withBool(undefined, { type: '' }).boolean).toBeBoolean()

    const withApi = rootReducer.inject(exampleApi)

    expectTypeOf(
      withApi(undefined, { type: '' }).api,
    ).toEqualTypeOf<ExampleApiState>()

    const withMixedSlice = rootReducer.inject(mixedSliceLike)

    expectTypeOf(
      withMixedSlice(undefined, { type: '' }).mixedSlice,
    ).toBeString()

    const withMixedReducer = rootReducer.inject({
      reducerPath: 'mixedReducer',
      reducer: mixedReducer,
    })

    expectTypeOf(
      withMixedReducer(undefined, { type: '' }).mixedReducer,
    ).toBeString()
  })

  test('selector() allows defining selectors with injected reducers defined', () => {
    const rootReducer = combineSlices(stringSlice).withLazyLoadedSlices<
      WithSlice<typeof numberSlice> & { boolean: boolean }
    >()

    type RootState = ReturnType<typeof rootReducer>

    const withoutInjection = rootReducer.selector(
      (state: RootState) => state.number,
    )

    expectTypeOf(
      withoutInjection(rootReducer(undefined, { type: '' })),
    ).toEqualTypeOf<number | undefined>()

    const withInjection = rootReducer
      .inject(numberSlice)
      .selector((state) => state.number)

    expectTypeOf(
      withInjection(rootReducer(undefined, { type: '' })),
    ).toBeNumber()
  })

  test('selector() passes arguments through', () => {
    const rootReducer = combineSlices(stringSlice).withLazyLoadedSlices<
      WithSlice<typeof numberSlice> & { boolean: boolean }
    >()

    const selector = rootReducer
      .inject(numberSlice)
      .selector((state, num: number) => state.number)

    const state = rootReducer(undefined, { type: '' })

    expectTypeOf(selector).toBeCallableWith(state, 0)

    // required argument
    expectTypeOf(selector).parameters.not.toMatchTypeOf([state])

    // number not string
    expectTypeOf(selector).parameters.not.toMatchTypeOf([state, ''])
  })

  test('nested calls inferred correctly', () => {
    const innerReducer =
      combineSlices(stringSlice).withLazyLoadedSlices<
        WithSlice<typeof numberSlice>
      >()

    const innerSelector = innerReducer.inject(numberSlice).selector(
      (state) => state.number,
      (rootState: RootState) => rootState.inner,
    )

    const outerReducer = combineSlices({ inner: innerReducer })

    type RootState = ReturnType<typeof outerReducer>

    expectTypeOf(outerReducer(undefined, { type: '' })).toMatchTypeOf<{
      inner: { string: string }
    }>()

    expectTypeOf(
      innerSelector(outerReducer(undefined, { type: '' })),
    ).toBeNumber()
  })

  test('selector errors if selectorFn and selectState are mismatched', () => {
    const combinedReducer =
      combineSlices(stringSlice).withLazyLoadedSlices<
        WithSlice<typeof numberSlice>
      >()

    const outerReducer = combineSlices({ inner: combinedReducer })

    type RootState = ReturnType<typeof outerReducer>

    combinedReducer.selector(
      (state) => state.number,
      // @ts-expect-error wrong state returned
      (rootState: RootState) => rootState.inner.number,
    )

    combinedReducer.selector(
      (state, num: number) => state.number,
      // @ts-expect-error wrong arguments
      (rootState: RootState, str: string) => rootState.inner,
    )

    combinedReducer.selector(
      (state, num: number) => state.number,
      (rootState: RootState) => rootState.inner,
    )

    // TODO: see if there's a way of making this work
    // probably a rare case so not the end of the world if not
    combinedReducer.selector(
      (state) => state.number,
      // @ts-ignore
      (rootState: RootState, num: number) => rootState.inner,
    )
  })

  test('correct type of state is inferred when not declared via `withLazyLoadedSlices`', () => {
    // Related to https://github.com/reduxjs/redux-toolkit/issues/4171

    const combinedReducer = combineSlices(stringSlice)

    const withNumber = combinedReducer.inject(numberSlice)

    expectTypeOf(withNumber).returns.toEqualTypeOf<{
      string: string
      number: number
    }>()

    expectTypeOf(
      withNumber(undefined, { type: '' }).number,
    ).toMatchTypeOf<number>()
  })
})