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

inject-infinite-query.test-d.ts

42 lines1.2 KB

1import { TestBed } from '@angular/core/testing'
2import { afterEach, beforeEach, describe, expectTypeOf, test, vi } from 'vitest'
3import { provideZonelessChangeDetection } from '@angular/core'
4import { 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 test('should narrow type after isSuccess', () => {
27 const query = TestBed.runInInjectionContext(() => {
28 return injectInfiniteQuery(() => ({
29 queryKey: ['infiniteQuery'],
30 queryFn: ({ pageParam }) =>
31 sleep(0).then(() => 'data on page ' + pageParam),
32 initialPageParam: 0,
33 getNextPageParam: () => 12,
34 }))
35 })
36​
37 if (query.isSuccess()) {
38 const data = query.data()
39 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, unknown>>()
40 }
41 })
42})