Skip to content

Commit e32c312

Browse files
committed
expose act from update to be wrapped in
1 parent 450309c commit e32c312

File tree

5 files changed

+14
-13
lines changed

5 files changed

+14
-13
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ const ThemesContext = createContext(themes)
6161
const useTheme = (initialTheme) => {
6262
const themes = useContext(ThemesContext)
6363
const [theme, setTheme] = useState(initialTheme)
64-
const changeTheme = () => {
64+
const toggleTheme = () => {
6565
setTheme(theme === 'light' ? 'dark' : 'light')
6666
}
67-
return useMemo(() => ({ ...themes[theme], changeTheme }), [theme])
67+
return useMemo(() => ({ ...themes[theme], toggleTheme }), [theme])
6868
}
6969

7070
// useTheme.test.js
@@ -83,11 +83,11 @@ describe('custom hook tests', () => {
8383
})
8484

8585
test('should update theme', () => {
86-
const { getCurrentValue } = useHook(() => useTheme('light'))
86+
const { getCurrentValue, act } = useHook(() => useTheme('light'))
8787

88-
const { changeTheme } = getCurrentValue()
88+
const { toggleTheme } = getCurrentValue()
8989

90-
changeTheme()
90+
act(() => toggleTheme())
9191

9292
const theme = getCurrentValue()
9393

@@ -122,7 +122,7 @@ npm install react-hooks-testing-library
122122
## Usage
123123

124124
```js
125-
// TODO: write this
125+
// TODO: write this - see Example above for now
126126
```
127127

128128
## Contributors

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ export const useHook = (hook, ...props) => {
6363
getCurrentValues: getCurrentValue,
6464
flushEffects,
6565
setProps,
66-
addContextProvider
66+
addContextProvider,
67+
act
6768
}
6869
}
6970

test/customHook.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ describe('custom hook tests', () => {
3131
})
3232

3333
test('should update theme using custom hook', () => {
34-
const { getCurrentValue } = useHook(() => useTheme('light'))
34+
const { getCurrentValue, act } = useHook(() => useTheme('light'))
3535

3636
const { changeTheme } = getCurrentValue()
3737

38-
changeTheme()
38+
act(() => changeTheme())
3939

4040
const theme = getCurrentValue()
4141

test/setState.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ describe('useState tests', () => {
1313
})
1414

1515
test('should update setState value using setter', () => {
16-
const { getCurrentValues } = useHook(() => useState('foo'))
16+
const { getCurrentValues, act } = useHook(() => useState('foo'))
1717

1818
const [_, setValue] = getCurrentValues()
1919

20-
setValue('bar')
20+
act(() => setValue('bar'))
2121

2222
const [value] = getCurrentValues()
2323

test/useReducer.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ describe('useReducer tests', () => {
66

77
test('should handle useReducer hook', () => {
88
const reducer = (state, action) => (action.type === 'inc' ? state + 1 : state)
9-
const { getCurrentValues } = useHook(() => useReducer(reducer, 0))
9+
const { getCurrentValues, act } = useHook(() => useReducer(reducer, 0))
1010

1111
const [initialState, dispatch] = getCurrentValues()
1212

1313
expect(initialState).toBe(0)
1414

15-
dispatch({ type: 'inc' })
15+
act(() => dispatch({ type: 'inc' }))
1616

1717
const [state] = getCurrentValues()
1818

0 commit comments

Comments
 (0)