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

inject-devtools-panel.test.ts

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