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

feature/use rerender props in wrapper #381

Merged
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
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@
"contributions": [
"doc"
]
},
{
"login": "hemlok",
"name": "Adam Seckel",
"avatar_url": "https://avatars2.githubusercontent.com/u/9043345?v=4",
"profile": "https://github.com/hemlok",
"contributions": [
"code"
]
}
],
"commitConvention": "none"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<tr>
<td align="center"><a href="http://frontstuff.io"><img src="https://avatars1.githubusercontent.com/u/5370675?v=4" width="100px;" alt=""/><br /><sub><b>Sarah Dayan</b></sub></a><br /><a href="#platform-sarahdayan" title="Packaging/porting to new platform">📦</a></td>
<td align="center"><a href="https://github.com/102"><img src="https://avatars1.githubusercontent.com/u/5839225?v=4" width="100px;" alt=""/><br /><sub><b>Roman Gusev</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=102" title="Documentation">📖</a></td>
<td align="center"><a href="https://github.com/hemlok"><img src="https://avatars2.githubusercontent.com/u/9043345?v=4" width="100px;" alt=""/><br /><sub><b>Adam Seckel</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=hemlok" title="Code">💻</a></td>
</tr>
</table>

Expand Down
5 changes: 3 additions & 2 deletions docs/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ The `renderHook` function accepts the following options as the second parameter:

### `initialProps`

The initial values to pass as `props` to the `callback` function of `renderHook.
The initial values to pass as `props` to the `callback` function of `renderHook`.

### `wrapper`

A React component to wrap the test component in when rendering. This is usually used to add context
providers from `React.createContext` for the hook to access with `useContext`.
providers from `React.createContext` for the hook to access with `useContext`. `initialProps` and
props subsequently set by `rerender` will be provided to the wrapper.

## `renderHook` Result

Expand Down
2 changes: 1 addition & 1 deletion src/pure.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function renderHook(callback, { initialProps, wrapper } = {}) {
const hookProps = { current: initialProps }

const wrapUiIfNeeded = (innerElement) =>
wrapper ? React.createElement(wrapper, null, innerElement) : innerElement
wrapper ? React.createElement(wrapper, hookProps.current, innerElement) : innerElement

const toRender = () =>
wrapUiIfNeeded(
Expand Down
21 changes: 20 additions & 1 deletion test/useContext.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('useContext tests', () => {
expect(result.current).toBe('bar')
})

test('should update value in context', () => {
test('should update mutated value in context', () => {
const TestContext = createContext('foo')

const value = { current: 'bar' }
Expand All @@ -41,4 +41,23 @@ describe('useContext tests', () => {

expect(result.current).toBe('baz')
})

test('should update value in context when props are updated', () => {
const TestContext = createContext('foo')

const wrapper = ({ current, children }) => (
<TestContext.Provider value={current}>{children}</TestContext.Provider>
)

const { result, rerender } = renderHook(() => useContext(TestContext), {
wrapper,
initialProps: {
current: 'bar'
}
})

rerender({ current: 'baz' })

expect(result.current).toBe('baz')
})
})