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

inject-devtools-panel.test.ts

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