Spaces:
Running
Running
File size: 2,227 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 | import type { SerializedError } from '@reduxjs/toolkit'
import { createSlice } from '@reduxjs/toolkit'
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
interface ResultType {
result: 'complex'
}
interface ArgType {
foo: 'bar'
count: 3
}
const baseQuery = fetchBaseQuery({ baseUrl: 'https://example.com' })
const api = createApi({
baseQuery,
endpoints(build) {
return {
querySuccess: build.query<ResultType, ArgType>({
query: () => '/success',
}),
querySuccess2: build.query({ query: () => '/success' }),
queryFail: build.query({ query: () => '/error' }),
mutationSuccess: build.mutation({
query: () => ({ url: '/success', method: 'POST' }),
}),
mutationSuccess2: build.mutation({
query: () => ({ url: '/success', method: 'POST' }),
}),
mutationFail: build.mutation({
query: () => ({ url: '/error', method: 'POST' }),
}),
}
},
})
describe('type tests', () => {
test('inferred types', () => {
createSlice({
name: 'auth',
initialState: {},
reducers: {},
extraReducers: (builder) => {
builder
.addMatcher(
api.endpoints.querySuccess.matchPending,
(state, action) => {
expectTypeOf(action.payload).toBeUndefined()
expectTypeOf(
action.meta.arg.originalArgs,
).toEqualTypeOf<ArgType>()
},
)
.addMatcher(
api.endpoints.querySuccess.matchFulfilled,
(state, action) => {
expectTypeOf(action.payload).toEqualTypeOf<ResultType>()
expectTypeOf(action.meta.fulfilledTimeStamp).toBeNumber()
expectTypeOf(
action.meta.arg.originalArgs,
).toEqualTypeOf<ArgType>()
},
)
.addMatcher(
api.endpoints.querySuccess.matchRejected,
(state, action) => {
expectTypeOf(action.error).toEqualTypeOf<SerializedError>()
expectTypeOf(
action.meta.arg.originalArgs,
).toEqualTypeOf<ArgType>()
},
)
},
})
})
})
|