Debian Codegraph @

install/freeipa-webui/node_modules/@reduxjs/toolkit/src/tests/mapBuilders.test-d.ts

raw
1 import type { SerializedError } from '@internal/createAsyncThunk'
2 import { createAsyncThunk } from '@internal/createAsyncThunk'
3 import { executeReducerBuilderCallback } from '@internal/mapBuilders'
4 import type { UnknownAction } from '@reduxjs/toolkit'
5 import { createAction } from '@reduxjs/toolkit'
6
7 describe('type tests', () => {
8 test('builder callback for actionMap', () => {
9 const increment = createAction<number, 'increment'>('increment')
10
11 const decrement = createAction<number, 'decrement'>('decrement')
12
13 executeReducerBuilderCallback<number>((builder) => {
14 builder.addCase(increment, (state, action) => {
15 expectTypeOf(state).toBeNumber()
16
17 expectTypeOf(action).toEqualTypeOf<{
18 type: 'increment'
19 payload: number
20 }>()
21
22 expectTypeOf(state).not.toBeString()
23
24 expectTypeOf(action).not.toMatchTypeOf<{
25 type: 'increment'
26 payload: string
27 }>()
28
29 expectTypeOf(action).not.toMatchTypeOf<{
30 type: 'decrement'
31 payload: number
32 }>()
33 })
34
35 builder.addCase('increment', (state, action) => {
36 expectTypeOf(state).toBeNumber()
37
38 expectTypeOf(action).toEqualTypeOf<{ type: 'increment' }>()
39
40 expectTypeOf(state).not.toBeString()
41
42 expectTypeOf(action).not.toMatchTypeOf<{ type: 'decrement' }>()
43
44 // this cannot be inferred and has to be manually specified
45 expectTypeOf(action).not.toMatchTypeOf<{
46 type: 'increment'
47 payload: number
48 }>()
49 })
50
51 builder.addCase(
52 increment,
53 (state, action: ReturnType<typeof increment>) => state,
54 )
55
56 // @ts-expect-error
57 builder.addCase(
58 increment,
59 (state, action: ReturnType<typeof decrement>) => state,
60 )
61
62 builder.addCase(
63 'increment',
64 (state, action: ReturnType<typeof increment>) => state,
65 )
66
67 // @ts-expect-error
68 builder.addCase(
69 'decrement',
70 (state, action: ReturnType<typeof increment>) => state,
71 )
72
73 // action type is inferred
74 builder.addMatcher(increment.match, (state, action) => {
75 expectTypeOf(action).toEqualTypeOf<ReturnType<typeof increment>>()
76 })
77
78 test('action type is inferred when type predicate lacks `type` property', () => {
79 type PredicateWithoutTypeProperty = {
80 payload: number
81 }
82
83 builder.addMatcher(
84 (action): action is PredicateWithoutTypeProperty => true,
85 (state, action) => {
86 expectTypeOf(action).toMatchTypeOf<PredicateWithoutTypeProperty>()
87
88 expectTypeOf(action).toMatchTypeOf<UnknownAction>()
89 },
90 )
91 })
92
93 // action type defaults to UnknownAction if no type predicate matcher is passed
94 builder.addMatcher(
95 () => true,
96 (state, action) => {
97 expectTypeOf(action).toMatchTypeOf<UnknownAction>()
98 },
99 )
100
101 // with a boolean checker, action can also be typed by type argument
102 builder.addMatcher<{ foo: boolean }>(
103 () => true,
104 (state, action) => {
105 expectTypeOf(action).toMatchTypeOf<{ foo: boolean }>()
106
107 expectTypeOf(action).toMatchTypeOf<UnknownAction>()
108 },
109 )
110
111 // addCase().addMatcher() is possible, action type inferred correctly
112 builder
113 .addCase(
114 'increment',
115 (state, action: ReturnType<typeof increment>) => state,
116 )
117 .addMatcher(decrement.match, (state, action) => {
118 expectTypeOf(action).toEqualTypeOf<ReturnType<typeof decrement>>()
119 })
120
121 // addCase().addDefaultCase() is possible, action type is UnknownAction
122 builder
123 .addCase(
124 'increment',
125 (state, action: ReturnType<typeof increment>) => state,
126 )
127 .addDefaultCase((state, action) => {
128 expectTypeOf(action).toMatchTypeOf<UnknownAction>()
129 })
130
131 test('addMatcher() should prevent further calls to addCase()', () => {
132 const b = builder.addMatcher(increment.match, () => {})
133
134 expectTypeOf(b).not.toHaveProperty('addCase')
135
136 expectTypeOf(b.addMatcher).toBeCallableWith(increment.match, () => {})
137
138 expectTypeOf(b.addDefaultCase).toBeCallableWith(() => {})
139 })
140
141 test('addDefaultCase() should prevent further calls to addCase(), addMatcher() and addDefaultCase', () => {
142 const b = builder.addDefaultCase(() => {})
143
144 expectTypeOf(b).not.toHaveProperty('addCase')
145
146 expectTypeOf(b).not.toHaveProperty('addMatcher')
147
148 expectTypeOf(b).not.toHaveProperty('addDefaultCase')
149 })
150
151 describe('`createAsyncThunk` actions work with `mapBuilder`', () => {
152 test('case 1: normal `createAsyncThunk`', () => {
153 const thunk = createAsyncThunk('test', () => {
154 return 'ret' as const
155 })
156 builder.addCase(thunk.pending, (_, action) => {
157 expectTypeOf(action).toMatchTypeOf<{
158 payload: undefined
159 meta: {
160 arg: void
161 requestId: string
162 requestStatus: 'pending'
163 }
164 }>()
165 })
166
167 builder.addCase(thunk.rejected, (_, action) => {
168 expectTypeOf(action).toMatchTypeOf<{
169 payload: unknown
170 error: SerializedError
171 meta: {
172 arg: void
173 requestId: string
174 requestStatus: 'rejected'
175 aborted: boolean
176 condition: boolean
177 rejectedWithValue: boolean
178 }
179 }>()
180 })
181 builder.addCase(thunk.fulfilled, (_, action) => {
182 expectTypeOf(action).toMatchTypeOf<{
183 payload: 'ret'
184 meta: {
185 arg: void
186 requestId: string
187 requestStatus: 'fulfilled'
188 }
189 }>()
190 })
191 })
192 })
193
194 test('case 2: `createAsyncThunk` with `meta`', () => {
195 const thunk = createAsyncThunk<
196 'ret',
197 void,
198 {
199 pendingMeta: { startedTimeStamp: number }
200 fulfilledMeta: {
201 fulfilledTimeStamp: number
202 baseQueryMeta: 'meta!'
203 }
204 rejectedMeta: {
205 baseQueryMeta: 'meta!'
206 }
207 }
208 >(
209 'test',
210 (_, api) => {
211 return api.fulfillWithValue('ret' as const, {
212 fulfilledTimeStamp: 5,
213 baseQueryMeta: 'meta!',
214 })
215 },
216 {
217 getPendingMeta() {
218 return { startedTimeStamp: 0 }
219 },
220 },
221 )
222
223 builder.addCase(thunk.pending, (_, action) => {
224 expectTypeOf(action).toMatchTypeOf<{
225 payload: undefined
226 meta: {
227 arg: void
228 requestId: string
229 requestStatus: 'pending'
230 startedTimeStamp: number
231 }
232 }>()
233 })
234
235 builder.addCase(thunk.rejected, (_, action) => {
236 expectTypeOf(action).toMatchTypeOf<{
237 payload: unknown
238 error: SerializedError
239 meta: {
240 arg: void
241 requestId: string
242 requestStatus: 'rejected'
243 aborted: boolean
244 condition: boolean
245 rejectedWithValue: boolean
246 baseQueryMeta?: 'meta!'
247 }
248 }>()
249
250 if (action.meta.rejectedWithValue) {
251 expectTypeOf(action.meta.baseQueryMeta).toEqualTypeOf<'meta!'>()
252 }
253 })
254 builder.addCase(thunk.fulfilled, (_, action) => {
255 expectTypeOf(action).toMatchTypeOf<{
256 payload: 'ret'
257 meta: {
258 arg: void
259 requestId: string
260 requestStatus: 'fulfilled'
261 baseQueryMeta: 'meta!'
262 }
263 }>()
264 })
265 })
266 })
267 })
268 })