From 0dd66fa5b34e8da2f3070e1d8000ffdfb78f9fec Mon Sep 17 00:00:00 2001 From: Aaron Jensen Date: Mon, 9 Sep 2019 19:34:29 -0700 Subject: [PATCH] Re-enable storeKey Fixes #1393 --- src/components/connectAdvanced.js | 17 ++-------- test/components/connect.spec.js | 56 ++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 34 deletions(-) diff --git a/src/components/connectAdvanced.js b/src/components/connectAdvanced.js index e3e6f1da4..9ac4429bf 100644 --- a/src/components/connectAdvanced.js +++ b/src/components/connectAdvanced.js @@ -80,7 +80,6 @@ export default function connectAdvanced( // determines whether this HOC subscribes to store changes shouldHandleStateChanges = true, - // REMOVED: the key of props/context to get the store storeKey = 'store', // REMOVED: expose the wrapped component via refs @@ -106,18 +105,6 @@ export default function connectAdvanced( 'withRef is removed. To access the wrapped instance, use a ref on the connected component' ) - const customStoreWarningMessage = - 'To use a custom Redux store for specific components, create a custom React context with ' + - "React.createContext(), and pass the context object to React Redux's Provider and specific components" + - ' like: . ' + - 'You may also pass a {context : MyContext} option to connect' - - invariant( - storeKey === 'store', - 'storeKey has been removed and does not do anything. ' + - customStoreWarningMessage - ) - const Context = context return function wrapWithConnect(WrappedComponent) { @@ -182,7 +169,7 @@ export default function connectAdvanced( const contextValue = useContext(ContextToUse) // The store _must_ exist as either a prop or in context - const didStoreComeFromProps = Boolean(props.store) + const didStoreComeFromProps = Boolean(props[storeKey]) const didStoreComeFromContext = Boolean(contextValue) && Boolean(contextValue.store) @@ -194,7 +181,7 @@ export default function connectAdvanced( `React context consumer to ${displayName} in connect options.` ) - const store = props.store || contextValue.store + const store = props[storeKey] || contextValue.store const childPropsSelector = useMemo(() => { // The child props selector needs the store reference as an input. diff --git a/test/components/connect.spec.js b/test/components/connect.spec.js index 29d6da6ea..39084037a 100644 --- a/test/components/connect.spec.js +++ b/test/components/connect.spec.js @@ -99,6 +99,43 @@ describe('React', () => { expect(tester.getByTestId('hi')).toHaveTextContent('there') }) + it('should receive the store state in the store prop', () => { + const store = createStore(() => ({ hi: 'there' })) + + @connect(state => state) + class Container extends Component { + render() { + return + } + } + + const tester = rtl.render() + + expect(tester.getByTestId('hi')).toHaveTextContent('there') + }) + + it('should allow overriding the store prop with storeKey', () => { + const store = createStore(() => ({ hi: 'there' })) + + @connect( + state => state, + undefined, + undefined, + { storeKey: 'reduxStore' } + ) + class Container extends Component { + render() { + return + } + } + + const tester = rtl.render( + + ) + + expect(tester.getByTestId('hi')).toHaveTextContent('there') + }) + it('should pass state and props to the given component', () => { const store = createStore(() => ({ foo: 'bar', @@ -2872,25 +2909,6 @@ describe('React', () => { ).toThrow(/withRef is removed/) }) - it('should error on receiving a custom store key', () => { - const connectOptions = { storeKey: 'customStoreKey' } - - expect(() => { - @connect( - undefined, - undefined, - undefined, - connectOptions - ) - class Container extends Component { - render() { - return - } - } - new Container() - }).toThrow(/storeKey has been removed/) - }) - it.skip('should error on custom store', () => { function Comp() { return
hi