Files
NapCatQQ/.pnpm-store/v3/files/d2/2f2c2028b92e375baf30ded7febb01c1862d83d7ce57fdeca77fad5a41c2d024dd4e49bc501da1a722987e93efd3aafded73f2e60cc3b033559da4286708e1
T
TanTan e311ca3275
Build NapCat Artifacts / Type Check & Test (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

81 lines
2.2 KiB
Plaintext

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>()
},
)
},
})
})
})