From 7626018a4b030a4357218edd7b578dcbbbecf027 Mon Sep 17 00:00:00 2001 From: Jonathan Ziller Date: Sat, 25 May 2019 11:51:07 +0200 Subject: [PATCH] add test to verify that docs suggestion of putting a connected component above the hooks component allows dealing with stale props --- test/hooks/useSelector.spec.js | 44 +++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/test/hooks/useSelector.spec.js b/test/hooks/useSelector.spec.js index 79b8327c4..fdaffdba4 100644 --- a/test/hooks/useSelector.spec.js +++ b/test/hooks/useSelector.spec.js @@ -7,7 +7,8 @@ import * as rtl from 'react-testing-library' import { Provider as ProviderMock, useSelector, - shallowEqual + shallowEqual, + connect } from '../../src/index.js' import { useReduxContext } from '../../src/hooks/useReduxContext' @@ -333,6 +334,47 @@ describe('React', () => { spy.mockRestore() }) + + it('allows dealing with stale props by putting a specific connected component above the hooks component', () => { + const spy = jest.spyOn(console, 'error').mockImplementation(() => {}) + + const Parent = () => { + const count = useSelector(s => s.count) + return + } + + const ConnectedWrapper = connect(({ count }) => ({ count }))( + ({ parentCount }) => { + return + } + ) + + let sawInconsistentState = false + + const Child = ({ parentCount }) => { + const result = useSelector(({ count }) => { + if (count !== parentCount) { + sawInconsistentState = true + } + + return count + parentCount + }) + + return
{result}
+ } + + rtl.render( + + + + ) + + store.dispatch({ type: '' }) + + expect(sawInconsistentState).toBe(false) + + spy.mockRestore() + }) }) describe('error handling for invalid arguments', () => {