Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade the typings dependency and update the docs #414

Merged
merged 5 commits into from Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions docs/usage/advanced-hooks.md
Expand Up @@ -54,6 +54,48 @@ test('should use custom step when incrementing', () => {
The `wrapper` option will accept any React component, but it **must** render `children` in order for
the test component to render and the hook to execute.

### Providing Props

Sometimes we need to test a hook with different context values. By using the `initialProps` option
and the new props of `rerender` method, we can easily do this:

```js
import { renderHook, act } from '@testing-library/react-hooks'
import { CounterStepProvider, useCounter } from './counter'

test('should use custom step when incrementing', () => {
const wrapper = ({ children, step }) => (
<CounterStepProvider step={step}>{children}</CounterStepProvider>
)
const { result, rerender } = renderHook(() => useCounter(), {
wrapper,
initialProps: {
step: 2
}
})

act(() => {
result.current.increment()
})

expect(result.current.count).toBe(2)

/**
* Change the step value
*/
rerender({ step: 8 })

act(() => {
result.current.increment()
})

expect(result.current.count).toBe(10)
})
```

Note the `initialProps` and the new props of `rerender` are also accessed by the callback function
of the `renderHook` which the wrapper is provided to.

### ESLint Warning

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