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

inject-devtools-panel.test.ts

191 lines4.6 KB

1import {
2 ElementRef,
3 provideZonelessChangeDetection,
4 signal,
5} from '@angular/core'
6import { TestBed } from '@angular/core/testing'
7import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
8import { QueryClient } from '@tanstack/query-core'
9import { provideTanStackQuery } from '../providers'
10import { injectDevtoolsPanel } from '../devtools-panel'
11​
12const mockDevtoolsPanelInstance = {
13 mount: vi.fn(),
14 unmount: vi.fn(),
15 setClient: vi.fn(),
16 setErrorTypes: vi.fn(),
17 setOnClose: vi.fn(),
19​
20const mocks = vi.hoisted(() => {
21 return {
22 mockTanstackQueryDevtoolsPanel: vi.fn(() => mockDevtoolsPanelInstance),
23 }
24})
25​
26vi.mock('@tanstack/query-devtools', () => ({
27 TanstackQueryDevtoolsPanel: mocks.mockTanstackQueryDevtoolsPanel,
28}))
29​
30describe('injectDevtoolsPanel', () => {
31 let queryClient: QueryClient
32 let mockElementRef: ElementRef
33​
34 const waitForDevtoolsToBeCreated = async () => {
35 await vi.waitFor(() => {
36 expect(mocks.mockTanstackQueryDevtoolsPanel).toHaveBeenCalledTimes(1)
37 })
38 }
39​
40 beforeEach(() => {
41 queryClient = new QueryClient()
42 mockElementRef = new ElementRef(document.createElement('div'))
43 TestBed.configureTestingModule({
44 providers: [
45 provideZonelessChangeDetection(),
46 provideTanStackQuery(queryClient),
47 { provide: ElementRef, useValue: signal(mockElementRef) },
48 ],
49 })
50 })
51​
52 afterEach(() => {
53 vi.restoreAllMocks()
54 })
55​
56 it('should return a DevtoolsPanelRef', () => {
57 const result = TestBed.runInInjectionContext(() => {
58 return injectDevtoolsPanel(() => ({
59 hostElement: TestBed.inject(ElementRef),
60 }))
61 })
62​
63 expect(result).toEqual({
64 destroy: expect.any(Function),
65 })
66 })
67​
68 it('should initialize TanstackQueryDevtoolsPanel', async () => {
69 TestBed.runInInjectionContext(() => {
70 injectDevtoolsPanel(() => ({
71 hostElement: TestBed.inject(ElementRef),
72 }))
73 })
74​
75 TestBed.tick()
76​
77 await waitForDevtoolsToBeCreated()
78​
79 expect(mockDevtoolsPanelInstance.mount).toHaveBeenCalledTimes(1)
80 })
81​
82 it('should destroy TanstackQueryDevtoolsPanel', async () => {
83 const result = TestBed.runInInjectionContext(() => {
84 return injectDevtoolsPanel(() => ({
85 hostElement: TestBed.inject(ElementRef),
86 }))
87 })
88​
89 TestBed.tick()
90​
91 await waitForDevtoolsToBeCreated()
92​
93 result.destroy()
94​
95 expect(mockDevtoolsPanelInstance.unmount).toHaveBeenCalledTimes(1)
96 })
97​
98 it('should destroy TanstackQueryDevtoolsPanel when hostElement is removed', async () => {
99 const hostElement = signal<ElementRef>(mockElementRef)
100​
101 TestBed.runInInjectionContext(() => {
102 return injectDevtoolsPanel(() => ({
103 hostElement: hostElement(),
104 }))
105 })
106​
107 TestBed.tick()
108​
109 await waitForDevtoolsToBeCreated()
110​
111 expect(mockDevtoolsPanelInstance.unmount).toHaveBeenCalledTimes(0)
112​
113 hostElement.set(null as unknown as ElementRef)
114​
115 TestBed.tick()
116​
117 expect(mockDevtoolsPanelInstance.unmount).toHaveBeenCalledTimes(1)
118 })
119​
120 it('should update client', async () => {
121 const client = signal(new QueryClient())
122​
123 TestBed.runInInjectionContext(() => {
124 return injectDevtoolsPanel(() => ({
125 hostElement: TestBed.inject(ElementRef),
126 client: client(),
127 }))
128 })
129​
130 TestBed.tick()
131​
132 await waitForDevtoolsToBeCreated()
133​
134 expect(mockDevtoolsPanelInstance.setClient).toHaveBeenCalledTimes(0)
135​
136 client.set(new QueryClient())
137​
138 TestBed.tick()
139​
140 expect(mockDevtoolsPanelInstance.setClient).toHaveBeenCalledTimes(1)
141 })
142​
143 it('should update error types', async () => {
144 const errorTypes = signal([])
145​
146 TestBed.runInInjectionContext(() => {
147 return injectDevtoolsPanel(() => ({
148 hostElement: TestBed.inject(ElementRef),
149 errorTypes: errorTypes(),
150 }))
151 })
152​
153 TestBed.tick()
154​
155 await waitForDevtoolsToBeCreated()
156​
157 expect(mockDevtoolsPanelInstance.setErrorTypes).toHaveBeenCalledTimes(0)
158​
159 errorTypes.set([])
160​
161 TestBed.tick()
162​
163 expect(mockDevtoolsPanelInstance.setErrorTypes).toHaveBeenCalledTimes(1)
164 })
165​
166 it('should update onclose', async () => {
167 const functionA = () => {}
168 const functionB = () => {}
169​
170 const onClose = signal(functionA)
171​
172 TestBed.runInInjectionContext(() => {
173 return injectDevtoolsPanel(() => ({
174 hostElement: TestBed.inject(ElementRef),
175 onClose: onClose(),
176 }))
177 })
178​
179 TestBed.tick()
180​
181 await waitForDevtoolsToBeCreated()
182​
183 expect(mockDevtoolsPanelInstance.setOnClose).toHaveBeenCalledTimes(0)
184​
185 onClose.set(functionB)
186​
187 TestBed.tick()
188​
189 expect(mockDevtoolsPanelInstance.setOnClose).toHaveBeenCalledTimes(1)
190 })
191})