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

inject-is-fetching.test.ts

131 lines3.7 KB

1import { TestBed } from '@angular/core/testing'
2import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
3import { Component, input, inputBinding, signal } from '@angular/core'
4import { render } from '@testing-library/angular'
5import { queryKey, sleep } from '@tanstack/query-test-utils'
6import {
7 QueryClient,
8 injectIsFetching,
9 injectQuery,
10 provideTanStackQuery,
11} from '..'
12import { provideAngularQueryChangeDetection } from './test-utils'
13​
14describe('injectIsFetching', () => {
15 let queryClient: QueryClient
16​
17 beforeEach(() => {
18 vi.useFakeTimers()
19 queryClient = new QueryClient()
20​
21 TestBed.configureTestingModule({
22 providers: [
23 provideAngularQueryChangeDetection(),
24 provideTanStackQuery(() => queryClient),
25 ],
26 })
27 })
28​
29 afterEach(() => {
30 vi.useRealTimers()
31 })
32​
33 it('should return the number of fetching queries', async () => {
34 const key = queryKey()
35​
36 @Component({
37 template: `<div>fetching: {{ isFetching() }}</div>`,
38 })
39 class Page {
40 readonly query = injectQuery(() => ({
41 queryKey: key,
42 queryFn: () => sleep(100).then(() => 'Some data'),
43 }))
44 readonly isFetching = injectIsFetching()
45 }
46​
47 const rendered = await render(Page)
48​
49 expect(rendered.getByText('fetching: 1')).toBeInTheDocument()
50​
51 await vi.advanceTimersByTimeAsync(101)
52 rendered.fixture.detectChanges()
53 expect(rendered.getByText('fetching: 0')).toBeInTheDocument()
54 })
55​
56 it('should return a read-only signal', () => {
57 const isFetching = TestBed.runInInjectionContext(() => injectIsFetching())
58​
59 expect(isFetching).not.toHaveProperty('set')
60 })
61​
62 it('should be able to filter by queryKey', async () => {
63 const key1 = queryKey()
64 const key2 = queryKey()
65​
66 @Component({
67 template: `<div>fetching: {{ isFetching() }}</div>`,
68 })
69 class Page {
70 readonly query1 = injectQuery(() => ({
71 queryKey: key1,
72 queryFn: () => sleep(10).then(() => 'test1'),
73 }))
74 readonly query2 = injectQuery(() => ({
75 queryKey: key2,
76 queryFn: () => sleep(100).then(() => 'test2'),
77 }))
78 readonly isFetching = injectIsFetching(() => ({ queryKey: key1 }))
79 }
80​
81 const rendered = await render(Page)
82​
83 await vi.advanceTimersByTimeAsync(0)
84 rendered.fixture.detectChanges()
85 expect(rendered.getByText('fetching: 1')).toBeInTheDocument()
86​
87 await vi.advanceTimersByTimeAsync(11)
88 rendered.fixture.detectChanges()
89 expect(rendered.getByText('fetching: 0')).toBeInTheDocument()
90 })
91​
92 it('should support signal reads in filter predicates', async () => {
93 const key = queryKey()
94​
95 void queryClient.query({
96 queryKey: key,
97 queryFn: () => sleep(100).then(() => 'data'),
98 })
99​
100 @Component({
101 template: `<div>fetching: {{ isFetching() }}</div>`,
102 })
103 class Page {
104 readonly includeQuery = input.required<boolean>()
105 readonly isFetching = injectIsFetching(() => ({
106 predicate: () => this.includeQuery(),
107 }))
108 }
109​
110 const subscribe = vi.spyOn(queryClient.getQueryCache(), 'subscribe')
111 const includeQuery = signal(true)
112 const rendered = await render(Page, {
113 bindings: [inputBinding('includeQuery', includeQuery)],
114 })
115​
116 expect(rendered.getByText('fetching: 1')).toBeInTheDocument()
117​
118 includeQuery.set(false)
119 rendered.fixture.detectChanges()
120 expect(subscribe).toHaveBeenCalledTimes(1)
121 expect(rendered.getByText('fetching: 0')).toBeInTheDocument()
122 })
123​
124 describe('injection context', () => {
125 it('should throw NG0203 with descriptive error outside injection context', () => {
126 expect(() => {
127 injectIsFetching()
128 }).toThrow(/NG0203(.*?)injectIsFetching/)
129 })
130 })
131})