From 7b75f00d7c4a5d82a8f3618f7ed7b913ef6ada65 Mon Sep 17 00:00:00 2001 From: Adam Seckel Date: Thu, 4 Jun 2020 14:25:43 +0100 Subject: [PATCH 1/2] Update wrapper props with rerenderProps --- .all-contributorsrc | 9 +++++++++ README.md | 1 + src/pure.js | 2 +- test/useContext.test.js | 21 ++++++++++++++++++++- 4 files changed, 31 insertions(+), 2 deletions(-) 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/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') + }) }) From 5eaac2d3da4c559a8cb6998665ca8c904384792b Mon Sep 17 00:00:00 2001 From: Adam Seckel Date: Thu, 4 Jun 2020 14:37:04 +0100 Subject: [PATCH 2/2] Update docs --- docs/api-reference.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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