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

inject-infinite-query.test-d.ts

43 lines1.2 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 { QueryClient, injectInfiniteQuery, provideTanStackQuery } from '..'
6import type { InfiniteData } from '@tanstack/query-core'
7​
8describe('injectInfiniteQuery', () => {
9 let queryClient: QueryClient
10​
11 beforeEach(() => {
12 queryClient = new QueryClient()
13 vi.useFakeTimers()
14 TestBed.configureTestingModule({
15 providers: [
16 provideZonelessChangeDetection(),
17 provideTanStackQuery(queryClient),
18 ],
19 })
20 })
21​
22 afterEach(() => {
23 vi.useRealTimers()
24 })
25​
26 it('should narrow type after isSuccess', () => {
27 const key = queryKey()
28 const query = TestBed.runInInjectionContext(() => {
29 return injectInfiniteQuery(() => ({
30 queryKey: key,
31 queryFn: ({ pageParam }) =>
32 sleep(0).then(() => 'data on page ' + pageParam),
33 initialPageParam: 0,
34 getNextPageParam: () => 12,
35 }))
36 })
37​
38 if (query.isSuccess()) {
39 const data = query.data()
40 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, unknown>>()
41 }
42 })
43})