diff --git a/.all-contributorsrc b/.all-contributorsrc index 06c7cb58..87ec42fd 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -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" diff --git a/README.md b/README.md index c7df1f10..646e286d 100644 --- a/README.md +++ b/README.md @@ -161,6 +161,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
Sarah Dayan

📦
Roman Gusev

📖 +
Adam Seckel

💻 diff --git a/docs/api-reference.md b/docs/api-reference.md index 72abd096..ab91f42f 100644 --- a/docs/api-reference.md +++ b/docs/api-reference.md @@ -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 diff --git a/src/pure.js b/src/pure.js index 36c21153..3b4a475d 100644 --- a/src/pure.js +++ b/src/pure.js @@ -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( diff --git a/test/useContext.test.js b/test/useContext.test.js index e8e16a82..2c22caca 100644 --- a/test/useContext.test.js +++ b/test/useContext.test.js @@ -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' } @@ -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 }) => ( + {children} + ) + + const { result, rerender } = renderHook(() => useContext(TestContext), { + wrapper, + initialProps: { + current: 'bar' + } + }) + + rerender({ current: 'baz' }) + + expect(result.current).toBe('baz') + }) })