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

84 lines
2.7 KiB
Plaintext

import { createApi } from '@reduxjs/toolkit/query'
const baseQuery = (args?: any) => ({ data: args })
const api = createApi({
baseQuery,
tagTypes: ['Banana', 'Bread'],
endpoints: (build) => ({
getBanana: build.query<unknown, number>({
query(id) {
return { url: `banana/${id}` }
},
providesTags: ['Banana'],
}),
getBananas: build.query<unknown, void>({
query() {
return { url: 'bananas' }
},
providesTags: ['Banana'],
}),
getBread: build.query<unknown, number>({
query(id) {
return { url: `bread/${id}` }
},
providesTags: ['Bread'],
}),
}),
})
describe('type tests', () => {
it('should allow for an array of string TagTypes', () => {
api.util.invalidateTags(['Banana', 'Bread'])
})
it('should allow for an array of full TagTypes descriptions', () => {
api.util.invalidateTags([{ type: 'Banana' }, { type: 'Bread', id: 1 }])
})
it('should allow for a mix of full descriptions as well as plain strings', () => {
api.util.invalidateTags(['Banana', { type: 'Bread', id: 1 }])
})
it('should error when using non-existing TagTypes', () => {
// @ts-expect-error
api.util.invalidateTags(['Missing Tag'])
})
it('should error when using non-existing TagTypes in the full format', () => {
// @ts-expect-error
api.util.invalidateTags([{ type: 'Missing' }])
})
it('should allow pre-fetching for an endpoint that takes an arg', () => {
api.util.prefetch('getBanana', 5, { force: true })
api.util.prefetch('getBanana', 5, { force: false })
api.util.prefetch('getBanana', 5, { ifOlderThan: false })
api.util.prefetch('getBanana', 5, { ifOlderThan: 30 })
api.util.prefetch('getBanana', 5, {})
})
it('should error when pre-fetching with the incorrect arg type', () => {
// @ts-expect-error arg should be number, not string
api.util.prefetch('getBanana', '5', { force: true })
})
it('should allow pre-fetching for an endpoint with a void arg', () => {
api.util.prefetch('getBananas', undefined, { force: true })
api.util.prefetch('getBananas', undefined, { force: false })
api.util.prefetch('getBananas', undefined, { ifOlderThan: false })
api.util.prefetch('getBananas', undefined, { ifOlderThan: 30 })
api.util.prefetch('getBananas', undefined, {})
})
it('should error when pre-fetching with a defined arg when expecting void', () => {
// @ts-expect-error arg should be void, not number
api.util.prefetch('getBananas', 5, { force: true })
})
it('should error when pre-fetching for an incorrect endpoint name', () => {
// @ts-expect-error endpoint name does not exist
api.util.prefetch('getPomegranates', undefined, { force: true })
})
})