Debian Codegraph @

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

raw
1 import { setupApiStore } from '@internal/tests/utils/helpers'
2 import type { SerializedError } from '@reduxjs/toolkit'
3 import { configureStore } from '@reduxjs/toolkit'
4 import type {
5 DefinitionsFromApi,
6 FetchBaseQueryError,
7 MutationDefinition,
8 OverrideResultType,
9 QueryDefinition,
10 TagDescription,
11 TagTypesFromApi,
12 } from '@reduxjs/toolkit/query'
13 import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query'
14
15 describe('type tests', () => {
16 test('sensible defaults', () => {
17 const api = createApi({
18 baseQuery: fetchBaseQuery(),
19 endpoints: (build) => ({
20 getUser: build.query<unknown, void>({
21 query(id) {
22 return { url: `user/${id}` }
23 },
24 }),
25 updateUser: build.mutation<unknown, void>({
26 query: () => '',
27 }),
28 }),
29 })
30
31 configureStore({
32 reducer: {
33 [api.reducerPath]: api.reducer,
34 },
35 middleware: (gDM) => gDM().concat(api.middleware),
36 })
37
38 expectTypeOf(api.reducerPath).toEqualTypeOf<'api'>()
39
40 expectTypeOf(api.util.invalidateTags)
41 .parameter(0)
42 .toEqualTypeOf<(null | undefined | TagDescription<never>)[]>()
43 })
44
45 describe('endpoint definition typings', () => {
46 const api = createApi({
47 baseQuery: (from: 'From'): { data: 'To' } | Promise<{ data: 'To' }> => ({
48 data: 'To',
49 }),
50 endpoints: () => ({}),
51 tagTypes: ['typeA', 'typeB'],
52 })
53
54 test('query: query & transformResponse types', () => {
55 api.injectEndpoints({
56 endpoints: (build) => ({
57 query: build.query<'RetVal', 'Arg'>({
58 query: (x: 'Arg') => 'From' as const,
59 transformResponse(r: 'To') {
60 return 'RetVal' as const
61 },
62 }),
63 query1: build.query<'RetVal', 'Arg'>({
64 // @ts-expect-error
65 query: (x: 'Error') => 'From' as const,
66 transformResponse(r: 'To') {
67 return 'RetVal' as const
68 },
69 }),
70 query2: build.query<'RetVal', 'Arg'>({
71 // @ts-expect-error
72 query: (x: 'Arg') => 'Error' as const,
73 transformResponse(r: 'To') {
74 return 'RetVal' as const
75 },
76 }),
77 query3: build.query<'RetVal', 'Arg'>({
78 query: (x: 'Arg') => 'From' as const,
79 // @ts-expect-error
80 transformResponse(r: 'Error') {
81 return 'RetVal' as const
82 },
83 }),
84 query4: build.query<'RetVal', 'Arg'>({
85 query: (x: 'Arg') => 'From' as const,
86 // @ts-expect-error
87 transformResponse(r: 'To') {
88 return 'Error' as const
89 },
90 }),
91 queryInference1: build.query<'RetVal', 'Arg'>({
92 query: (x) => {
93 expectTypeOf(x).toEqualTypeOf<'Arg'>()
94
95 return 'From'
96 },
97 transformResponse(r) {
98 expectTypeOf(r).toEqualTypeOf<'To'>()
99
100 return 'RetVal'
101 },
102 }),
103 queryInference2: (() => {
104 const query = build.query({
105 query: (x: 'Arg') => 'From' as const,
106 transformResponse(r: 'To') {
107 return 'RetVal' as const
108 },
109 })
110
111 expectTypeOf(query).toMatchTypeOf<
112 QueryDefinition<'Arg', any, any, 'RetVal'>
113 >()
114
115 return query
116 })(),
117 }),
118 })
119 })
120
121 test('mutation: query & transformResponse types', () => {
122 api.injectEndpoints({
123 endpoints: (build) => ({
124 query: build.mutation<'RetVal', 'Arg'>({
125 query: (x: 'Arg') => 'From' as const,
126 transformResponse(r: 'To') {
127 return 'RetVal' as const
128 },
129 }),
130 query1: build.mutation<'RetVal', 'Arg'>({
131 // @ts-expect-error
132 query: (x: 'Error') => 'From' as const,
133 transformResponse(r: 'To') {
134 return 'RetVal' as const
135 },
136 }),
137 query2: build.mutation<'RetVal', 'Arg'>({
138 // @ts-expect-error
139 query: (x: 'Arg') => 'Error' as const,
140 transformResponse(r: 'To') {
141 return 'RetVal' as const
142 },
143 }),
144 query3: build.mutation<'RetVal', 'Arg'>({
145 query: (x: 'Arg') => 'From' as const,
146 // @ts-expect-error
147 transformResponse(r: 'Error') {
148 return 'RetVal' as const
149 },
150 }),
151 query4: build.mutation<'RetVal', 'Arg'>({
152 query: (x: 'Arg') => 'From' as const,
153 // @ts-expect-error
154 transformResponse(r: 'To') {
155 return 'Error' as const
156 },
157 }),
158 mutationInference1: build.mutation<'RetVal', 'Arg'>({
159 query: (x) => {
160 expectTypeOf(x).toEqualTypeOf<'Arg'>()
161
162 return 'From'
163 },
164 transformResponse(r) {
165 expectTypeOf(r).toEqualTypeOf<'To'>()
166
167 return 'RetVal'
168 },
169 }),
170 mutationInference2: (() => {
171 const query = build.mutation({
172 query: (x: 'Arg') => 'From' as const,
173 transformResponse(r: 'To') {
174 return 'RetVal' as const
175 },
176 })
177
178 expectTypeOf(query).toMatchTypeOf<
179 MutationDefinition<'Arg', any, any, 'RetVal'>
180 >()
181
182 return query
183 })(),
184 }),
185 })
186 })
187
188 describe('enhancing endpoint definitions', () => {
189 const baseQuery = (x: string) => ({ data: 'success' })
190
191 function getNewApi() {
192 return createApi({
193 baseQuery,
194 tagTypes: ['old'],
195 endpoints: (build) => ({
196 query1: build.query<'out1', 'in1'>({ query: (id) => `${id}` }),
197 query2: build.query<'out2', 'in2'>({ query: (id) => `${id}` }),
198 mutation1: build.mutation<'out1', 'in1'>({
199 query: (id) => `${id}`,
200 }),
201 mutation2: build.mutation<'out2', 'in2'>({
202 query: (id) => `${id}`,
203 }),
204 }),
205 })
206 }
207
208 const api1 = getNewApi()
209
210 test('warn on wrong tagType', () => {
211 const storeRef = setupApiStore(api1, undefined, {
212 withoutTestLifecycles: true,
213 })
214
215 api1.enhanceEndpoints({
216 endpoints: {
217 query1: {
218 // @ts-expect-error
219 providesTags: ['new'],
220 },
221 query2: {
222 // @ts-expect-error
223 providesTags: ['missing'],
224 },
225 },
226 })
227
228 const enhanced = api1.enhanceEndpoints({
229 addTagTypes: ['new'],
230 endpoints: {
231 query1: {
232 providesTags: ['new'],
233 },
234 query2: {
235 // @ts-expect-error
236 providesTags: ['missing'],
237 },
238 },
239 })
240
241 storeRef.store.dispatch(api1.endpoints.query1.initiate('in1'))
242
243 storeRef.store.dispatch(api1.endpoints.query2.initiate('in2'))
244
245 enhanced.enhanceEndpoints({
246 endpoints: {
247 query1: {
248 // returned `enhanced` api contains "new" entityType
249 providesTags: ['new'],
250 },
251 query2: {
252 // @ts-expect-error
253 providesTags: ['missing'],
254 },
255 },
256 })
257 })
258
259 test('modify', () => {
260 const storeRef = setupApiStore(api1, undefined, {
261 withoutTestLifecycles: true,
262 })
263
264 api1.enhanceEndpoints({
265 endpoints: {
266 query1: {
267 query: (x) => {
268 expectTypeOf(x).toEqualTypeOf<'in1'>()
269
270 return 'modified1'
271 },
272 },
273 query2(definition) {
274 definition.query = (x) => {
275 expectTypeOf(x).toEqualTypeOf<'in2'>()
276
277 return 'modified2'
278 }
279 },
280 mutation1: {
281 query: (x) => {
282 expectTypeOf(x).toEqualTypeOf<'in1'>()
283
284 return 'modified1'
285 },
286 },
287 mutation2(definition) {
288 definition.query = (x) => {
289 expectTypeOf(x).toEqualTypeOf<'in2'>()
290
291 return 'modified2'
292 }
293 },
294 // @ts-expect-error
295 nonExisting: {},
296 },
297 })
298
299 storeRef.store.dispatch(api1.endpoints.query1.initiate('in1'))
300 storeRef.store.dispatch(api1.endpoints.query2.initiate('in2'))
301 storeRef.store.dispatch(api1.endpoints.mutation1.initiate('in1'))
302 storeRef.store.dispatch(api1.endpoints.mutation2.initiate('in2'))
303 })
304
305 test('updated transform response types', async () => {
306 const baseApi = createApi({
307 baseQuery: fetchBaseQuery({ baseUrl: 'https://example.com' }),
308 tagTypes: ['old'],
309 endpoints: (build) => ({
310 query1: build.query<'out1', void>({ query: () => 'success' }),
311 mutation1: build.mutation<'out1', void>({ query: () => 'success' }),
312 }),
313 })
314
315 type Transformed = { value: string }
316
317 type Definitions = DefinitionsFromApi<typeof api1>
318
319 type TagTypes = TagTypesFromApi<typeof api1>
320
321 type Q1Definition = OverrideResultType<
322 Definitions['query1'],
323 Transformed
324 >
325
326 type M1Definition = OverrideResultType<
327 Definitions['mutation1'],
328 Transformed
329 >
330
331 type UpdatedDefinitions = Omit<Definitions, 'query1' | 'mutation1'> & {
332 query1: Q1Definition
333 mutation1: M1Definition
334 }
335
336 const enhancedApi = baseApi.enhanceEndpoints<
337 TagTypes,
338 UpdatedDefinitions
339 >({
340 endpoints: {
341 query1: {
342 transformResponse: (a, b, c) => ({
343 value: 'transformed',
344 }),
345 },
346 mutation1: {
347 transformResponse: (a, b, c) => ({
348 value: 'transformed',
349 }),
350 },
351 },
352 })
353
354 const storeRef = setupApiStore(enhancedApi, undefined, {
355 withoutTestLifecycles: true,
356 })
357
358 const queryResponse = await storeRef.store.dispatch(
359 enhancedApi.endpoints.query1.initiate(),
360 )
361
362 expectTypeOf(queryResponse.data).toMatchTypeOf<
363 Transformed | undefined
364 >()
365
366 const mutationResponse = await storeRef.store.dispatch(
367 enhancedApi.endpoints.mutation1.initiate(),
368 )
369
370 expectTypeOf(mutationResponse).toMatchTypeOf<
371 | { data: Transformed }
372 | { error: FetchBaseQueryError | SerializedError }
373 >()
374 })
375 })
376 })
377 })