source: node_modules/@reduxjs/toolkit/src/query/tests/fakeBaseQuery.test.tsx@ ba17441

Last change on this file since ba17441 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 4.2 KB
Line 
1import { noop } from '@internal/listenerMiddleware/utils'
2import { configureStore } from '@reduxjs/toolkit'
3import { createApi, fakeBaseQuery } from '@reduxjs/toolkit/query'
4
5type CustomErrorType = { type: 'Custom' }
6
7const api = createApi({
8 baseQuery: fakeBaseQuery<CustomErrorType>(),
9 endpoints: (build) => ({
10 withQuery: build.query<string, string>({
11 // @ts-expect-error
12 query(arg: string) {
13 return `resultFrom(${arg})`
14 },
15 // @ts-expect-error
16 transformResponse(response) {
17 return response.wrappedByBaseQuery
18 },
19 }),
20 withQueryFn: build.query<string, string>({
21 queryFn(arg: string) {
22 return { data: `resultFrom(${arg})` }
23 },
24 }),
25 withInvalidDataQueryFn: build.query<string, string>({
26 // @ts-expect-error
27 queryFn(arg: string) {
28 return { data: 5 }
29 },
30 }),
31 withErrorQueryFn: build.query<string, string>({
32 queryFn(arg: string) {
33 return { error: { type: 'Custom' } }
34 },
35 }),
36 withInvalidErrorQueryFn: build.query<string, string>({
37 // @ts-expect-error
38 queryFn(arg: string) {
39 return { error: 5 }
40 },
41 }),
42 withAsyncQueryFn: build.query<string, string>({
43 async queryFn(arg: string) {
44 return { data: `resultFrom(${arg})` }
45 },
46 }),
47 withInvalidDataAsyncQueryFn: build.query<string, string>({
48 // @ts-expect-error
49 async queryFn(arg: string) {
50 return { data: 5 }
51 },
52 }),
53 withAsyncErrorQueryFn: build.query<string, string>({
54 async queryFn(arg: string) {
55 return { error: { type: 'Custom' } }
56 },
57 }),
58 withInvalidAsyncErrorQueryFn: build.query<string, string>({
59 // @ts-expect-error
60 async queryFn(arg: string) {
61 return { error: 5 }
62 },
63 }),
64
65 mutationWithQueryFn: build.mutation<string, string>({
66 queryFn(arg: string) {
67 return { data: `resultFrom(${arg})` }
68 },
69 }),
70 mutationWithInvalidDataQueryFn: build.mutation<string, string>({
71 // @ts-expect-error
72 queryFn(arg: string) {
73 return { data: 5 }
74 },
75 }),
76 mutationWithErrorQueryFn: build.mutation<string, string>({
77 queryFn(arg: string) {
78 return { error: { type: 'Custom' } }
79 },
80 }),
81 mutationWithInvalidErrorQueryFn: build.mutation<string, string>({
82 // @ts-expect-error
83 queryFn(arg: string) {
84 return { error: 5 }
85 },
86 }),
87
88 mutationWithAsyncQueryFn: build.mutation<string, string>({
89 async queryFn(arg: string) {
90 return { data: `resultFrom(${arg})` }
91 },
92 }),
93 mutationWithInvalidAsyncQueryFn: build.mutation<string, string>({
94 // @ts-expect-error
95 async queryFn(arg: string) {
96 return { data: 5 }
97 },
98 }),
99 mutationWithAsyncErrorQueryFn: build.mutation<string, string>({
100 async queryFn(arg: string) {
101 return { error: { type: 'Custom' } }
102 },
103 }),
104 mutationWithInvalidAsyncErrorQueryFn: build.mutation<string, string>({
105 // @ts-expect-error
106 async queryFn(arg: string) {
107 return { error: 5 }
108 },
109 }),
110 // @ts-expect-error
111 withNeither: build.query<string, string>({}),
112 // @ts-expect-error
113 mutationWithNeither: build.mutation<string, string>({}),
114 }),
115})
116
117const store = configureStore({
118 reducer: {
119 [api.reducerPath]: api.reducer,
120 },
121 middleware: (gDM) => gDM({}).concat(api.middleware),
122})
123
124test('fakeBaseQuery throws when invoking query', async () => {
125 const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(noop)
126
127 const thunk = api.endpoints.withQuery.initiate('')
128
129 const result = await store.dispatch(thunk)
130
131 expect(consoleErrorSpy).toHaveBeenCalledOnce()
132
133 expect(consoleErrorSpy).toHaveBeenLastCalledWith(
134 `An unhandled error occurred processing a request for the endpoint "withQuery".\nIn the case of an unhandled error, no tags will be "provided" or "invalidated".`,
135 Error(
136 'When using `fakeBaseQuery`, all queries & mutations must use the `queryFn` definition syntax.',
137 ),
138 )
139
140 expect(result!.error).toEqual({
141 message:
142 'When using `fakeBaseQuery`, all queries & mutations must use the `queryFn` definition syntax.',
143 name: 'Error',
144 stack: expect.any(String),
145 })
146
147 consoleErrorSpy.mockRestore()
148})
Note: See TracBrowser for help on using the repository browser.