Files
NapCatQQ/.pnpm-store/v3/files/41/5d404b5fc3c9d16fbbcfdf836042172ac4928dbc0fe5b5b9558dc5af710fb6eedd2c636bbf66cddc71e25481a432e74d20a27c795ed0b74226afd9cee0bc98
TanTan e311ca3275
Build NapCat Artifacts / Windows Loader Test (push) Canceled after 0s
Build NapCat Artifacts / Type Check & Test (push) Canceled after 0s
Build NapCat Artifacts / Build NapCat.Framework (push) Canceled after 0s
Build NapCat Artifacts / Build NapCat.Shell (push) Canceled after 0s
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

112 lines
2.7 KiB
Plaintext

import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
import { configureStore, createSelector } from '@reduxjs/toolkit'
describe('type tests', () => {
test('buildSelector type test', () => {
interface Todo {
userId: number
id: number
title: string
completed: boolean
}
type Todos = Array<Todo>
const exampleApi = createApi({
reducerPath: 'api',
baseQuery: fetchBaseQuery({
baseUrl: 'https://jsonplaceholder.typicode.com',
}),
endpoints: (build) => ({
getTodos: build.query<Todos, string>({
query: () => '/todos',
}),
}),
})
const exampleQuerySelector = exampleApi.endpoints.getTodos.select('/')
const todosSelector = createSelector(
[exampleQuerySelector],
(queryState) => {
return queryState?.data?.[0] ?? ({} as Todo)
},
)
const firstTodoTitleSelector = createSelector(
[todosSelector],
(todo) => todo?.title,
)
const store = configureStore({
reducer: {
[exampleApi.reducerPath]: exampleApi.reducer,
other: () => 1,
},
})
const todoTitle = firstTodoTitleSelector(store.getState())
// This only compiles if we carried the types through
const upperTitle = todoTitle.toUpperCase()
expectTypeOf(upperTitle).toBeString()
})
test('selectCachedArgsForQuery type test', () => {
interface Todo {
userId: number
id: number
title: string
completed: boolean
}
type Todos = Array<Todo>
const exampleApi = createApi({
reducerPath: 'api',
baseQuery: fetchBaseQuery({
baseUrl: 'https://jsonplaceholder.typicode.com',
}),
endpoints: (build) => ({
getTodos: build.query<Todos, string>({
query: () => '/todos',
}),
getInfiniteTodos: build.infiniteQuery<Todos, string, number>({
infiniteQueryOptions: {
initialPageParam: 0,
maxPages: 3,
getNextPageParam: (
lastPage,
allPages,
lastPageParam,
allPageParams,
) => lastPageParam + 1,
},
query({ pageParam }) {
return `/todos?page=${pageParam}`
},
}),
}),
})
const store = configureStore({
reducer: {
[exampleApi.reducerPath]: exampleApi.reducer,
other: () => 1,
},
})
expectTypeOf(
exampleApi.util.selectCachedArgsForQuery(store.getState(), 'getTodos'),
).toEqualTypeOf<string[]>()
expectTypeOf(
exampleApi.util.selectCachedArgsForQuery(
store.getState(),
'getInfiniteTodos',
),
).toEqualTypeOf<string[]>()
})
})