@@ -59,72 +59,56 @@ Using this library, you do not have to concern yourself with how to construct, r
5959## Example
6060
6161``` js
62- // useTheme .js
63- import { useState , createContext , useContext , useMemo } from ' react'
62+ // useCounter .js
63+ import { useState } from ' react'
6464
65- const themes = {
66- light: { primaryLight: ' #FFFFFF' , primaryDark: ' #000000' },
67- dark: { primaryLight: ' #000000' , primaryDark: ' #FFFFFF' }
68- }
65+ function useCounter (initialCount = 0 ) {
66+ const [count , setCount ] = useState (initialCount)
6967
70- const ThemesContext = createContext (themes)
68+ const incrementBy = useCallback ((n ) => setCount (count + n), [count])
69+ const decrementBy = useCallback ((n ) => setCount (count - n), [count])
7170
72- const useTheme = (initialTheme ) => {
73- const themes = useContext (ThemesContext)
74- const [theme , setTheme ] = useState (initialTheme)
75- const toggleTheme = () => {
76- setTheme (theme === ' light' ? ' dark' : ' light' )
77- }
78- return useMemo (() => ({ ... themes[theme], toggleTheme }), [theme])
71+ return { count, incrementBy, decrementBy }
7972}
73+
74+ export default useCounter
8075```
8176
8277``` js
83- // useTheme .test.js
78+ // useCounter .test.js
8479import { renderHook , cleanup , act } from ' react-hooks-testing-library'
80+ import useCounter from ' ./useCounter'
8581
86- describe (' custom hook tests' , () => {
87- afterEach (cleanup)
88-
89- test (' should use theme' , () => {
90- const { result } = renderHook (() => useTheme (' light' ))
82+ afterEach (cleanup)
9183
92- const theme = result .current
84+ test (' should create counter' , () => {
85+ const { result } = renderHook (() => useCounter ())
9386
94- expect (theme .primaryLight ).toBe (' #FFFFFF' )
95- expect (theme .primaryDark ).toBe (' #000000' )
96- })
87+ expect (result .current .count ).toBe (0 )
88+ })
9789
98- test (' should update theme ' , () => {
99- const { result } = renderHook (() => useTheme ( ' light ' ))
90+ test (' should increment counter ' , () => {
91+ const { result } = renderHook (() => useCounter ( ))
10092
101- const { toggleTheme } = result .current
93+ act (() => result .current . incrementBy ( 1 ))
10294
103- act (() => toggleTheme () )
95+ expect ( result . current . count ). toBe ( 1 )
10496
105- const theme = result .current
97+ act (() => result .current . incrementBy ( 2 ))
10698
107- expect (theme .primaryLight ).toBe (' #000000' )
108- expect (theme .primaryDark ).toBe (' #FFFFFF' )
109- })
99+ expect (result .current .count ).toBe (3 )
100+ })
110101
111- test (' should use custom theme' , () => {
112- const customThemes = {
113- light: { primaryLight: ' #AABBCC' , primaryDark: ' #CCBBAA' },
114- dark: { primaryLight: ' #CCBBAA' , primaryDark: ' #AABBCC' }
115- }
102+ test (' should decrement counter' , () => {
103+ const { result } = renderHook (() => useCounter ())
116104
117- const wrapper = ({ children }) => (
118- < ThemesContext .Provider value= {customThemes}> {children}< / ThemesContext .Provider >
119- )
105+ act (() => result .current .decrementBy (1 ))
120106
121- const { result } = renderHook (() => useTheme ( ' light ' ), { wrapper } )
107+ expect ( result . current . count ). toBe ( - 1 )
122108
123- const theme = result .current
109+ act (() => result .current . decrementBy ( 2 ))
124110
125- expect (theme .primaryLight ).toBe (' #AABBCC' )
126- expect (theme .primaryDark ).toBe (' #CCBBAA' )
127- })
111+ expect (result .current .count ).toBe (- 3 )
128112})
129113```
130114
0 commit comments