-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathrenderHook.ts
60 lines (50 loc) · 1.58 KB
/
renderHook.ts
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
import { useState, createContext, useContext, useMemo } from 'react'
import { renderHook } from 'react-hooks-testing-library'
const DARK: 'dark' = 'dark'
const LIGHT: 'light' = 'light'
type InitialTheme = typeof DARK | typeof LIGHT | undefined
const themes = {
light: { primaryLight: '#FFFFFF', primaryDark: '#000000' },
dark: { primaryLight: '#000000', primaryDark: '#FFFFFF' }
}
const ThemesContext = createContext(themes)
const useTheme = (initialTheme: InitialTheme = DARK) => {
const themes = useContext(ThemesContext)
const [theme, setTheme] = useState(initialTheme)
const toggleTheme = () => {
setTheme(theme === 'light' ? 'dark' : 'light')
}
return useMemo(() => ({ ...themes[theme], toggleTheme }), [theme])
}
type InitialProps = { initialTheme: InitialTheme }
function checkTypesWithNoInitialProps() {
const { result, unmount, rerender } = renderHook(() => useTheme())
// check types
const _result: {
current: {
primaryDark: string
primaryLight: string
toggleTheme: () => void
}
} = result
const _unmount: () => boolean = unmount
const _rerender: () => void = rerender
}
function checkTypesWithInitialProps() {
const { result, unmount, rerender } = renderHook(
({ initialTheme }: InitialProps) => useTheme(initialTheme),
{
initialProps: { initialTheme: DARK }
}
)
// check types
const _result: {
current: {
primaryDark: string
primaryLight: string
toggleTheme: () => void
}
} = result
const _unmount: () => boolean = unmount
const _rerender: (_: InitialProps) => void = rerender
}