Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { fireEvent, render, within } from '@solidjs/testing-library'
3import { QueryClient, onlineManager } from '@tanstack/query-core'
4import Explorer from '../Explorer'
5import { QueryDevtoolsContext, ThemeContext } from '../contexts'
6import type { Query } from '@tanstack/query-core'
7β
8// `goober` compiles every `css\`...\`` template literal at mount time;
9// replace it with a no-op factory so label/role-based assertions stay fast.
10vi.mock('goober', () => {
11 let counter = 0
12 const css = Object.assign(() => `tsqd-${++counter}`, {
13 bind: () => css,
14 })
15 return { css, glob: () => {}, setup: () => {} }
16})
17β
18describe('Explorer', () => {
19 let queryClient: QueryClient
20β
21 beforeEach(() => {
22 vi.useFakeTimers()
23 queryClient = new QueryClient()
24 })
25β
26 afterEach(() => {
27 queryClient.clear()
28 vi.useRealTimers()
29 })
30β
31 function renderExplorer(
32 props: Parameters<typeof Explorer>[0],
33 options: { theme?: 'dark' | 'light' } = {},
34 ) {
35 const theme = options.theme ?? 'dark'
36 return render(() => (
37 <QueryDevtoolsContext.Provider
38 value={{
39 client: queryClient,
40 queryFlavor: 'TanStack Query',
41 version: '5',
42 onlineManager,
43 }}
44 >
45 <ThemeContext.Provider value={() => theme}>
46 <Explorer {...props} />
47 </ThemeContext.Provider>
48 </QueryDevtoolsContext.Provider>
49 ))
50 }
51β
52 describe('primitive values', () => {
53 it('should render a "label: value" row for a string value', () => {
54 const rendered = renderExplorer({ label: 'name', value: 'Anna' })
55β
56 expect(rendered.getByText('name:')).toBeInTheDocument()
57 expect(rendered.getByText('"Anna"')).toBeInTheDocument()
58 })
59β
60 it('should render a "label: value" row for a number value', () => {
61 const rendered = renderExplorer({ label: 'count', value: 42 })
62β
63 expect(rendered.getByText('count:')).toBeInTheDocument()
64 expect(rendered.getByText('42')).toBeInTheDocument()
65 })
66β
67 it('should render a "label: value" row for a boolean value', () => {
68 const rendered = renderExplorer({ label: 'active', value: true })
69β
70 expect(rendered.getByText('active:')).toBeInTheDocument()
71 expect(rendered.getByText('true')).toBeInTheDocument()
72 })
73β
74 it('should render a "label: value" row for a "null" value', () => {
75 const rendered = renderExplorer({ label: 'missing', value: null })
76β
77 expect(rendered.getByText('missing:')).toBeInTheDocument()
78 expect(rendered.getByText('null')).toBeInTheDocument()
79 })
80 })
81β
82 describe('arrays and objects', () => {
83 it('should render an empty object as a primitive row (no expander)', () => {
84 const rendered = renderExplorer({ label: 'data', value: {} })
85β
86 expect(rendered.getByText('data:')).toBeInTheDocument()
87 expect(rendered.queryByRole('button', { expanded: false })).toBeNull()
88 })
89β
90 it('should render an array with an expander showing the item count', () => {
91 const rendered = renderExplorer({
92 label: 'list',
93 value: ['a', 'b', 'c'],
94 })
95β
96 const expander = rendered.getByRole('button', { expanded: false })
97 expect(expander).toBeInTheDocument()
98 expect(expander.textContent).toContain('list')
99 expect(expander.textContent).toContain('3 items')
100 })
101β
102 it('should render children under their index labels when the array expander is clicked', () => {
103 const rendered = renderExplorer({
104 label: 'list',
105 value: ['a', 'b'],
106 })
107β
108 fireEvent.click(rendered.getByRole('button', { expanded: false }))
109 expect(rendered.getByText('0:')).toBeInTheDocument()
110 expect(rendered.getByText('1:')).toBeInTheDocument()
111 expect(rendered.getByText('"a"')).toBeInTheDocument()
112 expect(rendered.getByText('"b"')).toBeInTheDocument()
113 })
114β
115 it('should render object entries under their keys when expanded', () => {
116 const rendered = renderExplorer({
117 label: 'user',
118 value: { name: 'Anna', age: 30 },
119 })
120β
121 fireEvent.click(rendered.getByRole('button', { expanded: false }))
122 expect(rendered.getByText('name:')).toBeInTheDocument()
123 expect(rendered.getByText('"Anna"')).toBeInTheDocument()
124 expect(rendered.getByText('age:')).toBeInTheDocument()
125 expect(rendered.getByText('30')).toBeInTheDocument()
126 })
127 })
128β
129 describe('Map and iterable values', () => {
130 it('should preserve "Map" keys as labels when expanded', () => {
131 const rendered = renderExplorer({
132 label: 'm',
133 value: new Map([
134 ['first', 1],
135 ['second', 2],
136 ]),
137 })
138β
139 fireEvent.click(rendered.getByRole('button', { expanded: false }))
140 expect(rendered.getByText('first:')).toBeInTheDocument()
141 expect(rendered.getByText('second:')).toBeInTheDocument()
142 })
143β
144 it('should mark an iterable value with an "(Iterable)" prefix on the expander', () => {
145 const rendered = renderExplorer({
146 label: 's',
147 value: new Set(['x', 'y']),
148 })
149β
150 expect(
151 rendered.getByRole('button', { expanded: false }).textContent,
152 ).toContain('(Iterable)')
153 })
154β
155 it('should render iterable children under their numeric index when expanded', () => {
156 const rendered = renderExplorer({
157 label: 's',
158 value: new Set(['x', 'y']),
159 })
160β
161 fireEvent.click(rendered.getByRole('button', { expanded: false }))
162 expect(rendered.getByText('0:')).toBeInTheDocument()
163 expect(rendered.getByText('1:')).toBeInTheDocument()
164 })
165 })
166β
167 describe('"defaultExpanded"', () => {
168 it('should render children eagerly when the label is in "defaultExpanded"', () => {
169 const rendered = renderExplorer({
170 label: 'list',
171 value: ['a'],
172 defaultExpanded: ['list'],
173 })
174β
175 expect(
176 rendered.getByRole('button', { expanded: true }),
177 ).toBeInTheDocument()
178 expect(rendered.getByText('0:')).toBeInTheDocument()
179 })
180 })
181β
182 describe('action menu', () => {
183 it('should copy the serialized value to the clipboard when the copy button is clicked', () => {
184 const writeText = vi.fn().mockResolvedValue(undefined)
185 vi.stubGlobal('navigator', { clipboard: { writeText } })
186 queryClient.setQueryData(['data'], { name: 'Anna' })
187β
188 const rendered = renderExplorer({
189 label: 'data',
190 value: { name: 'Anna' },
191 editable: true,
192 // oxlint-disable-next-line typescript/no-unnecessary-type-assertion
193 activeQuery: queryClient
194 .getQueryCache()
195 .find({ queryKey: ['data'] }) as Query,
196 })
197β
198 fireEvent.click(rendered.getByLabelText('Copy object to clipboard'))
199 expect(writeText).toHaveBeenCalledTimes(1)
200 const [arg] = writeText.mock.calls[0]!
201 expect(JSON.parse(arg as string)).toMatchObject({
202 json: { name: 'Anna' },
203 })
204 })
205β
206 it('should switch the copy button to an error state when clipboard write fails', async () => {
207 const writeText = vi.fn().mockRejectedValue(new Error('denied'))
208 vi.stubGlobal('navigator', { clipboard: { writeText } })
209 const consoleErrorMock = vi
210 .spyOn(console, 'error')
211 .mockImplementation(() => {})
212 queryClient.setQueryData(['data'], { name: 'Anna' })
213β
214 const rendered = renderExplorer({
215 label: 'data',
216 value: { name: 'Anna' },
217 editable: true,
218 // oxlint-disable-next-line typescript/no-unnecessary-type-assertion
219 activeQuery: queryClient
220 .getQueryCache()
221 .find({ queryKey: ['data'] }) as Query,
222 })
223β
224 fireEvent.click(rendered.getByLabelText('Copy object to clipboard'))
225 await vi.advanceTimersByTimeAsync(0)
226 expect(
227 rendered.getByLabelText('Error copying object to clipboard'),
228 ).toBeInTheDocument()
229 expect(consoleErrorMock).toHaveBeenCalledWith(
230 'Failed to copy: ',
231 new Error('denied'),
232 )
233 })
234β
235 it('should reset the copy button to the idle state 1500ms after a successful copy', async () => {
236 const writeText = vi.fn().mockResolvedValue(undefined)
237 vi.stubGlobal('navigator', { clipboard: { writeText } })
238 queryClient.setQueryData(['data'], { name: 'Anna' })
239β
240 const rendered = renderExplorer({
241 label: 'data',
242 value: { name: 'Anna' },
243 editable: true,
244 // oxlint-disable-next-line typescript/no-unnecessary-type-assertion
245 activeQuery: queryClient
246 .getQueryCache()
247 .find({ queryKey: ['data'] }) as Query,
248 })
249β
250 fireEvent.click(rendered.getByLabelText('Copy object to clipboard'))
251 await vi.advanceTimersByTimeAsync(0)
252 expect(
253 rendered.getByLabelText('Object copied to clipboard'),
254 ).toBeInTheDocument()
255 await vi.advanceTimersByTimeAsync(1500)
256 expect(
257 rendered.getByLabelText('Copy object to clipboard'),
258 ).toBeInTheDocument()
259 })
260β
261 it('should reset the copy button to the idle state 1500ms after a failed copy', async () => {
262 const writeText = vi.fn().mockRejectedValue(new Error('denied'))
263 vi.stubGlobal('navigator', { clipboard: { writeText } })
264 vi.spyOn(console, 'error').mockImplementation(() => {})
265 queryClient.setQueryData(['data'], { name: 'Anna' })
266β
267 const rendered = renderExplorer({
268 label: 'data',
269 value: { name: 'Anna' },
270 editable: true,
271 // oxlint-disable-next-line typescript/no-unnecessary-type-assertion
272 activeQuery: queryClient
273 .getQueryCache()
274 .find({ queryKey: ['data'] }) as Query,
275 })
276β
277 fireEvent.click(rendered.getByLabelText('Copy object to clipboard'))
278 await vi.advanceTimersByTimeAsync(0)
279 expect(
280 rendered.getByLabelText('Error copying object to clipboard'),
281 ).toBeInTheDocument()
282 await vi.advanceTimersByTimeAsync(1500)
283 expect(
284 rendered.getByLabelText('Copy object to clipboard'),
285 ).toBeInTheDocument()
286 })
287β
288 it('should clear array items via "setQueryData" when the clear-array button is clicked', () => {
289 queryClient.setQueryData(['data'], ['a', 'b', 'c'])
290β
291 const rendered = renderExplorer({
292 label: 'list',
293 value: ['a', 'b', 'c'],
294 editable: true,
295 // oxlint-disable-next-line typescript/no-unnecessary-type-assertion
296 activeQuery: queryClient
297 .getQueryCache()
298 .find({ queryKey: ['data'] }) as Query,
299 })
300β
301 fireEvent.click(rendered.getByLabelText('Remove all items'))
302 expect(queryClient.getQueryData(['data'])).toEqual([])
303 })
304β
305 it('should delete the entry at the current "dataPath" when the delete button is clicked', () => {
306 queryClient.setQueryData(['data'], ['a', 'b', 'c'])
307β
308 const rendered = renderExplorer({
309 label: 'list',
310 value: ['a', 'b', 'c'],
311 editable: true,
312 itemsDeletable: true,
313 // oxlint-disable-next-line typescript/no-unnecessary-type-assertion
314 activeQuery: queryClient
315 .getQueryCache()
316 .find({ queryKey: ['data'] }) as Query,
317 dataPath: ['1'],
318 })
319β
320 fireEvent.click(rendered.getByLabelText('Delete item'))
321 expect(queryClient.getQueryData(['data'])).toEqual(['a', 'c'])
322 })
323β
324 it('should toggle a boolean value via "setQueryData" when the toggle button is clicked', () => {
325 queryClient.setQueryData(['data'], { flag: true })
326β
327 const rendered = renderExplorer({
328 label: 'flag',
329 value: true,
330 editable: true,
331 // oxlint-disable-next-line typescript/no-unnecessary-type-assertion
332 activeQuery: queryClient
333 .getQueryCache()
334 .find({ queryKey: ['data'] }) as Query,
335 dataPath: ['flag'],
336 })
337β
338 fireEvent.click(rendered.getByLabelText('Toggle value'))
339 expect(queryClient.getQueryData(['data'])).toEqual({ flag: false })
340 })
341β
342 it('should not render action buttons when "editable" is false', () => {
343 queryClient.setQueryData(['data'], ['a'])
344β
345 const rendered = renderExplorer({
346 label: 'list',
347 value: ['a'],
348 editable: false,
349 // oxlint-disable-next-line typescript/no-unnecessary-type-assertion
350 activeQuery: queryClient
351 .getQueryCache()
352 .find({ queryKey: ['data'] }) as Query,
353 })
354β
355 expect(rendered.queryByLabelText('Copy object to clipboard')).toBeNull()
356 expect(rendered.queryByLabelText('Remove all items')).toBeNull()
357 })
358β
359 it('should not render "ClearArrayButton" when value is not an array', () => {
360 queryClient.setQueryData(['data'], { name: 'Anna' })
361β
362 const rendered = renderExplorer({
363 label: 'user',
364 value: { name: 'Anna' },
365 editable: true,
366 // oxlint-disable-next-line typescript/no-unnecessary-type-assertion
367 activeQuery: queryClient
368 .getQueryCache()
369 .find({ queryKey: ['data'] }) as Query,
370 })
371β
372 expect(rendered.queryByLabelText('Remove all items')).toBeNull()
373 expect(
374 rendered.getByLabelText('Copy object to clipboard'),
375 ).toBeInTheDocument()
376 })
377 })
378β
379 describe('pagination', () => {
380 it('should group entries into 100-item pages when the array has more than 100 entries', () => {
381 const rendered = renderExplorer({
382 label: 'big',
383 value: Array.from({ length: 101 }, (_, i) => i),
384 })
385β
386 fireEvent.click(rendered.getByRole('button', { expanded: false }))
387 expect(rendered.getByText('[0...99]')).toBeInTheDocument()
388 expect(rendered.getByText('[100...199]')).toBeInTheDocument()
389 })
390β
391 it('should keep the items of a page hidden until the page header is clicked', () => {
392 const rendered = renderExplorer({
393 label: 'big',
394 value: Array.from({ length: 101 }, (_, i) => `item-${i}`),
395 })
396β
397 fireEvent.click(rendered.getByRole('button', { expanded: false }))
398 expect(rendered.queryByText('0:')).toBeNull()
399 })
400β
401 it('should reveal the items of a page when the page header is clicked', () => {
402 const rendered = renderExplorer({
403 label: 'big',
404 value: Array.from({ length: 101 }, (_, i) => `item-${i}`),
405 })
406β
407 fireEvent.click(rendered.getByRole('button', { expanded: false }))
408 fireEvent.click(rendered.getByText('[0...99]'))
409 expect(rendered.getByText('0:')).toBeInTheDocument()
410 expect(rendered.getByText('"item-0"')).toBeInTheDocument()
411 })
412β
413 it('should independently toggle two pages when their headers are clicked', () => {
414 const rendered = renderExplorer({
415 label: 'big',
416 value: Array.from({ length: 200 }, (_, i) => `item-${i}`),
417 })
418β
419 fireEvent.click(rendered.getByRole('button', { expanded: false }))
420 fireEvent.click(rendered.getByText('[0...99]'))
421 fireEvent.click(rendered.getByText('[100...199]'))
422 expect(rendered.getByText('"item-0"')).toBeInTheDocument()
423 expect(rendered.getByText('"item-100"')).toBeInTheDocument()
424β
425 fireEvent.click(rendered.getByText('[0...99]'))
426 expect(rendered.queryByText('"item-0"')).toBeNull()
427 expect(rendered.getByText('"item-100"')).toBeInTheDocument()
428 })
429β
430 it('should render action buttons for items inside a paginated page', () => {
431 const value: Array<Array<number>> = Array.from(
432 { length: 200 },
433 (_, i) => [i],
434 )
435 queryClient.setQueryData(['data'], value)
436β
437 const rendered = renderExplorer({
438 label: 'Data',
439 value,
440 defaultExpanded: ['Data'],
441 editable: true,
442 // oxlint-disable-next-line typescript/no-unnecessary-type-assertion
443 activeQuery: queryClient
444 .getQueryCache()
445 .find({ queryKey: ['data'] }) as Query,
446 })
447β
448 fireEvent.click(rendered.getByText('[0...99]'))
449 expect(
450 rendered.getAllByLabelText('Remove all items').length,
451 ).toBeGreaterThan(1)
452 })
453 })
454β
455 describe('inline edit', () => {
456 it('should write the new string value via "setQueryData" when a text input is changed', () => {
457 queryClient.setQueryData(['data'], { name: 'Anna' })
458β
459 const rendered = renderExplorer({
460 label: 'name',
461 value: 'Anna',
462 editable: true,
463 // oxlint-disable-next-line typescript/no-unnecessary-type-assertion
464 activeQuery: queryClient
465 .getQueryCache()
466 .find({ queryKey: ['data'] }) as Query,
467 dataPath: ['name'],
468 })
469β
470 const input = rendered.getByLabelText('name:')
471 expect(input).toHaveAttribute('type', 'text')
472β
473 fireEvent.change(input, { target: { value: 'Bob' } })
474 expect(queryClient.getQueryData(['data'])).toEqual({ name: 'Bob' })
475 })
476β
477 it('should write the new number value via "setQueryData" when a number input is changed', () => {
478 queryClient.setQueryData(['data'], { count: 1 })
479β
480 const rendered = renderExplorer({
481 label: 'count',
482 value: 1,
483 editable: true,
484 // oxlint-disable-next-line typescript/no-unnecessary-type-assertion
485 activeQuery: queryClient
486 .getQueryCache()
487 .find({ queryKey: ['data'] }) as Query,
488 dataPath: ['count'],
489 })
490β
491 const input = rendered.getByLabelText('count:')
492 expect(input).toHaveAttribute('type', 'number')
493β
494 fireEvent.change(input, {
495 target: { value: '42', valueAsNumber: 42 },
496 })
497 expect(queryClient.getQueryData(['data'])).toEqual({ count: 42 })
498 })
499β
500 it('should render "ToggleValueButton" inline for a boolean primitive row', () => {
501 queryClient.setQueryData(['data'], { flag: false })
502β
503 const rendered = renderExplorer({
504 label: 'flag',
505 value: false,
506 editable: true,
507 // oxlint-disable-next-line typescript/no-unnecessary-type-assertion
508 activeQuery: queryClient
509 .getQueryCache()
510 .find({ queryKey: ['data'] }) as Query,
511 dataPath: ['flag'],
512 })
513β
514 expect(rendered.getByLabelText('Toggle value')).toBeInTheDocument()
515 })
516β
517 it('should render "DeleteItemButton" inline when a primitive row has "itemsDeletable"', () => {
518 queryClient.setQueryData(['data'], { name: 'Anna' })
519β
520 const rendered = renderExplorer({
521 label: 'name',
522 value: 'Anna',
523 editable: true,
524 itemsDeletable: true,
525 // oxlint-disable-next-line typescript/no-unnecessary-type-assertion
526 activeQuery: queryClient
527 .getQueryCache()
528 .find({ queryKey: ['data'] }) as Query,
529 dataPath: ['name'],
530 })
531β
532 expect(rendered.getByLabelText('Delete item')).toBeInTheDocument()
533 })
534β
535 it('should delete fields from the active query when their inline delete buttons are clicked', () => {
536 const value = { name: 'Anna', age: 30 }
537 queryClient.setQueryData(['data'], value)
538β
539 const rendered = renderExplorer({
540 label: 'Data',
541 value,
542 defaultExpanded: ['Data'],
543 editable: true,
544 // oxlint-disable-next-line typescript/no-unnecessary-type-assertion
545 activeQuery: queryClient
546 .getQueryCache()
547 .find({ queryKey: ['data'] }) as Query,
548 })
549β
550 const ageRow = rendered.getByText('age:').parentElement!
551 fireEvent.click(within(ageRow).getByLabelText('Delete item'))
552 expect(queryClient.getQueryData(['data'])).toEqual({ name: 'Anna' })
553β
554 const nameRow = rendered.getByText('name:').parentElement!
555 fireEvent.click(within(nameRow).getByLabelText('Delete item'))
556 expect(queryClient.getQueryData(['data'])).toEqual({})
557 })
558 })
559β
560 describe('theme', () => {
561 it('should render without throwing under the "light" theme', () => {
562 const value = { items: ['a'], flag: true }
563 queryClient.setQueryData(['data'], value)
564β
565 expect(() =>
566 renderExplorer(
567 {
568 label: 'Data',
569 value,
570 defaultExpanded: ['Data'],
571 editable: true,
572 // oxlint-disable-next-line typescript/no-unnecessary-type-assertion
573 activeQuery: queryClient
574 .getQueryCache()
575 .find({ queryKey: ['data'] }) as Query,
576 },
577 { theme: 'light' },
578 ),
579 ).not.toThrow()
580 })
581 })
582β
583 describe('"shadowDOMTarget"', () => {
584 it('should render without throwing when a "shadowDOMTarget" is provided', () => {
585 const host = document.createElement('div')
586 document.body.appendChild(host)
587 const shadowRoot = host.attachShadow({ mode: 'open' })
588 const value = { items: ['a'], flag: true }
589 queryClient.setQueryData(['data'], value)
590β
591 try {
592 expect(() =>
593 render(() => (
594 <QueryDevtoolsContext.Provider
595 value={{
596 client: queryClient,
597 queryFlavor: 'TanStack Query',
598 version: '5',
599 onlineManager,
600 shadowDOMTarget: shadowRoot,
601 }}
602 >
603 <ThemeContext.Provider value={() => 'dark'}>
604 <Explorer
605 label="Data"
606 value={value}
607 defaultExpanded={['Data']}
608 editable={true}
609 activeQuery={
610 // oxlint-disable-next-line typescript/no-unnecessary-type-assertion
611 queryClient
612 .getQueryCache()
613 .find({ queryKey: ['data'] }) as Query
614 }
615 />
616 </ThemeContext.Provider>
617 </QueryDevtoolsContext.Provider>
618 )),
619 ).not.toThrow()
620 } finally {
621 host.remove()
622 }
623 })
624 })
625})