Skip to content

Add subkey option to connect #944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/components/connectAdvanced.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,17 @@ export default function connectAdvanced(
// the key of props/context to get the store
storeKey = 'store',

// the key of context to get the subscription
subKey,

// if true, the wrapped element is exposed by this HOC via the getWrappedInstance() function.
withRef = false,

// additional options are passed through to the selectorFactory
...connectOptions
} = {}
) {
const subscriptionKey = storeKey + 'Subscription'
const subscriptionKey = subKey || `${storeKey}Subscription`
const version = hotReloadingVersion++

const contextTypes = {
Expand Down
4 changes: 2 additions & 2 deletions src/connect/selectorFactory.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import verifySubselectors from './verifySubselectors'

export function impureFinalPropsSelectorFactory(
mapStateToProps,
mapDispatchToProps,
Expand Down Expand Up @@ -64,7 +64,7 @@ export function pureFinalPropsSelectorFactory(
const nextStateProps = mapStateToProps(state, ownProps)
const statePropsChanged = !areStatePropsEqual(nextStateProps, stateProps)
stateProps = nextStateProps

if (statePropsChanged)
mergedProps = mergeProps(stateProps, dispatchProps, ownProps)

Expand Down
51 changes: 35 additions & 16 deletions test/components/Provider.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import { Provider, createProvider, connect } from '../../src/index'

describe('React', () => {
describe('Provider', () => {
const createChild = (storeKey = 'store') => {
const createChild = (storeKey = 'store', subKey = 'subKey') => {
class Child extends Component {
render() {
return <div />
}
}

Child.contextTypes = {
[storeKey]: PropTypes.object.isRequired
[storeKey]: PropTypes.object.isRequired,
[subKey]: PropTypes.object,
}

return Child
Expand Down Expand Up @@ -73,21 +74,39 @@ describe('React', () => {
})

it('should add the store to the child context using a custom store key', () => {
const store = createStore(() => ({}))
const CustomProvider = createProvider('customStoreKey');
const CustomChild = createChild('customStoreKey');

const spy = jest.spyOn(console, 'error').mockImplementation(() => {});
const testRenderer = TestRenderer.create(
<CustomProvider store={store}>
<CustomChild />
</CustomProvider>
)
spy.mockRestore()
expect(spy).toHaveBeenCalledTimes(0)
const store = createStore(() => ({}))
const CustomProvider = createProvider('customStoreKey');
const CustomChild = createChild('customStoreKey');

const spy = jest.spyOn(console, 'error').mockImplementation(() => {});
const testRenderer = TestRenderer.create(
<CustomProvider store={store}>
<CustomChild />
</CustomProvider>
)
spy.mockRestore()
expect(spy).toHaveBeenCalledTimes(0)

const child = testRenderer.root.findByType(CustomChild).instance
expect(child.context.customStoreKey).toBe(store)
})

it('should add the subscription to the child context using a custom subscription key', () => {
const store = createStore(() => ({}))
const CustomProvider = createProvider('customStoreKey', 'customSubKey')
const CustomChild = createChild('customStoreKey', 'customSubKey')

const spy = jest.spyOn(console, 'error').mockImplementation(() => {})
const testRenderer = TestRenderer.create(
<CustomProvider store={store}>
<CustomChild />
</CustomProvider>
)
spy.mockRestore()
expect(spy).toHaveBeenCalledTimes(0)

const child = testRenderer.root.findByType(CustomChild).instance
expect(child.context.customStoreKey).toBe(store)
const child = testRenderer.root.findByType(CustomChild).instance
expect(child.context.customSubKey).toBe(null)
})

it('should warn once when receiving a new store in props', () => {
Expand Down
59 changes: 58 additions & 1 deletion test/components/connect.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import PropTypes from 'prop-types'
import ReactDOM from 'react-dom'
import TestRenderer from 'react-test-renderer'
import { createStore } from 'redux'
import { connect } from '../../src/index'
import { connect, createProvider } from '../../src/index'

describe('React', () => {
describe('connect', () => {
Expand Down Expand Up @@ -2324,5 +2324,62 @@ describe('React', () => {
expect(mapStateToPropsC).toHaveBeenCalledTimes(2)
expect(mapStateToPropsD).toHaveBeenCalledTimes(2)
})

it('should receive the store in the context using a custom store key', () => {
const store = createStore(() => ({}))
const CustomProvider = createProvider('customStoreKey')
const connectOptions = { storeKey: 'customStoreKey' }

@connect(undefined, undefined, undefined, connectOptions)
class Container extends Component {
render() {
return <Passthrough {...this.props} />
}
}

const testRenderer = TestRenderer.create(
<CustomProvider store={store}>
<Container />
</CustomProvider>
)

const container = testRenderer.root.findByType(Container)
expect(container.instance.store).toBe(store)
})

it('should receive the subscription in the context using a custom store key', () => {
const store = createStore(() => ({}))
const CustomProvider = createProvider('customStoreKey', 'customSubKey')
const connectOptions = { storeKey: 'customStoreKey', subKey: 'customSubKey' }

@connect(() => ({}), undefined, undefined, connectOptions)
class OuterContainer extends Component {
render() {
return Children.only(this.props.children)
}
}

@connect(() => ({}), undefined, undefined, connectOptions)
class NestedContainer extends Component {
render() {
return <Passthrough {...this.props} />
}
}

const testRenderer = TestRenderer.create(
<CustomProvider store={store}>
<OuterContainer>
<NestedContainer />
</OuterContainer>
</CustomProvider>
)

const outerContainer = testRenderer.root.findByType(OuterContainer)
const nestedContainer = testRenderer.root.findByType(NestedContainer)
expect(outerContainer.instance.context.customSubKey).toBe(null)
expect(nestedContainer.instance.context.customSubKey).toBe(
outerContainer.instance.subscription
)
})
})
})