-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
test(query-devtools/DevtoolsPanelComponent): add tests for rendering without throwing, panel-only mode, and 'onClose' prop #10680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
packages/query-devtools/src/__tests__/DevtoolsPanelComponent.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { QueryClient, onlineManager } from '@tanstack/query-core' | ||
| import { render } from '@solidjs/testing-library' | ||
| import DevtoolsPanelComponent from '../DevtoolsPanelComponent' | ||
|
|
||
| // `solid-transition-group` internally imports from | ||
| // `@solid-primitives/transition-group`, whose `exports` field points at | ||
| // `src/index.ts` (not published) under a `@solid-primitives/source` condition | ||
| // that Vite can't fall through, so we stub it with a transparent pass-through. | ||
| vi.mock('solid-transition-group', () => ({ | ||
| TransitionGroup: (props: { children: unknown }) => props.children, | ||
| })) | ||
|
|
||
| // `goober` compiles every `css\`...\`` template literal at mount time | ||
| // (template parsing + class hashing + style serialization), which | ||
| // dominates mount cost and produces no value for label/role-based | ||
| // assertions, so we replace it with a no-op factory. | ||
| vi.mock('goober', () => { | ||
| let counter = 0 | ||
| const css = Object.assign(() => `tsqd-${++counter}`, { | ||
| bind: () => css, | ||
| }) | ||
| return { css, glob: () => {}, setup: () => {} } | ||
| }) | ||
|
|
||
| describe('DevtoolsPanelComponent', () => { | ||
| const storage: { [key: string]: string } = {} | ||
| let queryClient: QueryClient | ||
| let previousRootFontSize = '' | ||
|
|
||
| beforeEach(() => { | ||
| previousRootFontSize = document.documentElement.style.fontSize | ||
| vi.stubGlobal('localStorage', { | ||
| getItem: (key: string) => | ||
| Object.prototype.hasOwnProperty.call(storage, key) | ||
| ? storage[key] | ||
| : null, | ||
| setItem: (key: string, value: string) => { | ||
| storage[key] = value | ||
| }, | ||
| removeItem: (key: string) => { | ||
| delete storage[key] | ||
| }, | ||
| clear: () => { | ||
| Object.keys(storage).forEach((key) => delete storage[key]) | ||
| }, | ||
| }) | ||
| vi.stubGlobal( | ||
| 'matchMedia', | ||
| vi.fn().mockImplementation((query: string) => ({ | ||
| matches: false, | ||
| media: query, | ||
| onchange: null, | ||
| addEventListener: vi.fn(), | ||
| removeEventListener: vi.fn(), | ||
| addListener: vi.fn(), | ||
| removeListener: vi.fn(), | ||
| dispatchEvent: vi.fn(), | ||
| })), | ||
| ) | ||
| vi.stubGlobal( | ||
| 'ResizeObserver', | ||
| class { | ||
| observe = vi.fn() | ||
| unobserve = vi.fn() | ||
| disconnect = vi.fn() | ||
| }, | ||
| ) | ||
| queryClient = new QueryClient() | ||
| document.documentElement.style.fontSize = '16px' | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals() | ||
| Object.keys(storage).forEach((key) => delete storage[key]) | ||
| queryClient.clear() | ||
| document.documentElement.style.fontSize = previousRootFontSize | ||
| }) | ||
|
|
||
| it('should render the panel without throwing', () => { | ||
| expect(() => | ||
| render(() => ( | ||
| <DevtoolsPanelComponent | ||
| client={queryClient} | ||
| queryFlavor="TanStack Query" | ||
| version="5" | ||
| onlineManager={onlineManager} | ||
| /> | ||
| )), | ||
| ).not.toThrow() | ||
| }) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| it('should not render the open devtools button in panel-only mode', () => { | ||
| const rendered = render(() => ( | ||
| <DevtoolsPanelComponent | ||
| client={queryClient} | ||
| queryFlavor="TanStack Query" | ||
| version="5" | ||
| onlineManager={onlineManager} | ||
| /> | ||
| )) | ||
|
|
||
| expect( | ||
| rendered.queryByLabelText('Open Tanstack query devtools'), | ||
| ).not.toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('should call "onClose" when the close button is clicked', () => { | ||
| const onClose = vi.fn() | ||
| const rendered = render(() => ( | ||
| <DevtoolsPanelComponent | ||
| client={queryClient} | ||
| queryFlavor="TanStack Query" | ||
| version="5" | ||
| onlineManager={onlineManager} | ||
| onClose={onClose} | ||
| /> | ||
| )) | ||
|
|
||
| rendered.getByLabelText('Close Tanstack query devtools').click() | ||
|
|
||
| expect(onClose).toHaveBeenCalledTimes(1) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.