| 1 | import {
|
|---|
| 2 | actionsReducer,
|
|---|
| 3 | hookWaitFor,
|
|---|
| 4 | setupApiStore,
|
|---|
| 5 | } from '@internal/tests/utils/helpers'
|
|---|
| 6 | import { createSlice } from '@reduxjs/toolkit'
|
|---|
| 7 | import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
|
|---|
| 8 | import { act, renderHook } from '@testing-library/react'
|
|---|
| 9 |
|
|---|
| 10 | interface ResultType {
|
|---|
| 11 | result: 'complex'
|
|---|
| 12 | }
|
|---|
| 13 |
|
|---|
| 14 | interface ArgType {
|
|---|
| 15 | foo: 'bar'
|
|---|
| 16 | count: 3
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | const baseQuery = fetchBaseQuery({ baseUrl: 'https://example.com' })
|
|---|
| 20 | const api = createApi({
|
|---|
| 21 | baseQuery,
|
|---|
| 22 | endpoints(build) {
|
|---|
| 23 | return {
|
|---|
| 24 | querySuccess: build.query<ResultType, ArgType>({
|
|---|
| 25 | query: () => '/success',
|
|---|
| 26 | }),
|
|---|
| 27 | querySuccess2: build.query({ query: () => '/success' }),
|
|---|
| 28 | queryFail: build.query({ query: () => '/error' }),
|
|---|
| 29 | mutationSuccess: build.mutation({
|
|---|
| 30 | query: () => ({ url: '/success', method: 'POST' }),
|
|---|
| 31 | }),
|
|---|
| 32 | mutationSuccess2: build.mutation({
|
|---|
| 33 | query: () => ({ url: '/success', method: 'POST' }),
|
|---|
| 34 | }),
|
|---|
| 35 | mutationFail: build.mutation({
|
|---|
| 36 | query: () => ({ url: '/error', method: 'POST' }),
|
|---|
| 37 | }),
|
|---|
| 38 | }
|
|---|
| 39 | },
|
|---|
| 40 | })
|
|---|
| 41 |
|
|---|
| 42 | const storeRef = setupApiStore(api, {
|
|---|
| 43 | ...actionsReducer,
|
|---|
| 44 | })
|
|---|
| 45 |
|
|---|
| 46 | const {
|
|---|
| 47 | mutationFail,
|
|---|
| 48 | mutationSuccess,
|
|---|
| 49 | mutationSuccess2,
|
|---|
| 50 | queryFail,
|
|---|
| 51 | querySuccess,
|
|---|
| 52 | querySuccess2,
|
|---|
| 53 | } = api.endpoints
|
|---|
| 54 |
|
|---|
| 55 | test('matches query pending & fulfilled actions for the given endpoint', async () => {
|
|---|
| 56 | const endpoint = querySuccess2
|
|---|
| 57 | const otherEndpoint = queryFail
|
|---|
| 58 | const { result } = renderHook(() => endpoint.useQuery({} as any), {
|
|---|
| 59 | wrapper: storeRef.wrapper,
|
|---|
| 60 | })
|
|---|
| 61 | await hookWaitFor(() => expect(result.current.isLoading).toBeFalsy())
|
|---|
| 62 |
|
|---|
| 63 | expect(storeRef.store.getState().actions).toMatchSequence(
|
|---|
| 64 | api.internalActions.middlewareRegistered.match,
|
|---|
| 65 | endpoint.matchPending,
|
|---|
| 66 | endpoint.matchFulfilled,
|
|---|
| 67 | )
|
|---|
| 68 | expect(storeRef.store.getState().actions).not.toMatchSequence(
|
|---|
| 69 | api.internalActions.middlewareRegistered.match,
|
|---|
| 70 | otherEndpoint.matchPending,
|
|---|
| 71 | otherEndpoint.matchFulfilled,
|
|---|
| 72 | )
|
|---|
| 73 | expect(storeRef.store.getState().actions).not.toMatchSequence(
|
|---|
| 74 | api.internalActions.middlewareRegistered.match,
|
|---|
| 75 | endpoint.matchFulfilled,
|
|---|
| 76 | api.endpoints.mutationSuccess.matchFulfilled,
|
|---|
| 77 | endpoint.matchRejected,
|
|---|
| 78 | )
|
|---|
| 79 | expect(storeRef.store.getState().actions).not.toMatchSequence(
|
|---|
| 80 | api.internalActions.middlewareRegistered.match,
|
|---|
| 81 | endpoint.matchPending,
|
|---|
| 82 | endpoint.matchRejected,
|
|---|
| 83 | )
|
|---|
| 84 | })
|
|---|
| 85 | test('matches query pending & rejected actions for the given endpoint', async () => {
|
|---|
| 86 | const endpoint = queryFail
|
|---|
| 87 | const { result } = renderHook(() => endpoint.useQuery({}), {
|
|---|
| 88 | wrapper: storeRef.wrapper,
|
|---|
| 89 | })
|
|---|
| 90 | await hookWaitFor(() => expect(result.current.isLoading).toBeFalsy())
|
|---|
| 91 | expect(storeRef.store.getState().actions).toMatchSequence(
|
|---|
| 92 | api.internalActions.middlewareRegistered.match,
|
|---|
| 93 | endpoint.matchPending,
|
|---|
| 94 | endpoint.matchRejected,
|
|---|
| 95 | )
|
|---|
| 96 | expect(storeRef.store.getState().actions).not.toMatchSequence(
|
|---|
| 97 | api.internalActions.middlewareRegistered.match,
|
|---|
| 98 | endpoint.matchFulfilled,
|
|---|
| 99 | endpoint.matchRejected,
|
|---|
| 100 | )
|
|---|
| 101 | expect(storeRef.store.getState().actions).not.toMatchSequence(
|
|---|
| 102 | api.internalActions.middlewareRegistered.match,
|
|---|
| 103 | endpoint.matchPending,
|
|---|
| 104 | endpoint.matchFulfilled,
|
|---|
| 105 | )
|
|---|
| 106 | })
|
|---|
| 107 |
|
|---|
| 108 | test('matches lazy query pending & fulfilled actions for given endpoint', async () => {
|
|---|
| 109 | const endpoint = querySuccess
|
|---|
| 110 | const { result } = renderHook(() => endpoint.useLazyQuery(), {
|
|---|
| 111 | wrapper: storeRef.wrapper,
|
|---|
| 112 | })
|
|---|
| 113 | act(() => void result.current[0]({} as any))
|
|---|
| 114 | await hookWaitFor(() => expect(result.current[1].isLoading).toBeFalsy())
|
|---|
| 115 |
|
|---|
| 116 | expect(storeRef.store.getState().actions).toMatchSequence(
|
|---|
| 117 | api.internalActions.middlewareRegistered.match,
|
|---|
| 118 | endpoint.matchPending,
|
|---|
| 119 | endpoint.matchFulfilled,
|
|---|
| 120 | )
|
|---|
| 121 | expect(storeRef.store.getState().actions).not.toMatchSequence(
|
|---|
| 122 | api.internalActions.middlewareRegistered.match,
|
|---|
| 123 | endpoint.matchFulfilled,
|
|---|
| 124 | endpoint.matchRejected,
|
|---|
| 125 | )
|
|---|
| 126 |
|
|---|
| 127 | expect(storeRef.store.getState().actions).not.toMatchSequence(
|
|---|
| 128 | api.internalActions.middlewareRegistered.match,
|
|---|
| 129 | endpoint.matchPending,
|
|---|
| 130 | endpoint.matchRejected,
|
|---|
| 131 | )
|
|---|
| 132 | })
|
|---|
| 133 |
|
|---|
| 134 | test('matches lazy query pending & rejected actions for given endpoint', async () => {
|
|---|
| 135 | const endpoint = queryFail
|
|---|
| 136 | const { result } = renderHook(() => endpoint.useLazyQuery(), {
|
|---|
| 137 | wrapper: storeRef.wrapper,
|
|---|
| 138 | })
|
|---|
| 139 | act(() => void result.current[0]({}))
|
|---|
| 140 | await hookWaitFor(() => expect(result.current[1].isLoading).toBeFalsy())
|
|---|
| 141 |
|
|---|
| 142 | expect(storeRef.store.getState().actions).toMatchSequence(
|
|---|
| 143 | api.internalActions.middlewareRegistered.match,
|
|---|
| 144 | endpoint.matchPending,
|
|---|
| 145 | endpoint.matchRejected,
|
|---|
| 146 | )
|
|---|
| 147 | expect(storeRef.store.getState().actions).not.toMatchSequence(
|
|---|
| 148 | api.internalActions.middlewareRegistered.match,
|
|---|
| 149 | endpoint.matchFulfilled,
|
|---|
| 150 | endpoint.matchRejected,
|
|---|
| 151 | )
|
|---|
| 152 | expect(storeRef.store.getState().actions).not.toMatchSequence(
|
|---|
| 153 | api.internalActions.middlewareRegistered.match,
|
|---|
| 154 | endpoint.matchPending,
|
|---|
| 155 | endpoint.matchFulfilled,
|
|---|
| 156 | )
|
|---|
| 157 | })
|
|---|
| 158 |
|
|---|
| 159 | test('matches mutation pending & fulfilled actions for the given endpoint', async () => {
|
|---|
| 160 | const endpoint = mutationSuccess
|
|---|
| 161 | const otherEndpoint = mutationSuccess2
|
|---|
| 162 | const { result } = renderHook(() => endpoint.useMutation(), {
|
|---|
| 163 | wrapper: storeRef.wrapper,
|
|---|
| 164 | })
|
|---|
| 165 | act(() => void result.current[0]({}))
|
|---|
| 166 | await hookWaitFor(() => expect(result.current[1].isLoading).toBeFalsy())
|
|---|
| 167 |
|
|---|
| 168 | expect(storeRef.store.getState().actions).toMatchSequence(
|
|---|
| 169 | api.internalActions.middlewareRegistered.match,
|
|---|
| 170 | endpoint.matchPending,
|
|---|
| 171 | endpoint.matchFulfilled,
|
|---|
| 172 | )
|
|---|
| 173 | expect(storeRef.store.getState().actions).not.toMatchSequence(
|
|---|
| 174 | api.internalActions.middlewareRegistered.match,
|
|---|
| 175 | otherEndpoint.matchPending,
|
|---|
| 176 | otherEndpoint.matchFulfilled,
|
|---|
| 177 | )
|
|---|
| 178 | expect(storeRef.store.getState().actions).not.toMatchSequence(
|
|---|
| 179 | api.internalActions.middlewareRegistered.match,
|
|---|
| 180 | endpoint.matchFulfilled,
|
|---|
| 181 | endpoint.matchRejected,
|
|---|
| 182 | )
|
|---|
| 183 | expect(storeRef.store.getState().actions).not.toMatchSequence(
|
|---|
| 184 | api.internalActions.middlewareRegistered.match,
|
|---|
| 185 | endpoint.matchPending,
|
|---|
| 186 | endpoint.matchRejected,
|
|---|
| 187 | )
|
|---|
| 188 | })
|
|---|
| 189 | test('matches mutation pending & rejected actions for the given endpoint', async () => {
|
|---|
| 190 | const endpoint = mutationFail
|
|---|
| 191 | const { result } = renderHook(() => endpoint.useMutation(), {
|
|---|
| 192 | wrapper: storeRef.wrapper,
|
|---|
| 193 | })
|
|---|
| 194 | act(() => void result.current[0]({}))
|
|---|
| 195 | await hookWaitFor(() => expect(result.current[1].isLoading).toBeFalsy())
|
|---|
| 196 |
|
|---|
| 197 | expect(storeRef.store.getState().actions).toMatchSequence(
|
|---|
| 198 | api.internalActions.middlewareRegistered.match,
|
|---|
| 199 | endpoint.matchPending,
|
|---|
| 200 | endpoint.matchRejected,
|
|---|
| 201 | )
|
|---|
| 202 | expect(storeRef.store.getState().actions).not.toMatchSequence(
|
|---|
| 203 | api.internalActions.middlewareRegistered.match,
|
|---|
| 204 | endpoint.matchFulfilled,
|
|---|
| 205 | endpoint.matchRejected,
|
|---|
| 206 | )
|
|---|
| 207 | expect(storeRef.store.getState().actions).not.toMatchSequence(
|
|---|
| 208 | api.internalActions.middlewareRegistered.match,
|
|---|
| 209 | endpoint.matchPending,
|
|---|
| 210 | endpoint.matchFulfilled,
|
|---|
| 211 | )
|
|---|
| 212 | })
|
|---|
| 213 |
|
|---|
| 214 | test('inferred types', () => {
|
|---|
| 215 | createSlice({
|
|---|
| 216 | name: 'auth',
|
|---|
| 217 | initialState: {},
|
|---|
| 218 | reducers: {},
|
|---|
| 219 | extraReducers: (builder) => {
|
|---|
| 220 | builder
|
|---|
| 221 | .addMatcher(
|
|---|
| 222 | api.endpoints.querySuccess.matchPending,
|
|---|
| 223 | (state, action) => {
|
|---|
| 224 | // @ts-expect-error
|
|---|
| 225 | console.log(action.error)
|
|---|
| 226 | },
|
|---|
| 227 | )
|
|---|
| 228 | .addMatcher(
|
|---|
| 229 | api.endpoints.querySuccess.matchFulfilled,
|
|---|
| 230 | (state, action) => {
|
|---|
| 231 | // @ts-expect-error
|
|---|
| 232 | console.log(action.error)
|
|---|
| 233 | },
|
|---|
| 234 | )
|
|---|
| 235 | .addMatcher(
|
|---|
| 236 | api.endpoints.querySuccess.matchRejected,
|
|---|
| 237 | (state, action) => {},
|
|---|
| 238 | )
|
|---|
| 239 | },
|
|---|
| 240 | })
|
|---|
| 241 | })
|
|---|