From fa3f9ab702fc95c0eac34247c5d31c80183e6d15 Mon Sep 17 00:00:00 2001 From: Wonsuk Choi Date: Mon, 25 May 2026 15:07:54 +0900 Subject: [PATCH] test(query-devtools/contexts/ThemeContext): split 'ThemeContext' tests into a unit test file mirroring the 'src/contexts' structure --- .../src/__tests__/Devtools.test.tsx | 44 +------------ .../__tests__/contexts/ThemeContext.test.tsx | 65 +++++++++++++++++++ 2 files changed, 66 insertions(+), 43 deletions(-) create mode 100644 packages/query-devtools/src/__tests__/contexts/ThemeContext.test.tsx diff --git a/packages/query-devtools/src/__tests__/Devtools.test.tsx b/packages/query-devtools/src/__tests__/Devtools.test.tsx index 7aa12fe052..ddaa730ec6 100644 --- a/packages/query-devtools/src/__tests__/Devtools.test.tsx +++ b/packages/query-devtools/src/__tests__/Devtools.test.tsx @@ -3,12 +3,7 @@ import { QueryClient, QueryObserver, onlineManager } from '@tanstack/query-core' import { fireEvent, render } from '@solidjs/testing-library' import { createLocalStorage } from '@solid-primitives/storage' import { Devtools } from '../Devtools' -import { - PiPProvider, - QueryDevtoolsContext, - ThemeContext, - useTheme, -} from '../contexts' +import { PiPProvider, QueryDevtoolsContext, ThemeContext } from '../contexts' import type { QueryDevtoolsProps } from '../contexts' // `solid-transition-group` internally imports from @@ -1521,41 +1516,4 @@ describe('Devtools', () => { ).not.toBeInTheDocument() }) }) - - describe('default theme', () => { - it('should fall back to the "dark" theme when no "ThemeContext.Provider" wraps it', () => { - let resolvedTheme: ReturnType> | undefined - function ThemeProbe() { - const theme = useTheme() - resolvedTheme = theme() - return null - } - - const rendered = render(() => { - const [localStore, setLocalStore] = createLocalStorage({ - prefix: 'TanstackQueryDevtools', - }) - return ( - - - - - - - ) - }) - - expect(resolvedTheme).toBe('dark') - expect( - rendered.getByLabelText('Open Tanstack query devtools'), - ).toBeInTheDocument() - }) - }) }) diff --git a/packages/query-devtools/src/__tests__/contexts/ThemeContext.test.tsx b/packages/query-devtools/src/__tests__/contexts/ThemeContext.test.tsx new file mode 100644 index 0000000000..5d0cb2c727 --- /dev/null +++ b/packages/query-devtools/src/__tests__/contexts/ThemeContext.test.tsx @@ -0,0 +1,65 @@ +import { describe, expect, it } from 'vitest' +import { render } from '@solidjs/testing-library' +import { createEffect, createSignal } from 'solid-js' +import { ThemeContext, useTheme } from '../../contexts' + +type Theme = ReturnType> + +function renderThemeProbe(provider?: () => Theme) { + let resolvedTheme: Theme | undefined + function ThemeProbe() { + const theme = useTheme() + resolvedTheme = theme() + return null + } + + render(() => + provider ? ( + + + + ) : ( + + ), + ) + + return resolvedTheme +} + +describe('ThemeContext', () => { + describe('default value', () => { + it('should resolve to "dark" when no "ThemeContext.Provider" wraps the consumer', () => { + expect(renderThemeProbe()).toBe('dark') + }) + }) + + describe('with a "ThemeContext.Provider"', () => { + it('should resolve to the value provided by the "Provider"', () => { + expect(renderThemeProbe(() => 'light')).toBe('light') + expect(renderThemeProbe(() => 'dark')).toBe('dark') + }) + + it('should reflect updates when the "Provider" value is a reactive "Accessor"', () => { + const [theme, setTheme] = createSignal('dark') + let observed: Theme | undefined + function ThemeProbe() { + const resolved = useTheme() + createEffect(() => { + observed = resolved() + }) + return null + } + + render(() => ( + + + + )) + + expect(observed).toBe('dark') + + setTheme('light') + expect(observed).toBe('light') + }) + }) +})