Skip to content

Commit 514c132

Browse files
authored
test(query-devtools/contexts/ThemeContext): split 'ThemeContext' tests into a unit test file mirroring the 'src/contexts' structure (TanStack#10787)
1 parent 0cfde2a commit 514c132

2 files changed

Lines changed: 66 additions & 43 deletions

File tree

packages/query-devtools/src/__tests__/Devtools.test.tsx

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@ import { QueryClient, QueryObserver, onlineManager } from '@tanstack/query-core'
33
import { fireEvent, render } from '@solidjs/testing-library'
44
import { createLocalStorage } from '@solid-primitives/storage'
55
import { Devtools } from '../Devtools'
6-
import {
7-
PiPProvider,
8-
QueryDevtoolsContext,
9-
ThemeContext,
10-
useTheme,
11-
} from '../contexts'
6+
import { PiPProvider, QueryDevtoolsContext, ThemeContext } from '../contexts'
127
import type { QueryDevtoolsProps } from '../contexts'
138

149
// `solid-transition-group` internally imports from
@@ -1521,41 +1516,4 @@ describe('Devtools', () => {
15211516
).not.toBeInTheDocument()
15221517
})
15231518
})
1524-
1525-
describe('default theme', () => {
1526-
it('should fall back to the "dark" theme when no "ThemeContext.Provider" wraps it', () => {
1527-
let resolvedTheme: ReturnType<ReturnType<typeof useTheme>> | undefined
1528-
function ThemeProbe() {
1529-
const theme = useTheme()
1530-
resolvedTheme = theme()
1531-
return null
1532-
}
1533-
1534-
const rendered = render(() => {
1535-
const [localStore, setLocalStore] = createLocalStorage({
1536-
prefix: 'TanstackQueryDevtools',
1537-
})
1538-
return (
1539-
<QueryDevtoolsContext.Provider
1540-
value={{
1541-
client: queryClient,
1542-
queryFlavor: 'TanStack Query',
1543-
version: '5',
1544-
onlineManager,
1545-
}}
1546-
>
1547-
<PiPProvider localStore={localStore} setLocalStore={setLocalStore}>
1548-
<Devtools localStore={localStore} setLocalStore={setLocalStore} />
1549-
<ThemeProbe />
1550-
</PiPProvider>
1551-
</QueryDevtoolsContext.Provider>
1552-
)
1553-
})
1554-
1555-
expect(resolvedTheme).toBe('dark')
1556-
expect(
1557-
rendered.getByLabelText('Open Tanstack query devtools'),
1558-
).toBeInTheDocument()
1559-
})
1560-
})
15611519
})
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { render } from '@solidjs/testing-library'
3+
import { createEffect, createSignal } from 'solid-js'
4+
import { ThemeContext, useTheme } from '../../contexts'
5+
6+
type Theme = ReturnType<ReturnType<typeof useTheme>>
7+
8+
function renderThemeProbe(provider?: () => Theme) {
9+
let resolvedTheme: Theme | undefined
10+
function ThemeProbe() {
11+
const theme = useTheme()
12+
resolvedTheme = theme()
13+
return null
14+
}
15+
16+
render(() =>
17+
provider ? (
18+
<ThemeContext.Provider value={provider}>
19+
<ThemeProbe />
20+
</ThemeContext.Provider>
21+
) : (
22+
<ThemeProbe />
23+
),
24+
)
25+
26+
return resolvedTheme
27+
}
28+
29+
describe('ThemeContext', () => {
30+
describe('default value', () => {
31+
it('should resolve to "dark" when no "ThemeContext.Provider" wraps the consumer', () => {
32+
expect(renderThemeProbe()).toBe('dark')
33+
})
34+
})
35+
36+
describe('with a "ThemeContext.Provider"', () => {
37+
it('should resolve to the value provided by the "Provider"', () => {
38+
expect(renderThemeProbe(() => 'light')).toBe('light')
39+
expect(renderThemeProbe(() => 'dark')).toBe('dark')
40+
})
41+
42+
it('should reflect updates when the "Provider" value is a reactive "Accessor"', () => {
43+
const [theme, setTheme] = createSignal<Theme>('dark')
44+
let observed: Theme | undefined
45+
function ThemeProbe() {
46+
const resolved = useTheme()
47+
createEffect(() => {
48+
observed = resolved()
49+
})
50+
return null
51+
}
52+
53+
render(() => (
54+
<ThemeContext.Provider value={theme}>
55+
<ThemeProbe />
56+
</ThemeContext.Provider>
57+
))
58+
59+
expect(observed).toBe('dark')
60+
61+
setTheme('light')
62+
expect(observed).toBe('light')
63+
})
64+
})
65+
})

0 commit comments

Comments
 (0)