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

inject-is-fetching.test.ts

129 lines3.7 KB

1import { Component, input, inputBinding, signal } from '@angular/core'
2import { TestBed } from '@angular/core/testing'
3import { createKey, sleep } from '@tanstack/query-test-utils'
4import { render } from '@testing-library/angular'
5import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
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 = createKey()
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 await vi.advanceTimersByTimeAsync(101)
51 rendered.fixture.detectChanges()
52 expect(rendered.getByText('fetching: 0')).toBeInTheDocument()
53 })
54​
55 it('should return a read-only signal', () => {
56 const isFetching = TestBed.runInInjectionContext(() => injectIsFetching())
57​
58 expect(isFetching).not.toHaveProperty('set')
59 })
60​
61 it('should be able to filter by queryKey', async () => {
62 const key1 = createKey()
63 const key2 = createKey()
64​
65 @Component({
66 template: `<div>fetching: {{ isFetching() }}</div>`,
67 })
68 class Page {
69 readonly query1 = injectQuery(() => ({
70 queryKey: key1,
71 queryFn: () => sleep(10).then(() => 'test1'),
72 }))
73 readonly query2 = injectQuery(() => ({
74 queryKey: key2,
75 queryFn: () => sleep(100).then(() => 'test2'),
76 }))
77 readonly isFetching = injectIsFetching(() => ({ queryKey: key1 }))
78 }
79​
80 const rendered = await render(Page)
81​
82 await vi.advanceTimersByTimeAsync(0)
83 rendered.fixture.detectChanges()
84 expect(rendered.getByText('fetching: 1')).toBeInTheDocument()
85 await vi.advanceTimersByTimeAsync(11)
86 rendered.fixture.detectChanges()
87 expect(rendered.getByText('fetching: 0')).toBeInTheDocument()
88 })
89​
90 it('should support signal reads in filter predicates', async () => {
91 const key = createKey()
92​
93 void queryClient.query({
94 queryKey: key,
95 queryFn: () => sleep(100).then(() => 'data'),
96 })
97​
98 @Component({
99 template: `<div>fetching: {{ isFetching() }}</div>`,
100 })
101 class Page {
102 readonly includeQuery = input.required<boolean>()
103 readonly isFetching = injectIsFetching(() => ({
104 predicate: () => this.includeQuery(),
105 }))
106 }
107​
108 const subscribe = vi.spyOn(queryClient.getQueryCache(), 'subscribe')
109 const includeQuery = signal(true)
110 const rendered = await render(Page, {
111 bindings: [inputBinding('includeQuery', includeQuery)],
112 })
113​
114 expect(rendered.getByText('fetching: 1')).toBeInTheDocument()
115​
116 includeQuery.set(false)
117 rendered.fixture.detectChanges()
118 expect(subscribe).toHaveBeenCalledTimes(1)
119 expect(rendered.getByText('fetching: 0')).toBeInTheDocument()
120 })
121​
122 describe('injection context', () => {
123 it('should throw NG0203 with descriptive error outside injection context', () => {
124 expect(() => {
125 injectIsFetching()
126 }).toThrow(/NG0203(.*?)injectIsFetching/)
127 })
128 })
129})