Skip to content

Commit

Permalink
Merge pull request #381 from hemlok/pr/use-rerender-props-in-wrapper
Browse files Browse the repository at this point in the history
feature/use rerender props in wrapper
  • Loading branch information
mpeyper committed Jun 4, 2020
2 parents e77aab8 + 5eaac2d commit 7c98baf
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
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')
})
})

0 comments on commit 7c98baf

Please sign in to comment.