Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

ThemeContext.test.tsx

48 lines1.3 KB

1import { describe, expect, it } from 'vitest'
2import { render } from '@solidjs/testing-library'
3import { createEffect, createSignal } from 'solid-js'
4import { ThemeContext, useTheme } from '../../contexts'
5​
6type Theme = ReturnType<ReturnType<typeof useTheme>>
7​
8describe('ThemeContext', () => {
9 describe('default value', () => {
10 it('should resolve to "dark" when no "ThemeContext.Provider" wraps the consumer', () => {
11 let resolvedTheme: Theme | undefined
12 function ThemeProbe() {
13 const theme = useTheme()
14 resolvedTheme = theme()
15 return null
16 }
17​
18 render(() => <ThemeProbe />)
19​
20 expect(resolvedTheme).toBe('dark')
21 })
22 })
23​
24 describe('with a "ThemeContext.Provider"', () => {
25 it('should reflect updates when the "Provider" value is a reactive "Accessor"', () => {
26 const [theme, setTheme] = createSignal<Theme>('dark')
27 const observed: Array<Theme> = []
28 function ThemeProbe() {
29 const resolved = useTheme()
30 createEffect(() => {
31 observed.push(resolved())
32 })
33 return null
34 }
35​
36 render(() => (
37 <ThemeContext.Provider value={theme}>
38 <ThemeProbe />
39 </ThemeContext.Provider>
40 ))
41​
42 expect(observed).toStrictEqual(['dark'])
43​
44 setTheme('light')
45 expect(observed).toStrictEqual(['dark', 'light'])
46 })
47 })
48})