File size: 4,338 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
import { noop } from '@internal/listenerMiddleware/utils'
import { configureStore } from '@reduxjs/toolkit'
import { createApi, fakeBaseQuery } from '@reduxjs/toolkit/query'

type CustomErrorType = { type: 'Custom' }

const api = createApi({
  baseQuery: fakeBaseQuery<CustomErrorType>(),
  endpoints: (build) => ({
    withQuery: build.query<string, string>({
      // @ts-expect-error
      query(arg: string) {
        return `resultFrom(${arg})`
      },
      // @ts-expect-error
      transformResponse(response) {
        return response.wrappedByBaseQuery
      },
    }),
    withQueryFn: build.query<string, string>({
      queryFn(arg: string) {
        return { data: `resultFrom(${arg})` }
      },
    }),
    withInvalidDataQueryFn: build.query<string, string>({
      // @ts-expect-error
      queryFn(arg: string) {
        return { data: 5 }
      },
    }),
    withErrorQueryFn: build.query<string, string>({
      queryFn(arg: string) {
        return { error: { type: 'Custom' } }
      },
    }),
    withInvalidErrorQueryFn: build.query<string, string>({
      // @ts-expect-error
      queryFn(arg: string) {
        return { error: 5 }
      },
    }),
    withAsyncQueryFn: build.query<string, string>({
      async queryFn(arg: string) {
        return { data: `resultFrom(${arg})` }
      },
    }),
    withInvalidDataAsyncQueryFn: build.query<string, string>({
      // @ts-expect-error
      async queryFn(arg: string) {
        return { data: 5 }
      },
    }),
    withAsyncErrorQueryFn: build.query<string, string>({
      async queryFn(arg: string) {
        return { error: { type: 'Custom' } }
      },
    }),
    withInvalidAsyncErrorQueryFn: build.query<string, string>({
      // @ts-expect-error
      async queryFn(arg: string) {
        return { error: 5 }
      },
    }),

    mutationWithQueryFn: build.mutation<string, string>({
      queryFn(arg: string) {
        return { data: `resultFrom(${arg})` }
      },
    }),
    mutationWithInvalidDataQueryFn: build.mutation<string, string>({
      // @ts-expect-error
      queryFn(arg: string) {
        return { data: 5 }
      },
    }),
    mutationWithErrorQueryFn: build.mutation<string, string>({
      queryFn(arg: string) {
        return { error: { type: 'Custom' } }
      },
    }),
    mutationWithInvalidErrorQueryFn: build.mutation<string, string>({
      // @ts-expect-error
      queryFn(arg: string) {
        return { error: 5 }
      },
    }),

    mutationWithAsyncQueryFn: build.mutation<string, string>({
      async queryFn(arg: string) {
        return { data: `resultFrom(${arg})` }
      },
    }),
    mutationWithInvalidAsyncQueryFn: build.mutation<string, string>({
      // @ts-expect-error
      async queryFn(arg: string) {
        return { data: 5 }
      },
    }),
    mutationWithAsyncErrorQueryFn: build.mutation<string, string>({
      async queryFn(arg: string) {
        return { error: { type: 'Custom' } }
      },
    }),
    mutationWithInvalidAsyncErrorQueryFn: build.mutation<string, string>({
      // @ts-expect-error
      async queryFn(arg: string) {
        return { error: 5 }
      },
    }),
    // @ts-expect-error
    withNeither: build.query<string, string>({}),
    // @ts-expect-error
    mutationWithNeither: build.mutation<string, string>({}),
  }),
})

const store = configureStore({
  reducer: {
    [api.reducerPath]: api.reducer,
  },
  middleware: (gDM) => gDM({}).concat(api.middleware),
})

test('fakeBaseQuery throws when invoking query', async () => {
  const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(noop)

  const thunk = api.endpoints.withQuery.initiate('')

  const result = await store.dispatch(thunk)

  expect(consoleErrorSpy).toHaveBeenCalledOnce()

  expect(consoleErrorSpy).toHaveBeenLastCalledWith(
    `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".`,
    Error(
      'When using `fakeBaseQuery`, all queries & mutations must use the `queryFn` definition syntax.',
    ),
  )

  expect(result!.error).toEqual({
    message:
      'When using `fakeBaseQuery`, all queries & mutations must use the `queryFn` definition syntax.',
    name: 'Error',
    stack: expect.any(String),
  })

  consoleErrorSpy.mockRestore()
})