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

DevtoolsIsolation.test.tsx

214 lines7.1 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { QueryClient, onlineManager } from '@tanstack/query-core'
3import { fireEvent, render } from '@solidjs/testing-library'
4import DevtoolsComponent from '../DevtoolsComponent'
5import DevtoolsPanelComponent from '../DevtoolsPanelComponent'
6import type { render as renderType } from '@solidjs/testing-library'
7​
8// `solid-transition-group` internally imports from
9// `@solid-primitives/transition-group`, whose `exports` field points at
10// `src/index.ts` (not published) under a `@solid-primitives/source` condition
11// that Vite can't fall through, so we stub it with a transparent pass-through.
12vi.mock('solid-transition-group', () => ({
13 TransitionGroup: (props: { children: unknown }) => props.children,
14}))
15​
16describe('Devtools instance isolation', () => {
17 const storage: { [key: string]: string } = {}
18 let clientA: QueryClient
19 let clientB: QueryClient
20 let renderedA: ReturnType<typeof renderType> | undefined
21 let renderedB: ReturnType<typeof renderType> | undefined
22​
23 beforeEach(() => {
24 vi.stubGlobal('localStorage', {
25 getItem: (key: string) =>
26 Object.prototype.hasOwnProperty.call(storage, key)
27 ? storage[key]
28 : null,
29 setItem: (key: string, value: string) => {
30 storage[key] = value
31 },
32 removeItem: (key: string) => {
33 delete storage[key]
34 },
35 clear: () => {
36 Object.keys(storage).forEach((key) => delete storage[key])
37 },
38 })
39 vi.stubGlobal(
40 'matchMedia',
41 vi.fn().mockImplementation((query: string) => ({
42 matches: false,
43 media: query,
44 onchange: null,
45 addEventListener: vi.fn(),
46 removeEventListener: vi.fn(),
47 addListener: vi.fn(),
48 removeListener: vi.fn(),
49 dispatchEvent: vi.fn(),
50 })),
51 )
52 vi.stubGlobal(
53 'ResizeObserver',
54 class {
55 observe = vi.fn()
56 unobserve = vi.fn()
57 disconnect = vi.fn()
58 },
59 )
60 clientA = new QueryClient()
61 clientB = new QueryClient()
62 // Both clients hold a query with the same key, mirroring the issue's
63 // repro where each panel has its own QueryClient with similar queries.
64 clientA.setQueryData(['shared'], { owner: 'a' })
65 clientB.setQueryData(['shared'], { owner: 'b' })
66 })
67​
68 afterEach(() => {
69 renderedA?.unmount()
70 renderedB?.unmount()
71 renderedA = undefined
72 renderedB = undefined
73 vi.unstubAllGlobals()
74 Object.keys(storage).forEach((key) => delete storage[key])
75 clientA.clear()
76 clientB.clear()
77 })
78​
79 function renderPair() {
80 renderedA = render(() => (
81 <DevtoolsComponent
82 client={clientA}
83 queryFlavor="TanStack Query"
84 version="5"
85 onlineManager={onlineManager}
86 initialIsOpen={true}
87 />
88 ))
89 renderedB = render(() => (
90 <DevtoolsComponent
91 client={clientB}
92 queryFlavor="TanStack Query"
93 version="5"
94 onlineManager={onlineManager}
95 initialIsOpen={true}
96 />
97 ))
98 }
99​
100 function detailsOf(rendered: ReturnType<typeof renderType>): Element | null {
101 return rendered.container.querySelector('.tsqd-query-details-container')
102 }
103​
104 // The Data Explorer renders string values as editable inputs, so the
105 // displayed data value is read from the input rather than textContent.
106 function dataOwnerValue(
107 rendered: ReturnType<typeof renderType>,
108 ): string | undefined {
109 const input = detailsOf(rendered)?.querySelector('input')
110 return input ? (input as HTMLInputElement).value : undefined
111 }
112​
113 it('selecting a query in one panel does not affect the other panel', () => {
114 renderPair()
115​
116 // Both panels render their own query list
117 expect(
118 renderedA!.getByLabelText(/Query key \["shared"\]/),
119 ).toBeInTheDocument()
120 expect(
121 renderedB!.getByLabelText(/Query key \["shared"\]/),
122 ).toBeInTheDocument()
123​
124 // Select the query in panel A
125 fireEvent.click(renderedA!.getByLabelText(/Query key \["shared"\]/))
126​
127 // Panel A shows the query details...
128 expect(detailsOf(renderedA!)).toBeInTheDocument()
129 // ...but panel B must stay unaffected (https://github.com/TanStack/query/issues/9681)
130 expect(detailsOf(renderedB!)).not.toBeInTheDocument()
131 })
132​
133 it('deselecting in one panel does not clear the selection of the other panel', () => {
134 renderPair()
135​
136 fireEvent.click(renderedA!.getByLabelText(/Query key \["shared"\]/))
137 fireEvent.click(renderedB!.getByLabelText(/Query key \["shared"\]/))
138​
139 // Both panels show their own details
140 expect(detailsOf(renderedA!)).toBeInTheDocument()
141 expect(detailsOf(renderedB!)).toBeInTheDocument()
142​
143 // Deselect in panel A by clicking the row again
144 fireEvent.click(renderedA!.getByLabelText(/Query key \["shared"\]/))
145​
146 // Panel A hides its details, panel B keeps its own selection
147 expect(detailsOf(renderedA!)).not.toBeInTheDocument()
148 expect(detailsOf(renderedB!)).toBeInTheDocument()
149 })
150​
151 it('cache updates in one client do not leak into the other panel', () => {
152 renderPair()
153​
154 // Both panels show details for their own client's `["shared"]` query
155 fireEvent.click(renderedA!.getByLabelText(/Query key \["shared"\]/))
156 fireEvent.click(renderedB!.getByLabelText(/Query key \["shared"\]/))
157 expect(dataOwnerValue(renderedA!)).toBe('a')
158 expect(dataOwnerValue(renderedB!)).toBe('b')
159​
160 // Update the query in client A only
161 clientA.setQueryData(['shared'], { owner: 'a-updated' })
162​
163 // Panel A reflects the update...
164 expect(dataOwnerValue(renderedA!)).toBe('a-updated')
165 // ...but panel B must keep showing its own client's data
166 expect(dataOwnerValue(renderedB!)).toBe('b')
167 })
168​
169 it('unmounting one panel does not break cache updates in the other panel', () => {
170 renderPair()
171​
172 fireEvent.click(renderedB!.getByLabelText(/Query key \["shared"\]/))
173 expect(detailsOf(renderedB!)).toBeInTheDocument()
174​
175 // Unmount panel A entirely; its cleanup must not wipe panel B's
176 // cache-subscription registrations
177 renderedA!.unmount()
178 renderedA = undefined
179​
180 clientB.setQueryData(['shared'], { owner: 'b-updated' })
181 expect(dataOwnerValue(renderedB!)).toBe('b-updated')
182 })
183​
184 it('a panel-only instance reflects the onlineManager status', () => {
185 // DevtoolsPanelComponent renders ContentView without Devtools, so the
186 // offline indicator must be driven by the provider-level subscription
187 const renderedPanel = render(() => (
188 <DevtoolsPanelComponent
189 client={clientA}
190 queryFlavor="TanStack Query"
191 version="5"
192 onlineManager={onlineManager}
193 initialIsOpen={true}
194 />
195 ))
196 try {
197 const offlineButton = () =>
198 renderedPanel.container.querySelector(
199 '.tsqd-action-mock-offline-behavior',
200 )
201​
202 expect(offlineButton()!.getAttribute('aria-pressed')).toBe('false')
203​
204 onlineManager.setOnline(false)
205 expect(offlineButton()!.getAttribute('aria-pressed')).toBe('true')
206​
207 onlineManager.setOnline(true)
208 expect(offlineButton()!.getAttribute('aria-pressed')).toBe('false')
209 } finally {
210 onlineManager.setOnline(true)
211 renderedPanel.unmount()
212 }
213 })
214})