-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
test(react-query-devtools): add tests for missing 'QueryClient', context provider, 'client' prop, and production fallback #10627
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
sukvvon
merged 3 commits into
main
from
test/react-query-devtools-add-tests-for-client-and-production-fallback
May 3, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
77 changes: 77 additions & 0 deletions
77
packages/react-query-devtools/src/__tests__/ReactQueryDevtools.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,77 @@ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { render } from '@testing-library/react' | ||
| import { QueryClient, QueryClientProvider } from '@tanstack/react-query' | ||
| import type { TanstackQueryDevtools } from '@tanstack/query-devtools' | ||
|
|
||
| const mountMock = vi.fn() | ||
| const unmountMock = vi.fn() | ||
| const setClientMock = vi.fn() | ||
| const setButtonPositionMock = vi.fn() | ||
| const setPositionMock = vi.fn() | ||
| const setInitialIsOpenMock = vi.fn() | ||
| const setErrorTypesMock = vi.fn() | ||
| const setThemeMock = vi.fn() | ||
|
|
||
| vi.mock('@tanstack/query-devtools', () => ({ | ||
| TanstackQueryDevtools: vi.fn(function (this: TanstackQueryDevtools) { | ||
| this.mount = mountMock | ||
| this.unmount = unmountMock | ||
| this.setClient = setClientMock | ||
| this.setButtonPosition = setButtonPositionMock | ||
| this.setPosition = setPositionMock | ||
| this.setInitialIsOpen = setInitialIsOpenMock | ||
| this.setErrorTypes = setErrorTypesMock | ||
| this.setTheme = setThemeMock | ||
| }), | ||
| })) | ||
|
|
||
| describe('ReactQueryDevtools', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| it('should throw an error if no query client has been set', async () => { | ||
| const { ReactQueryDevtools } = await import('../ReactQueryDevtools') | ||
|
|
||
| expect(() => render(<ReactQueryDevtools />)).toThrow( | ||
| 'No QueryClient set, use QueryClientProvider to set one', | ||
| ) | ||
| }) | ||
|
|
||
| it('should not throw an error if query client is provided via context', async () => { | ||
| const { ReactQueryDevtools } = await import('../ReactQueryDevtools') | ||
| const queryClient = new QueryClient() | ||
|
|
||
| expect(() => | ||
| render( | ||
| <QueryClientProvider client={queryClient}> | ||
| <ReactQueryDevtools /> | ||
| </QueryClientProvider>, | ||
| ), | ||
| ).not.toThrow() | ||
| expect(mountMock).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('should not throw an error if query client is provided via props', async () => { | ||
| const { ReactQueryDevtools } = await import('../ReactQueryDevtools') | ||
| const queryClient = new QueryClient() | ||
|
|
||
| expect(() => | ||
| render(<ReactQueryDevtools client={queryClient} />), | ||
| ).not.toThrow() | ||
| expect(mountMock).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('should return null in non-development environments', async () => { | ||
| vi.stubEnv('NODE_ENV', 'production') | ||
| vi.resetModules() | ||
|
|
||
| try { | ||
| const { ReactQueryDevtools } = await import('..') | ||
| expect(ReactQueryDevtools({})).toBeNull() | ||
| } finally { | ||
| vi.unstubAllEnvs() | ||
| vi.resetModules() | ||
| } | ||
| }) | ||
| }) | ||
78 changes: 78 additions & 0 deletions
78
packages/react-query-devtools/src/__tests__/ReactQueryDevtoolsPanel.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,78 @@ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { render } from '@testing-library/react' | ||
| import { QueryClient, QueryClientProvider } from '@tanstack/react-query' | ||
| import type { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools' | ||
|
|
||
| const mountMock = vi.fn() | ||
| const unmountMock = vi.fn() | ||
| const setClientMock = vi.fn() | ||
| const setOnCloseMock = vi.fn() | ||
| const setErrorTypesMock = vi.fn() | ||
| const setThemeMock = vi.fn() | ||
|
|
||
| vi.mock('@tanstack/query-devtools', () => ({ | ||
| TanstackQueryDevtoolsPanel: vi.fn(function ( | ||
| this: TanstackQueryDevtoolsPanel, | ||
| ) { | ||
| this.mount = mountMock | ||
| this.unmount = unmountMock | ||
| this.setClient = setClientMock | ||
| this.setOnClose = setOnCloseMock | ||
| this.setErrorTypes = setErrorTypesMock | ||
| this.setTheme = setThemeMock | ||
| }), | ||
| })) | ||
|
|
||
| describe('ReactQueryDevtoolsPanel', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| it('should throw an error if no query client has been set', async () => { | ||
| const { ReactQueryDevtoolsPanel } = | ||
| await import('../ReactQueryDevtoolsPanel') | ||
|
|
||
| expect(() => render(<ReactQueryDevtoolsPanel />)).toThrow( | ||
| 'No QueryClient set, use QueryClientProvider to set one', | ||
| ) | ||
| }) | ||
|
|
||
| it('should not throw an error if query client is provided via context', async () => { | ||
| const { ReactQueryDevtoolsPanel } = | ||
| await import('../ReactQueryDevtoolsPanel') | ||
| const queryClient = new QueryClient() | ||
|
|
||
| expect(() => | ||
| render( | ||
| <QueryClientProvider client={queryClient}> | ||
| <ReactQueryDevtoolsPanel /> | ||
| </QueryClientProvider>, | ||
| ), | ||
| ).not.toThrow() | ||
| expect(mountMock).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('should not throw an error if query client is provided via props', async () => { | ||
| const { ReactQueryDevtoolsPanel } = | ||
| await import('../ReactQueryDevtoolsPanel') | ||
| const queryClient = new QueryClient() | ||
|
|
||
| expect(() => | ||
| render(<ReactQueryDevtoolsPanel client={queryClient} />), | ||
| ).not.toThrow() | ||
| expect(mountMock).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('should return null in non-development environments', async () => { | ||
| vi.stubEnv('NODE_ENV', 'production') | ||
| vi.resetModules() | ||
|
|
||
| try { | ||
| const { ReactQueryDevtoolsPanel } = await import('..') | ||
| expect(ReactQueryDevtoolsPanel({})).toBeNull() | ||
| } finally { | ||
| vi.unstubAllEnvs() | ||
| vi.resetModules() | ||
| } | ||
| }) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| }) | ||
7 changes: 0 additions & 7 deletions
7
packages/react-query-devtools/src/__tests__/devtools.test.tsx
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
packages/react-query-devtools/src/__tests__/not-development.test.tsx
This file was deleted.
Oops, something went wrong.
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.