Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { QueryClient, onlineManager } from '@tanstack/query-core'
3import { fireEvent, render, within } from '@solidjs/testing-library'
4import DevtoolsComponent from '../DevtoolsComponent'
5β
6// `solid-transition-group` internally imports from
7// `@solid-primitives/transition-group`, whose `exports` field points at
8// `src/index.ts` (not published) under a `@solid-primitives/source` condition
9// that Vite can't fall through, so we stub it with a transparent pass-through.
10vi.mock('solid-transition-group', () => ({
11 TransitionGroup: (props: { children: unknown }) => props.children,
12}))
13β
14// `goober` compiles every `css\`...\`` template literal at mount time, which
15// dominates mount cost and produces no value for label/role-based
16// assertions, so we replace it with a no-op factory.
17vi.mock('goober', () => {
18 let counter = 0
19 const css = Object.assign(() => `tsqd-${++counter}`, {
20 bind: () => css,
21 })
22 return { css, glob: () => {}, setup: () => {} }
23})
24β
25// Regression tests for https://github.com/TanStack/query/issues/9681 β
26// selection, panel width, offline-mocking, and cache-subscription state used
27// to live in module-level signals/maps shared by every mounted Devtools
28// instance, so actions in one panel leaked into every other panel on the
29// page (e.g. two panels pointing at two different `QueryClient`s).
30describe('multiple Devtools instances', () => {
31 const storage: { [key: string]: string } = {}
32 let queryClientA: QueryClient
33 let queryClientB: QueryClient
34β
35 beforeEach(() => {
36 vi.stubGlobal('localStorage', {
37 getItem: (key: string) =>
38 Object.prototype.hasOwnProperty.call(storage, key)
39 ? storage[key]
40 : null,
41 setItem: (key: string, value: string) => {
42 storage[key] = value
43 },
44 removeItem: (key: string) => {
45 delete storage[key]
46 },
47 clear: () => {
48 Object.keys(storage).forEach((key) => delete storage[key])
49 },
50 })
51 vi.stubGlobal(
52 'matchMedia',
53 vi.fn().mockImplementation((query: string) => ({
54 matches: false,
55 media: query,
56 onchange: null,
57 addEventListener: vi.fn(),
58 removeEventListener: vi.fn(),
59 addListener: vi.fn(),
60 removeListener: vi.fn(),
61 dispatchEvent: vi.fn(),
62 })),
63 )
64 vi.stubGlobal(
65 'ResizeObserver',
66 class {
67 observe = vi.fn()
68 unobserve = vi.fn()
69 disconnect = vi.fn()
70 },
71 )
72 queryClientA = new QueryClient()
73 queryClientB = new QueryClient()
74 })
75β
76 afterEach(() => {
77 vi.unstubAllGlobals()
78 Object.keys(storage).forEach((key) => delete storage[key])
79 queryClientA.clear()
80 queryClientB.clear()
81 })
82β
83 function renderTwoInstances() {
84 // Both clients use the same query key on purpose: this is exactly the
85 // scenario that leaked through the old module-level `queryCacheMap`,
86 // where a notify from client A's cache could invoke client B's row
87 // callback with client A's cache accessor.
88 queryClientA.setQueryData(['shared-key'], { owner: 'A' })
89 queryClientB.setQueryData(['shared-key'], { owner: 'B' })
90β
91 return render(() => (
92 <>
93 <DevtoolsComponent
94 client={queryClientA}
95 queryFlavor="TanStack Query"
96 version="5"
97 onlineManager={onlineManager}
98 initialIsOpen={true}
99 />
100 <DevtoolsComponent
101 client={queryClientB}
102 queryFlavor="TanStack Query"
103 version="5"
104 onlineManager={onlineManager}
105 initialIsOpen={true}
106 />
107 </>
108 ))
109 }
110β
111 it('should not open the query details panel in instance B when a row is selected in instance A', () => {
112 const rendered = renderTwoInstances()
113 const panels = rendered.getAllByLabelText('Tanstack query devtools')
114 expect(panels).toHaveLength(2)
115 const [panelA, panelB] = panels
116β
117 fireEvent.click(within(panelA).getByLabelText(/Query key \["shared-key"\]/))
118β
119 expect(within(panelA).getByText('Query Details')).toBeInTheDocument()
120 expect(within(panelB).queryByText('Query Details')).not.toBeInTheDocument()
121 })
122β
123 it("should show each instance's own data in its query details, not the other instance's", () => {
124 const rendered = renderTwoInstances()
125 const panels = rendered.getAllByLabelText('Tanstack query devtools')
126 const [panelA, panelB] = panels
127β
128 fireEvent.click(within(panelA).getByLabelText(/Query key \["shared-key"\]/))
129 fireEvent.click(within(panelB).getByLabelText(/Query key \["shared-key"\]/))
130β
131 // The "Data" explorer field is editable, so string values render as an
132 // `<input>` rather than plain text.
133 expect(within(panelA).getByDisplayValue('A')).toBeInTheDocument()
134 expect(within(panelB).getByDisplayValue('B')).toBeInTheDocument()
135 })
136β
137 it("should not leak client A's cache updates into client B's row data", () => {
138 const rendered = renderTwoInstances()
139 const panels = rendered.getAllByLabelText('Tanstack query devtools')
140 const [panelA, panelB] = panels
141β
142 fireEvent.click(within(panelB).getByLabelText(/Query key \["shared-key"\]/))
143 expect(within(panelB).getByDisplayValue('B')).toBeInTheDocument()
144β
145 // Notify only client A's cache. Under the old module-level
146 // `queryCacheMap`, this would also invoke B's row/detail callbacks with
147 // A's cache accessor, overwriting B's displayed data with A's.
148 queryClientA.setQueryData(['shared-key'], { owner: 'A-updated' })
149β
150 expect(within(panelB).getByDisplayValue('B')).toBeInTheDocument()
151 expect(
152 within(panelB).queryByDisplayValue('A-updated'),
153 ).not.toBeInTheDocument()
154 })
155})