Skip to content

Commit 53c3b79

Browse files
committed
Add a test to verify subscription passthrough with store-as-prop
1 parent eb03a37 commit 53c3b79

File tree

2 files changed

+103
-5
lines changed

2 files changed

+103
-5
lines changed

test/components/Provider.spec.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import React, { Component } from 'react'
44
import ReactDOM from 'react-dom'
55
import { createStore } from 'redux'
6-
import { Provider, connect } from '../../src/index.js'
7-
import { ReactReduxContext } from '../../src/components/Context'
6+
import { Provider, connect, ReactReduxContext } from '../../src/index.js'
87
import * as rtl from 'react-testing-library'
98
import 'jest-dom/extend-expect'
109

@@ -221,7 +220,10 @@ describe('React', () => {
221220
)
222221
expect(innerMapStateToProps).toHaveBeenCalledTimes(1)
223222

224-
innerStore.dispatch({ type: 'INC' })
223+
rtl.act(() => {
224+
innerStore.dispatch({ type: 'INC' })
225+
})
226+
225227
expect(innerMapStateToProps).toHaveBeenCalledTimes(2)
226228
})
227229

test/components/connect.spec.js

+98-2
Original file line numberDiff line numberDiff line change
@@ -1638,6 +1638,104 @@ describe('React', () => {
16381638
expect(actualState).toEqual(expectedState)
16391639
})
16401640

1641+
it('should pass through ancestor subscription when store is given as a prop', () => {
1642+
const c3Spy = jest.fn()
1643+
const c2Spy = jest.fn()
1644+
const c1Spy = jest.fn()
1645+
1646+
const Comp3 = ({ first }) => {
1647+
c3Spy()
1648+
return <Passthrough c={first} />
1649+
}
1650+
const ConnectedComp3 = connect(state => state)(Comp3)
1651+
1652+
const Comp2 = ({ second }) => {
1653+
c2Spy()
1654+
return (
1655+
<div>
1656+
<Passthrough b={second} />
1657+
<ConnectedComp3 />
1658+
</div>
1659+
)
1660+
}
1661+
const ConnectedComp2 = connect(state => state)(Comp2)
1662+
1663+
const Comp1 = ({ children, first }) => {
1664+
c1Spy()
1665+
return (
1666+
<div>
1667+
<Passthrough a={first} />
1668+
{children}
1669+
</div>
1670+
)
1671+
}
1672+
const ConnectedComp1 = connect(state => state)(Comp1)
1673+
1674+
const reducer1 = (state = { first: '1' }, action) => {
1675+
switch (action.type) {
1676+
case 'CHANGE':
1677+
return { first: '2' }
1678+
default:
1679+
return state
1680+
}
1681+
}
1682+
1683+
const reducer2 = (state = { second: '3' }, action) => {
1684+
switch (action.type) {
1685+
case 'CHANGE':
1686+
return { second: '4' }
1687+
default:
1688+
return state
1689+
}
1690+
}
1691+
1692+
const store1 = createStore(reducer1)
1693+
const store2 = createStore(reducer2)
1694+
1695+
const tester = rtl.render(
1696+
<ProviderMock store={store1}>
1697+
<ConnectedComp1>
1698+
<ConnectedComp2 store={store2} />
1699+
</ConnectedComp1>
1700+
</ProviderMock>
1701+
)
1702+
1703+
// Initial render: C1/C3 read from store 1, C2 reads from store 2, one render each
1704+
expect(tester.getByTestId('a')).toHaveTextContent('1')
1705+
expect(tester.getByTestId('b')).toHaveTextContent('3')
1706+
expect(tester.getByTestId('c')).toHaveTextContent('1')
1707+
1708+
expect(c3Spy).toHaveBeenCalledTimes(1)
1709+
expect(c2Spy).toHaveBeenCalledTimes(1)
1710+
expect(c1Spy).toHaveBeenCalledTimes(1)
1711+
1712+
rtl.act(() => {
1713+
store1.dispatch({ type: 'CHANGE' })
1714+
})
1715+
1716+
// Store 1 update: C1 and C3 should re-render, no updates for C2
1717+
expect(tester.getByTestId('a')).toHaveTextContent('2')
1718+
expect(tester.getByTestId('b')).toHaveTextContent('3')
1719+
expect(tester.getByTestId('c')).toHaveTextContent('2')
1720+
1721+
expect(c3Spy).toHaveBeenCalledTimes(2)
1722+
expect(c2Spy).toHaveBeenCalledTimes(1)
1723+
expect(c1Spy).toHaveBeenCalledTimes(2)
1724+
1725+
rtl.act(() => {
1726+
store2.dispatch({ type: 'CHANGE' })
1727+
})
1728+
1729+
// Store 2 update: C2 should re-render, no updates for C1 or C3
1730+
expect(tester.getByTestId('a')).toHaveTextContent('2')
1731+
expect(tester.getByTestId('b')).toHaveTextContent('4')
1732+
expect(tester.getByTestId('c')).toHaveTextContent('2')
1733+
1734+
expect(c3Spy).toHaveBeenCalledTimes(2)
1735+
expect(c2Spy).toHaveBeenCalledTimes(2)
1736+
expect(c1Spy).toHaveBeenCalledTimes(2)
1737+
})
1738+
16411739
it('should use a custom context provider and consumer if passed as a prop to the component', () => {
16421740
class Container extends Component {
16431741
render() {
@@ -1796,8 +1894,6 @@ describe('React', () => {
17961894
done()
17971895
})
17981896

1799-
1800-
18011897
it('should correctly separate and pass through props to the wrapped component with a forwarded ref', async done => {
18021898
const store = createStore(() => ({}))
18031899

0 commit comments

Comments
 (0)