Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

inject-infinite-query.test-d.ts

66 lines1.9 KB

1import { TestBed } from '@angular/core/testing'
2import { afterEach, beforeEach, describe, expectTypeOf, it, vi } from 'vitest'
3import { provideZonelessChangeDetection } from '@angular/core'
4import { queryKey, sleep } from '@tanstack/query-test-utils'
5import {
6 QueryClient,
7 injectInfiniteQuery,
8 provideTanStackQuery,
9 skipToken,
10} from '..'
11import type { InfiniteData } from '@tanstack/query-core'
12​
13describe('injectInfiniteQuery', () => {
14 let queryClient: QueryClient
15​
16 beforeEach(() => {
17 queryClient = new QueryClient()
18 vi.useFakeTimers()
19 TestBed.configureTestingModule({
20 providers: [
21 provideZonelessChangeDetection(),
22 provideTanStackQuery(queryClient),
23 ],
24 })
25 })
26​
27 afterEach(() => {
28 vi.useRealTimers()
29 })
30​
31 it('should narrow type after isSuccess', () => {
32 const key = queryKey()
33 const query = TestBed.runInInjectionContext(() => {
34 return injectInfiniteQuery(() => ({
35 queryKey: key,
36 queryFn: ({ pageParam }) =>
37 sleep(0).then(() => 'data on page ' + pageParam),
38 initialPageParam: 0,
39 getNextPageParam: () => 12,
40 }))
41 })
42​
43 if (query.isSuccess()) {
44 const data = query.data()
45 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, unknown>>()
46 }
47 })
48​
49 it('should work when queryFn is skipToken', () => {
50 const key = queryKey()
51 const query = TestBed.runInInjectionContext(() => {
52 return injectInfiniteQuery(() => ({
53 queryKey: key,
54 queryFn: skipToken,
55 initialPageParam: 0,
56 getNextPageParam: () => 1,
57 }))
58 })
59​
60 // TPageParam falls back to `unknown` here (unlike `infiniteQueryOptions`, which infers
61 // `number` for the same options) because there's no skipToken-specific overload to carry it.
62 expectTypeOf(query.data()).toEqualTypeOf<
63 InfiniteData<unknown, unknown> | undefined
64 >()
65 })
66})