|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { QueryClient, onlineManager } from '@tanstack/query-core' |
| 3 | +import { render } from '@solidjs/testing-library' |
| 4 | +import DevtoolsPanelComponent from '../DevtoolsPanelComponent' |
| 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. |
| 10 | +vi.mock('solid-transition-group', () => ({ |
| 11 | + TransitionGroup: (props: { children: unknown }) => props.children, |
| 12 | +})) |
| 13 | + |
| 14 | +// `goober` compiles every `css\`...\`` template literal at mount time |
| 15 | +// (template parsing + class hashing + style serialization), which |
| 16 | +// dominates mount cost and produces no value for label/role-based |
| 17 | +// assertions, so we replace it with a no-op factory. |
| 18 | +vi.mock('goober', () => { |
| 19 | + let counter = 0 |
| 20 | + const css = Object.assign(() => `tsqd-${++counter}`, { |
| 21 | + bind: () => css, |
| 22 | + }) |
| 23 | + return { css, glob: () => {}, setup: () => {} } |
| 24 | +}) |
| 25 | + |
| 26 | +describe('DevtoolsPanelComponent', () => { |
| 27 | + const storage: { [key: string]: string } = {} |
| 28 | + let queryClient: QueryClient |
| 29 | + let previousRootFontSize = '' |
| 30 | + |
| 31 | + beforeEach(() => { |
| 32 | + previousRootFontSize = document.documentElement.style.fontSize |
| 33 | + vi.stubGlobal('localStorage', { |
| 34 | + getItem: (key: string) => |
| 35 | + Object.prototype.hasOwnProperty.call(storage, key) |
| 36 | + ? storage[key] |
| 37 | + : null, |
| 38 | + setItem: (key: string, value: string) => { |
| 39 | + storage[key] = value |
| 40 | + }, |
| 41 | + removeItem: (key: string) => { |
| 42 | + delete storage[key] |
| 43 | + }, |
| 44 | + clear: () => { |
| 45 | + Object.keys(storage).forEach((key) => delete storage[key]) |
| 46 | + }, |
| 47 | + }) |
| 48 | + vi.stubGlobal( |
| 49 | + 'matchMedia', |
| 50 | + vi.fn().mockImplementation((query: string) => ({ |
| 51 | + matches: false, |
| 52 | + media: query, |
| 53 | + onchange: null, |
| 54 | + addEventListener: vi.fn(), |
| 55 | + removeEventListener: vi.fn(), |
| 56 | + addListener: vi.fn(), |
| 57 | + removeListener: vi.fn(), |
| 58 | + dispatchEvent: vi.fn(), |
| 59 | + })), |
| 60 | + ) |
| 61 | + vi.stubGlobal( |
| 62 | + 'ResizeObserver', |
| 63 | + class { |
| 64 | + observe = vi.fn() |
| 65 | + unobserve = vi.fn() |
| 66 | + disconnect = vi.fn() |
| 67 | + }, |
| 68 | + ) |
| 69 | + queryClient = new QueryClient() |
| 70 | + document.documentElement.style.fontSize = '16px' |
| 71 | + }) |
| 72 | + |
| 73 | + afterEach(() => { |
| 74 | + vi.unstubAllGlobals() |
| 75 | + Object.keys(storage).forEach((key) => delete storage[key]) |
| 76 | + queryClient.clear() |
| 77 | + document.documentElement.style.fontSize = previousRootFontSize |
| 78 | + }) |
| 79 | + |
| 80 | + it('should render the panel without throwing', () => { |
| 81 | + expect(() => |
| 82 | + render(() => ( |
| 83 | + <DevtoolsPanelComponent |
| 84 | + client={queryClient} |
| 85 | + queryFlavor="TanStack Query" |
| 86 | + version="5" |
| 87 | + onlineManager={onlineManager} |
| 88 | + /> |
| 89 | + )), |
| 90 | + ).not.toThrow() |
| 91 | + }) |
| 92 | + |
| 93 | + it('should not render the open devtools button in panel-only mode', () => { |
| 94 | + const rendered = render(() => ( |
| 95 | + <DevtoolsPanelComponent |
| 96 | + client={queryClient} |
| 97 | + queryFlavor="TanStack Query" |
| 98 | + version="5" |
| 99 | + onlineManager={onlineManager} |
| 100 | + /> |
| 101 | + )) |
| 102 | + |
| 103 | + expect( |
| 104 | + rendered.queryByLabelText('Open Tanstack query devtools'), |
| 105 | + ).not.toBeInTheDocument() |
| 106 | + }) |
| 107 | + |
| 108 | + it('should call "onClose" when the close button is clicked', () => { |
| 109 | + const onClose = vi.fn() |
| 110 | + const rendered = render(() => ( |
| 111 | + <DevtoolsPanelComponent |
| 112 | + client={queryClient} |
| 113 | + queryFlavor="TanStack Query" |
| 114 | + version="5" |
| 115 | + onlineManager={onlineManager} |
| 116 | + onClose={onClose} |
| 117 | + /> |
| 118 | + )) |
| 119 | + |
| 120 | + rendered.getByLabelText('Close Tanstack query devtools').click() |
| 121 | + |
| 122 | + expect(onClose).toHaveBeenCalledTimes(1) |
| 123 | + }) |
| 124 | +}) |
0 commit comments