-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathinject-is-fetching.test.ts
More file actions
63 lines (55 loc) · 1.65 KB
/
inject-is-fetching.test.ts
File metadata and controls
63 lines (55 loc) · 1.65 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
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,
injectIsFetching,
injectQuery,
provideTanStackQuery,
} from '..'
describe('injectIsFetching', () => {
let queryClient: QueryClient
beforeEach(() => {
vi.useFakeTimers()
queryClient = new QueryClient()
TestBed.configureTestingModule({
providers: [
provideZonelessChangeDetection(),
provideTanStackQuery(queryClient),
],
})
})
afterEach(() => {
vi.useRealTimers()
})
it('Returns number of fetching queries', async () => {
const key = queryKey()
const isFetching = TestBed.runInInjectionContext(() => {
injectQuery(() => ({
queryKey: key,
queryFn: () => sleep(100).then(() => 'Some data'),
}))
return injectIsFetching()
})
expect(isFetching()).toStrictEqual(0)
await vi.advanceTimersByTimeAsync(1)
expect(isFetching()).toStrictEqual(1)
await vi.advanceTimersByTimeAsync(100)
expect(isFetching()).toStrictEqual(0)
})
describe('injection context', () => {
it('throws NG0203 with descriptive error outside injection context', () => {
expect(() => {
injectIsFetching()
}).toThrow(/NG0203(.*?)injectIsFetching/)
})
it('can be used outside injection context when passing an injector', () => {
expect(
injectIsFetching(undefined, {
injector: TestBed.inject(Injector),
}),
).not.toThrow()
})
})
})