Skip to content

Commit ac01d70

Browse files
jimbollatimdorr
authored andcommitted
Fixes #582 (Nested Provider subscriptions) (#584)
1 parent aa228ce commit ac01d70

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/components/Provider.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Component, PropTypes, Children } from 'react'
2+
import Subscription from '../utils/Subscription'
23
import storeShape from '../utils/storeShape'
34
import warning from '../utils/warning'
45

@@ -20,7 +21,7 @@ function warnAboutReceivingStore() {
2021

2122
export default class Provider extends Component {
2223
getChildContext() {
23-
return { store: this.store }
24+
return { store: this.store, storeSubscription: null }
2425
}
2526

2627
constructor(props, context) {
@@ -49,6 +50,7 @@ Provider.propTypes = {
4950
children: PropTypes.element.isRequired
5051
}
5152
Provider.childContextTypes = {
52-
store: storeShape.isRequired
53+
store: storeShape.isRequired,
54+
storeSubscription: PropTypes.instanceOf(Subscription)
5355
}
5456
Provider.displayName = 'Provider'

test/components/Provider.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,29 @@ describe('React', () => {
109109
expect(child.context.store.getState()).toEqual(11)
110110
expect(spy.calls.length).toBe(0)
111111
})
112+
113+
it('should handle subscriptions correctly when there is nested Providers', () => {
114+
const reducer = (state = 0, action) => (action.type === 'INC' ? state + 1 : state)
115+
116+
const innerStore = createStore(reducer)
117+
const innerMapStateToProps = expect.createSpy().andCall(state => ({ count: state }))
118+
@connect(innerMapStateToProps)
119+
class Inner extends Component {
120+
render() { return <div>{this.props.count}</div> }
121+
}
122+
123+
const outerStore = createStore(reducer)
124+
@connect(state => ({ count: state }))
125+
class Outer extends Component {
126+
render() { return <Provider store={innerStore}><Inner /></Provider> }
127+
}
128+
129+
TestUtils.renderIntoDocument(<Provider store={outerStore}><Outer /></Provider>)
130+
expect(innerMapStateToProps.calls.length).toBe(1)
131+
132+
innerStore.dispatch({ type: 'INC'})
133+
expect(innerMapStateToProps.calls.length).toBe(2)
134+
})
112135
})
113136

114137
it('should pass state consistently to mapState', () => {

0 commit comments

Comments
 (0)