Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e88a323

Browse files
committed
Update the docs
- Add new section "Providing props" under "Advanced Hooks/Context"
1 parent 18008d5 commit e88a323

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

‎docs/usage/advanced-hooks.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,48 @@ test('should use custom step when incrementing', () => {
5454
The `wrapper` option will accept any React component, but it **must** render `children` in order for
5555
the test component to render and the hook to execute.
5656

57+
### Providing Props
58+
59+
Sometimes we need to test a hook with different context values. By using the `initialProps` option
60+
and the new props of `rerender` method, we can easily do this:
61+
62+
```js
63+
import { renderHook, act } from '@testing-library/react-hooks'
64+
import { CounterStepProvider, useCounter } from './counter'
65+
66+
test('should use custom step when incrementing', () => {
67+
const wrapper = ({ children, step }) => (
68+
<CounterStepProvider step={step}>{children}</CounterStepProvider>
69+
)
70+
const { result, rerender } = renderHook(() => useCounter(), {
71+
wrapper,
72+
initialProps: {
73+
step: 2
74+
}
75+
})
76+
77+
act(() => {
78+
result.current.increment()
79+
})
80+
81+
expect(result.current.count).toBe(2)
82+
83+
/**
84+
* Change the step value
85+
*/
86+
rerender({ step: 8 })
87+
88+
act(() => {
89+
result.current.increment()
90+
})
91+
92+
expect(result.current.count).toBe(10)
93+
})
94+
```
95+
96+
Note the `initialProps` and the new props of `rerender` are also accessed by the callback function
97+
of the `renderHook` which the wrapper is provided to.
98+
5799
### ESLint Warning
58100

59101
It can be very tempting to try to inline the `wrapper` variable into the `renderHook` line, and

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /