|
1 |
| -import type { UseMutation, UseQuery } from '@internal/query/react/buildHooks' |
| 1 | +import type { |
| 2 | + QueryStateSelector, |
| 3 | + UseMutation, |
| 4 | + UseQuery, |
| 5 | +} from '@internal/query/react/buildHooks' |
2 | 6 | import { ANY } from '@internal/tests/utils/helpers'
|
3 | 7 | import type { SerializedError } from '@reduxjs/toolkit'
|
4 |
| -import type { SubscriptionOptions } from '@reduxjs/toolkit/query/react' |
| 8 | +import type { |
| 9 | + QueryDefinition, |
| 10 | + SubscriptionOptions, |
| 11 | + TypedQueryStateSelector, |
| 12 | +} from '@reduxjs/toolkit/query/react' |
5 | 13 | import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
|
6 | 14 | import { useState } from 'react'
|
7 | 15 |
|
@@ -260,4 +268,107 @@ describe('type tests', () => {
|
260 | 268 | api.endpoints.updateUser.useMutation,
|
261 | 269 | )
|
262 | 270 | })
|
| 271 | + |
| 272 | + test('TypedQueryStateSelector creates a pre-typed version of QueryStateSelector', () => { |
| 273 | + type Post = { |
| 274 | + id: number |
| 275 | + title: string |
| 276 | + } |
| 277 | + |
| 278 | + type PostsApiResponse = { |
| 279 | + posts: Post[] |
| 280 | + total: number |
| 281 | + skip: number |
| 282 | + limit: number |
| 283 | + } |
| 284 | + |
| 285 | + type QueryArgument = number | undefined |
| 286 | + |
| 287 | + type BaseQueryFunction = ReturnType<typeof fetchBaseQuery> |
| 288 | + |
| 289 | + type SelectedResult = Pick<PostsApiResponse, 'posts'> |
| 290 | + |
| 291 | + const postsApiSlice = createApi({ |
| 292 | + baseQuery: fetchBaseQuery({ baseUrl: 'https://dummyjson.com/posts' }), |
| 293 | + reducerPath: 'postsApi', |
| 294 | + tagTypes: ['Posts'], |
| 295 | + endpoints: (build) => ({ |
| 296 | + getPosts: build.query<PostsApiResponse, QueryArgument>({ |
| 297 | + query: (limit = 5) => `?limit=${limit}&select=title`, |
| 298 | + }), |
| 299 | + }), |
| 300 | + }) |
| 301 | + |
| 302 | + const { useGetPostsQuery } = postsApiSlice |
| 303 | + |
| 304 | + function PostById({ id }: { id: number }) { |
| 305 | + const { post } = useGetPostsQuery(undefined, { |
| 306 | + selectFromResult: (state) => ({ |
| 307 | + post: state.data?.posts.find((post) => post.id === id), |
| 308 | + }), |
| 309 | + }) |
| 310 | + |
| 311 | + expectTypeOf(post).toEqualTypeOf<Post | undefined>() |
| 312 | + |
| 313 | + return <li>{post?.title}</li> |
| 314 | + } |
| 315 | + |
| 316 | + const EMPTY_ARRAY: Post[] = [] |
| 317 | + |
| 318 | + const typedSelectFromResult: TypedQueryStateSelector< |
| 319 | + PostsApiResponse, |
| 320 | + QueryArgument, |
| 321 | + BaseQueryFunction, |
| 322 | + SelectedResult |
| 323 | + > = (state) => ({ posts: state.data?.posts ?? EMPTY_ARRAY }) |
| 324 | + |
| 325 | + expectTypeOf< |
| 326 | + TypedQueryStateSelector< |
| 327 | + PostsApiResponse, |
| 328 | + QueryArgument, |
| 329 | + BaseQueryFunction, |
| 330 | + SelectedResult |
| 331 | + > |
| 332 | + >().toEqualTypeOf< |
| 333 | + QueryStateSelector< |
| 334 | + SelectedResult, |
| 335 | + QueryDefinition< |
| 336 | + QueryArgument, |
| 337 | + BaseQueryFunction, |
| 338 | + string, |
| 339 | + PostsApiResponse |
| 340 | + > |
| 341 | + > |
| 342 | + >() |
| 343 | + |
| 344 | + expectTypeOf(typedSelectFromResult).toEqualTypeOf< |
| 345 | + QueryStateSelector< |
| 346 | + SelectedResult, |
| 347 | + QueryDefinition< |
| 348 | + QueryArgument, |
| 349 | + BaseQueryFunction, |
| 350 | + string, |
| 351 | + PostsApiResponse |
| 352 | + > |
| 353 | + > |
| 354 | + >() |
| 355 | + |
| 356 | + function PostsList() { |
| 357 | + const { posts } = useGetPostsQuery(undefined, { |
| 358 | + selectFromResult: typedSelectFromResult, |
| 359 | + }) |
| 360 | + |
| 361 | + expectTypeOf(posts).toEqualTypeOf<Post[]>() |
| 362 | + |
| 363 | + return ( |
| 364 | + <div> |
| 365 | + <ul> |
| 366 | + {posts.map((post) => ( |
| 367 | + <PostById key={post.id} id={post.id} /> |
| 368 | + ))} |
| 369 | + </ul> |
| 370 | + </div> |
| 371 | + ) |
| 372 | + } |
| 373 | + }) |
263 | 374 | })
|
0 commit comments