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

inject-devtools-panel.test.ts

170 lines4.2 KB

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