Skip to content

Commit 8266ad0

Browse files
committed
Updated README with testHook example
1 parent 3d9ff87 commit 8266ad0

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,28 +64,28 @@ const useTheme = (initialTheme) => {
6464
}
6565

6666
// useTheme.test.js
67-
import { useHook, cleanup } from 'react-hooks-testing-library'
67+
import { testHook, cleanup, act } from 'react-hooks-testing-library'
6868

6969
describe('custom hook tests', () => {
7070
afterEach(cleanup)
7171

7272
test('should use theme', () => {
73-
const { getCurrentValue } = useHook(() => useTheme('light'))
73+
const { result } = testHook(() => useTheme('light'))
7474

75-
const theme = getCurrentValue()
75+
const theme = result.current
7676

7777
expect(theme.primaryLight).toBe('#FFFFFF')
7878
expect(theme.primaryDark).toBe('#000000')
7979
})
8080

8181
test('should update theme', () => {
82-
const { getCurrentValue, act } = useHook(() => useTheme('light'))
82+
const { result } = testHook(() => useTheme('light'))
8383

84-
const { toggleTheme } = getCurrentValue()
84+
const { toggleTheme } = result.current
8585

8686
act(() => toggleTheme())
8787

88-
const theme = getCurrentValue()
88+
const theme = result.current
8989

9090
expect(theme.primaryLight).toBe('#000000')
9191
expect(theme.primaryDark).toBe('#FFFFFF')
@@ -97,11 +97,13 @@ describe('custom hook tests', () => {
9797
dark: { primaryLight: '#CCBBAA', primaryDark: '#AABBCC' }
9898
}
9999

100-
const { getCurrentValue, addContextProvider } = useHook(() => useTheme('light'))
100+
const wrapper = ({ children }) => (
101+
<ThemesContext.Provider value={customThemes}>{children}</ThemesContext.Provider>
102+
)
101103

102-
addContextProvider(ThemesContext, { value: customThemes })
104+
const { result } = testHook(() => useTheme('light'), { wrapper })
103105

104-
const theme = getCurrentValue()
106+
const theme = result.current
105107

106108
expect(theme.primaryLight).toBe('#AABBCC')
107109
expect(theme.primaryDark).toBe('#CCBBAA')

test/customHook.test.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,37 @@ describe('custom hook tests', () => {
1313
const useTheme = (initialTheme) => {
1414
const themes = useContext(ThemesContext)
1515
const [theme, setTheme] = useState(initialTheme)
16-
const changeTheme = () => {
16+
const toggleTheme = () => {
1717
setTheme(theme === 'light' ? 'dark' : 'light')
1818
}
19-
return useMemo(() => ({ ...themes[theme], changeTheme }), [theme])
19+
return useMemo(() => ({ ...themes[theme], toggleTheme }), [theme])
2020
}
2121

2222
afterEach(cleanup)
2323

24-
test('should get initial theme from custom hook', () => {
24+
test('should use theme', () => {
2525
const { result } = testHook(() => useTheme('light'))
2626

2727
const theme = result.current
2828

2929
expect(theme.primaryLight).toBe('#FFFFFF')
3030
expect(theme.primaryDark).toBe('#000000')
31-
expect(typeof theme.changeTheme).toBe('function')
3231
})
3332

34-
test('should update theme using custom hook', () => {
33+
test('should update theme', () => {
3534
const { result } = testHook(() => useTheme('light'))
3635

37-
const { changeTheme } = result.current
36+
const { toggleTheme } = result.current
3837

39-
act(() => changeTheme())
38+
act(() => toggleTheme())
4039

4140
const theme = result.current
4241

4342
expect(theme.primaryLight).toBe('#000000')
4443
expect(theme.primaryDark).toBe('#FFFFFF')
45-
expect(typeof theme.changeTheme).toBe('function')
4644
})
4745

48-
test('should get custom theme from custom hook', () => {
46+
test('should use custom theme', () => {
4947
const customThemes = {
5048
light: { primaryLight: '#AABBCC', primaryDark: '#CCBBAA' },
5149
dark: { primaryLight: '#CCBBAA', primaryDark: '#AABBCC' }
@@ -61,6 +59,5 @@ describe('custom hook tests', () => {
6159

6260
expect(theme.primaryLight).toBe('#AABBCC')
6361
expect(theme.primaryDark).toBe('#CCBBAA')
64-
expect(typeof theme.changeTheme).toBe('function')
6562
})
6663
})

0 commit comments

Comments
 (0)