-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathThemeContext.test.tsx
More file actions
65 lines (55 loc) · 1.7 KB
/
ThemeContext.test.tsx
File metadata and controls
65 lines (55 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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<ReturnType<typeof useTheme>>
function renderThemeProbe(provider?: () => Theme) {
let resolvedTheme: Theme | undefined
function ThemeProbe() {
const theme = useTheme()
resolvedTheme = theme()
return null
}
render(() =>
provider ? (
<ThemeContext.Provider value={provider}>
<ThemeProbe />
</ThemeContext.Provider>
) : (
<ThemeProbe />
),
)
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<Theme>('dark')
let observed: Theme | undefined
function ThemeProbe() {
const resolved = useTheme()
createEffect(() => {
observed = resolved()
})
return null
}
render(() => (
<ThemeContext.Provider value={theme}>
<ThemeProbe />
</ThemeContext.Provider>
))
expect(observed).toBe('dark')
setTheme('light')
expect(observed).toBe('light')
})
})
})