Skip to content

Commit a47b64c

Browse files
committed
convert to rtl
1 parent 5333b40 commit a47b64c

File tree

1 file changed

+57
-47
lines changed

1 file changed

+57
-47
lines changed

test/components/Provider.spec.js

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,28 @@ import PropTypes from 'prop-types'
55
import semver from 'semver'
66
import { createStore } from 'redux'
77
import { Provider, createProvider, connect } from '../../src/index.js'
8-
import { TestRenderer, enzyme } from '../getTestDeps.js'
8+
import * as rtl from 'react-testing-library'
9+
import 'jest-dom/extend-expect'
910

1011
describe('React', () => {
1112
describe('Provider', () => {
12-
const createChild = (storeKey = 'store') => {
13-
class Child extends Component {
14-
render() {
15-
return <div />
16-
}
13+
afterEach(() => rtl.cleanup())
14+
const createChild = (storeKey = 'store') => {
15+
class Child extends Component {
16+
render() {
17+
return (
18+
<div data-testid="store">
19+
{storeKey} - {this.context[storeKey] && this.context[storeKey].mine ? this.context[storeKey].mine : ''}
20+
</div>
21+
)
1722
}
23+
}
1824

19-
Child.contextTypes = {
20-
[storeKey]: PropTypes.object.isRequired
21-
}
25+
Child.contextTypes = {
26+
[storeKey]: PropTypes.object.isRequired
27+
}
2228

23-
return Child
29+
return Child
2430
}
2531
const Child = createChild();
2632

@@ -34,33 +40,33 @@ describe('React', () => {
3440
const spy = jest.spyOn(console, 'error').mockImplementation(() => {})
3541

3642
try {
37-
expect(() => enzyme.mount(
43+
expect(() => rtl.render(
3844
<Provider store={store}>
3945
<div />
4046
</Provider>
4147
)).not.toThrow()
4248

4349
if (semver.lt(React.version, '15.0.0')) {
44-
expect(() => enzyme.mount(
50+
expect(() => rtl.render(
4551
<Provider store={store}>
4652
</Provider>
4753
)).toThrow(/children with exactly one child/)
4854
} else {
49-
expect(() => enzyme.mount(
55+
expect(() => rtl.render(
5056
<Provider store={store}>
5157
</Provider>
5258
)).toThrow(/a single React element child/)
5359
}
5460

5561
if (semver.lt(React.version, '15.0.0')) {
56-
expect(() => enzyme.mount(
62+
expect(() => rtl.render(
5763
<Provider store={store}>
5864
<div />
5965
<div />
6066
</Provider>
6167
)).toThrow(/children with exactly one child/)
6268
} else {
63-
expect(() => enzyme.mount(
69+
expect(() => rtl.render(
6470
<Provider store={store}>
6571
<div />
6672
<div />
@@ -75,47 +81,52 @@ describe('React', () => {
7581

7682
it('should add the store to the child context', () => {
7783
const store = createStore(() => ({}))
84+
store.mine = 'hi'
7885

7986
const spy = jest.spyOn(console, 'error').mockImplementation(() => {})
80-
const testRenderer = enzyme.mount(
87+
const tester = rtl.render(
8188
<Provider store={store}>
8289
<Child />
8390
</Provider>
8491
)
8592
expect(spy).toHaveBeenCalledTimes(0)
8693
spy.mockRestore()
87-
88-
const child = testRenderer.find(Child).instance()
89-
expect(child.context.store).toBe(store)
94+
95+
expect(tester.getByTestId('store')).toHaveTextContent('store - hi')
9096
})
9197

9298
it('should add the store to the child context using a custom store key', () => {
93-
const store = createStore(() => ({}))
94-
const CustomProvider = createProvider('customStoreKey');
95-
const CustomChild = createChild('customStoreKey');
96-
97-
const spy = jest.spyOn(console, 'error').mockImplementation(() => {});
98-
const testRenderer = enzyme.mount(
99-
<CustomProvider store={store}>
100-
<CustomChild />
101-
</CustomProvider>
102-
)
103-
expect(spy).toHaveBeenCalledTimes(0)
104-
spy.mockRestore()
99+
const store = createStore(() => ({}))
100+
store.mine = 'hi'
101+
const CustomProvider = createProvider('customStoreKey');
102+
const CustomChild = createChild('customStoreKey');
103+
104+
const spy = jest.spyOn(console, 'error').mockImplementation(() => {});
105+
const tester = rtl.render(
106+
<CustomProvider store={store}>
107+
<CustomChild />
108+
</CustomProvider>
109+
)
110+
expect(spy).toHaveBeenCalledTimes(0)
111+
spy.mockRestore()
105112

106-
const child = testRenderer.find(CustomChild).instance()
107-
expect(child.context.customStoreKey).toBe(store)
113+
expect(tester.getByTestId('store')).toHaveTextContent('customStoreKey - hi')
108114
})
109115

110116
it('should warn once when receiving a new store in props', () => {
111117
const store1 = createStore((state = 10) => state + 1)
118+
store1.mine = '1'
112119
const store2 = createStore((state = 10) => state * 2)
120+
store2.mine = '2'
113121
const store3 = createStore((state = 10) => state * state)
122+
store3.mine = '3'
114123

124+
let externalSetState
115125
class ProviderContainer extends Component {
116126
constructor() {
117127
super()
118128
this.state = { store: store1 }
129+
externalSetState = this.setState.bind(this)
119130
}
120131
render() {
121132
return (
@@ -126,14 +137,13 @@ describe('React', () => {
126137
}
127138
}
128139

129-
const testRenderer = enzyme.mount(<ProviderContainer />)
130-
const child = testRenderer.find(Child).instance()
131-
expect(child.context.store.getState()).toEqual(11)
140+
const tester = rtl.render(<ProviderContainer />)
141+
expect(tester.getByTestId('store')).toHaveTextContent('store - 1')
132142

133143
let spy = jest.spyOn(console, 'error').mockImplementation(() => {})
134-
testRenderer.setState({ store: store2 })
135-
136-
expect(child.context.store.getState()).toEqual(11)
144+
externalSetState({ store: store2 })
145+
146+
expect(tester.getByTestId('store')).toHaveTextContent('store - 1')
137147
expect(spy).toHaveBeenCalledTimes(1)
138148
expect(spy.mock.calls[0][0]).toBe(
139149
'<Provider> does not support changing `store` on the fly. ' +
@@ -145,9 +155,9 @@ describe('React', () => {
145155
spy.mockRestore()
146156

147157
spy = jest.spyOn(console, 'error').mockImplementation(() => {})
148-
testRenderer.setState({ store: store3 })
149-
150-
expect(child.context.store.getState()).toEqual(11)
158+
externalSetState({ store: store3 })
159+
160+
expect(tester.getByTestId('store')).toHaveTextContent('store - 1')
151161
expect(spy).toHaveBeenCalledTimes(0)
152162
spy.mockRestore()
153163
})
@@ -168,7 +178,7 @@ describe('React', () => {
168178
render() { return <Provider store={innerStore}><Inner /></Provider> }
169179
}
170180

171-
enzyme.mount(<Provider store={outerStore}><Outer /></Provider>)
181+
rtl.render(<Provider store={outerStore}><Outer /></Provider>)
172182
expect(innerMapStateToProps).toHaveBeenCalledTimes(1)
173183

174184
innerStore.dispatch({ type: 'INC'})
@@ -216,7 +226,7 @@ describe('React', () => {
216226
}
217227
}
218228

219-
const testRenderer = enzyme.mount(
229+
const tester = rtl.render(
220230
<Provider store={store}>
221231
<Container />
222232
</Provider>
@@ -229,8 +239,8 @@ describe('React', () => {
229239
expect(childMapStateInvokes).toBe(2)
230240

231241
// setState calls DOM handlers are batched
232-
const button = testRenderer.find('button')
233-
button.prop('onClick')()
242+
const button = tester.getByText('change')
243+
rtl.fireEvent.click(button)
234244
expect(childMapStateInvokes).toBe(3)
235245

236246
// Provider uses unstable_batchedUpdates() under the hood
@@ -245,7 +255,7 @@ describe('React', () => {
245255
const spy = jest.spyOn(console, 'error').mockImplementation(() => {})
246256
const store = createStore(() => ({}))
247257

248-
TestRenderer.create(
258+
rtl.render(
249259
<React.StrictMode>
250260
<Provider store={store}>
251261
<div />

0 commit comments

Comments
 (0)