Open History to see the last commit
1import { TestBed } from '@angular/core/testing'
2import { beforeEach, describe, expect } from 'vitest'
3import {
4 Injector,
5 provideExperimentalZonelessChangeDetection,
6} from '@angular/core'
7import {
8 QueryClient,
9 injectIsFetching,
10 injectQuery,
11 provideTanStackQuery,
12} from '..'
13import { delayedFetcher } from './test-utils'
14β
15const QUERY_DURATION = 100
16β
17const resolveQueries = () => vi.advanceTimersByTimeAsync(QUERY_DURATION)
18β
19describe('injectIsFetching', () => {
20 let queryClient: QueryClient
21β
22 beforeEach(() => {
23 vi.useFakeTimers()
24 queryClient = new QueryClient()
25β
26 TestBed.configureTestingModule({
27 providers: [
28 provideExperimentalZonelessChangeDetection(),
29 provideTanStackQuery(queryClient),
30 ],
31 })
32 })
33β
34 afterEach(() => {
35 vi.useRealTimers()
36 })
37β
38 test('Returns number of fetching queries', async () => {
39 const isFetching = TestBed.runInInjectionContext(() => {
40 injectQuery(() => ({
41 queryKey: ['isFetching1'],
42 queryFn: delayedFetcher(100),
43 }))
44 return injectIsFetching()
45 })
46β
47 vi.advanceTimersByTime(1)
48β
49 expect(isFetching()).toStrictEqual(1)
50 await resolveQueries()
51 expect(isFetching()).toStrictEqual(0)
52 })
53β
54 describe('injection context', () => {
55 test('throws NG0203 with descriptive error outside injection context', () => {
56 expect(() => {
57 injectIsFetching()
58 }).toThrowError(/NG0203(.*?)injectIsFetching/)
59 })
60β
61 test('can be used outside injection context when passing an injector', () => {
62 expect(
63 injectIsFetching(undefined, {
64 injector: TestBed.inject(Injector),
65 }),
66 ).not.toThrow()
67 })
68 })
69})