-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathinject-infinite-query.test.ts
More file actions
98 lines (85 loc) · 2.57 KB
/
inject-infinite-query.test.ts
File metadata and controls
98 lines (85 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { TestBed } from '@angular/core/testing'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { Injector, provideZonelessChangeDetection } from '@angular/core'
import { queryKey, sleep } from '@tanstack/query-test-utils'
import { QueryClient, injectInfiniteQuery, provideTanStackQuery } from '..'
import { expectSignals } from './test-utils'
describe('injectInfiniteQuery', () => {
let queryClient: QueryClient
beforeEach(() => {
queryClient = new QueryClient()
vi.useFakeTimers()
TestBed.configureTestingModule({
providers: [
provideZonelessChangeDetection(),
provideTanStackQuery(queryClient),
],
})
})
afterEach(() => {
vi.useRealTimers()
})
it('should properly execute infinite query', async () => {
const key = queryKey()
const query = TestBed.runInInjectionContext(() => {
return injectInfiniteQuery(() => ({
queryKey: key,
queryFn: ({ pageParam }) =>
sleep(10).then(() => 'data on page ' + pageParam),
initialPageParam: 0,
getNextPageParam: () => 12,
}))
})
expectSignals(query, {
data: undefined,
status: 'pending',
})
await vi.advanceTimersByTimeAsync(11)
expectSignals(query, {
data: {
pageParams: [0],
pages: ['data on page 0'],
},
status: 'success',
})
void query.fetchNextPage()
await vi.advanceTimersByTimeAsync(11)
expectSignals(query, {
data: {
pageParams: [0, 12],
pages: ['data on page 0', 'data on page 12'],
},
status: 'success',
})
})
describe('injection context', () => {
it('throws NG0203 with descriptive error outside injection context', () => {
const key = queryKey()
expect(() => {
injectInfiniteQuery(() => ({
queryKey: key,
queryFn: ({ pageParam }) =>
sleep(0).then(() => 'data on page ' + pageParam),
initialPageParam: 0,
getNextPageParam: () => 12,
}))
}).toThrow(/NG0203(.*?)injectInfiniteQuery/)
})
it('can be used outside injection context when passing an injector', () => {
const key = queryKey()
const query = injectInfiniteQuery(
() => ({
queryKey: key,
queryFn: ({ pageParam }) =>
sleep(0).then(() => 'data on page ' + pageParam),
initialPageParam: 0,
getNextPageParam: () => 12,
}),
{
injector: TestBed.inject(Injector),
},
)
expect(query.status()).toBe('pending')
})
})
})