Open History to see the last commit
1import { TestBed } from '@angular/core/testing'
2import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
3import { Injector, provideZonelessChangeDetection } from '@angular/core'
4import { queryKey, sleep } from '@tanstack/query-test-utils'
5import { QueryClient, injectInfiniteQuery, provideTanStackQuery } from '..'
6import { expectSignals } from './test-utils'
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 properly execute infinite query', async () => {
27 const key = queryKey()
28 const query = TestBed.runInInjectionContext(() => {
29 return injectInfiniteQuery(() => ({
30 queryKey: key,
31 queryFn: ({ pageParam }) =>
32 sleep(10).then(() => 'data on page ' + pageParam),
33 initialPageParam: 0,
34 getNextPageParam: () => 12,
35 }))
36 })
37β
38 expectSignals(query, {
39 data: undefined,
40 status: 'pending',
41 })
42β
43 await vi.advanceTimersByTimeAsync(11)
44β
45 expectSignals(query, {
46 data: {
47 pageParams: [0],
48 pages: ['data on page 0'],
49 },
50 status: 'success',
51 })
52β
53 void query.fetchNextPage()
54β
55 await vi.advanceTimersByTimeAsync(11)
56β
57 expectSignals(query, {
58 data: {
59 pageParams: [0, 12],
60 pages: ['data on page 0', 'data on page 12'],
61 },
62 status: 'success',
63 })
64 })
65β
66 describe('injection context', () => {
67 test('throws NG0203 with descriptive error outside injection context', () => {
68 const key = queryKey()
69 expect(() => {
70 injectInfiniteQuery(() => ({
71 queryKey: key,
72 queryFn: ({ pageParam }) =>
73 sleep(0).then(() => 'data on page ' + pageParam),
74 initialPageParam: 0,
75 getNextPageParam: () => 12,
76 }))
77 }).toThrow(/NG0203(.*?)injectInfiniteQuery/)
78 })
79β
80 test('can be used outside injection context when passing an injector', () => {
81 const key = queryKey()
82 const query = injectInfiniteQuery(
83 () => ({
84 queryKey: key,
85 queryFn: ({ pageParam }) =>
86 sleep(0).then(() => 'data on page ' + pageParam),
87 initialPageParam: 0,
88 getNextPageParam: () => 12,
89 }),
90 {
91 injector: TestBed.inject(Injector),
92 },
93 )
94β
95 expect(query.status()).toBe('pending')
96 })
97 })
98})