|
1 |
| -import { useState, createContext, useContext, useMemo } from 'react' |
| 1 | +import { useState, useCallback } from 'react' |
2 | 2 | import { renderHook } from 'react-hooks-testing-library'
|
3 | 3 |
|
4 |
| -const DARK: 'dark' = 'dark' |
5 |
| -const LIGHT: 'light' = 'light' |
6 |
| - |
7 |
| -type InitialTheme = typeof DARK | typeof LIGHT | undefined |
8 |
| - |
9 |
| -const themes = { |
10 |
| - light: { primaryLight: '#FFFFFF', primaryDark: '#000000' }, |
11 |
| - dark: { primaryLight: '#000000', primaryDark: '#FFFFFF' } |
12 |
| -} |
13 |
| - |
14 |
| -const ThemesContext = createContext(themes) |
15 |
| - |
16 |
| -const useTheme = (initialTheme: InitialTheme = DARK) => { |
17 |
| - const themes = useContext(ThemesContext) |
18 |
| - const [theme, setTheme] = useState(initialTheme) |
19 |
| - const toggleTheme = () => { |
20 |
| - setTheme(theme === 'light' ? 'dark' : 'light') |
| 4 | +const useCounter = (initialCount: number = 0) => { |
| 5 | + const [count, setCount] = useState(initialCount) |
| 6 | + const incrementBy = useCallback( |
| 7 | + (n: number) => { |
| 8 | + setCount(count + n) |
| 9 | + }, |
| 10 | + [count] |
| 11 | + ) |
| 12 | + const decrementBy = useCallback( |
| 13 | + (n: number) => { |
| 14 | + setCount(count - n) |
| 15 | + }, |
| 16 | + [count] |
| 17 | + ) |
| 18 | + return { |
| 19 | + count, |
| 20 | + incrementBy, |
| 21 | + decrementBy |
21 | 22 | }
|
22 |
| - return useMemo(() => ({ ...themes[theme], toggleTheme }), [theme]) |
23 | 23 | }
|
24 | 24 |
|
25 | 25 | function checkTypesWithNoInitialProps() {
|
26 |
| - const { result, unmount, rerender } = renderHook(() => useTheme()) |
| 26 | + const { result, unmount, rerender } = renderHook(() => useCounter()) |
27 | 27 |
|
28 | 28 | // check types
|
29 | 29 | const _result: {
|
30 | 30 | current: {
|
31 |
| - primaryDark: string |
32 |
| - primaryLight: string |
33 |
| - toggleTheme: () => void |
| 31 | + count: number |
| 32 | + incrementBy: (_: number) => void |
| 33 | + decrementBy: (_: number) => void |
34 | 34 | }
|
35 | 35 | } = result
|
36 | 36 | const _unmount: () => boolean = unmount
|
37 | 37 | const _rerender: () => void = rerender
|
38 | 38 | }
|
39 | 39 |
|
40 | 40 | function checkTypesWithInitialProps() {
|
41 |
| - const { result, unmount, rerender } = renderHook(({ theme }) => useTheme(theme), { |
42 |
| - initialProps: { theme: DARK } |
| 41 | + const { result, unmount, rerender } = renderHook(({ count }) => useCounter(count), { |
| 42 | + initialProps: { count: 10 } |
43 | 43 | })
|
44 | 44 |
|
45 | 45 | // check types
|
46 | 46 | const _result: {
|
47 | 47 | current: {
|
48 |
| - primaryDark: string |
49 |
| - primaryLight: string |
50 |
| - toggleTheme: () => void |
| 48 | + count: number |
| 49 | + incrementBy: (_: number) => void |
| 50 | + decrementBy: (_: number) => void |
51 | 51 | }
|
52 | 52 | } = result
|
53 | 53 | const _unmount: () => boolean = unmount
|
54 |
| - const _rerender: (_?: { theme: typeof DARK }) => void = rerender |
| 54 | + const _rerender: (_?: { count: number }) => void = rerender |
55 | 55 | }
|
0 commit comments