Skip to content

Commit b9d7742

Browse files
committed
Switch to react-test-render
test-utils have side-effects between tests.
1 parent fc3e837 commit b9d7742

File tree

4 files changed

+141
-113
lines changed

4 files changed

+141
-113
lines changed

package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
"jest": "^22.4.3",
9595
"react": "^16.3.0",
9696
"react-dom": "^16.3.0",
97+
"react-test-renderer": "^16.3.0",
9798
"redux": "^3.0.0",
9899
"rimraf": "^2.6.2",
99100
"rollup": "^0.56.5",

test/components/Provider.spec.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import React, { Component } from 'react'
44
import PropTypes from 'prop-types'
5-
import TestUtils from 'react-dom/test-utils'
5+
import TestRenderer from 'react-test-renderer'
66
import { createStore } from 'redux'
77
import { Provider, createProvider, connect } from '../../src/index'
88

@@ -33,18 +33,18 @@ describe('React', () => {
3333
const spy = jest.spyOn(console, 'error').mockImplementation(() => {})
3434

3535
try {
36-
expect(() => TestUtils.renderIntoDocument(
36+
expect(() => TestRenderer.create(
3737
<Provider store={store}>
3838
<div />
3939
</Provider>
4040
)).not.toThrow()
4141

42-
expect(() => TestUtils.renderIntoDocument(
42+
expect(() => TestRenderer.create(
4343
<Provider store={store}>
4444
</Provider>
4545
)).toThrow(/a single React element child/)
4646

47-
expect(() => TestUtils.renderIntoDocument(
47+
expect(() => TestRenderer.create(
4848
<Provider store={store}>
4949
<div />
5050
<div />
@@ -60,15 +60,15 @@ describe('React', () => {
6060
const store = createStore(() => ({}))
6161

6262
const spy = jest.spyOn(console, 'error').mockImplementation(() => {})
63-
const tree = TestUtils.renderIntoDocument(
63+
const testRenderer = TestRenderer.create(
6464
<Provider store={store}>
6565
<Child />
6666
</Provider>
6767
)
6868
spy.mockRestore()
6969
expect(spy).toHaveBeenCalledTimes(0)
7070

71-
const child = TestUtils.findRenderedComponentWithType(tree, Child)
71+
const child = testRenderer.root.findByType(Child).instance
7272
expect(child.context.store).toBe(store)
7373
})
7474

@@ -78,15 +78,15 @@ describe('React', () => {
7878
const CustomChild = createChild('customStoreKey');
7979

8080
const spy = jest.spyOn(console, 'error').mockImplementation(() => {});
81-
const tree = TestUtils.renderIntoDocument(
81+
const testRenderer = TestRenderer.create(
8282
<CustomProvider store={store}>
8383
<CustomChild />
8484
</CustomProvider>
8585
)
8686
spy.mockRestore()
8787
expect(spy).toHaveBeenCalledTimes(0)
8888

89-
const child = TestUtils.findRenderedComponentWithType(tree, CustomChild)
89+
const child = testRenderer.root.findByType(CustomChild).instance
9090
expect(child.context.customStoreKey).toBe(store)
9191
})
9292

@@ -109,12 +109,12 @@ describe('React', () => {
109109
}
110110
}
111111

112-
const container = TestUtils.renderIntoDocument(<ProviderContainer />)
113-
const child = TestUtils.findRenderedComponentWithType(container, Child)
112+
const testRenderer = TestRenderer.create(<ProviderContainer />)
113+
const child = testRenderer.root.findByType(Child).instance
114114
expect(child.context.store.getState()).toEqual(11)
115115

116116
let spy = jest.spyOn(console, 'error').mockImplementation(() => {})
117-
container.setState({ store: store2 })
117+
testRenderer.root.instance.setState({ store: store2 })
118118
spy.mockRestore()
119119

120120
expect(child.context.store.getState()).toEqual(11)
@@ -128,7 +128,7 @@ describe('React', () => {
128128
)
129129

130130
spy = jest.spyOn(console, 'error').mockImplementation(() => {})
131-
container.setState({ store: store3 })
131+
testRenderer.root.instance.setState({ store: store3 })
132132
spy.mockRestore()
133133

134134
expect(child.context.store.getState()).toEqual(11)
@@ -151,7 +151,7 @@ describe('React', () => {
151151
render() { return <Provider store={innerStore}><Inner /></Provider> }
152152
}
153153

154-
TestUtils.renderIntoDocument(<Provider store={outerStore}><Outer /></Provider>)
154+
TestRenderer.create(<Provider store={outerStore}><Outer /></Provider>)
155155
expect(innerMapStateToProps).toHaveBeenCalledTimes(1)
156156

157157
innerStore.dispatch({ type: 'INC'})
@@ -199,7 +199,7 @@ describe('React', () => {
199199
}
200200
}
201201

202-
const tree = TestUtils.renderIntoDocument(
202+
const testRenderer = TestRenderer.create(
203203
<Provider store={store}>
204204
<Container />
205205
</Provider>
@@ -212,9 +212,8 @@ describe('React', () => {
212212
expect(childMapStateInvokes).toBe(2)
213213

214214
// setState calls DOM handlers are batched
215-
const container = TestUtils.findRenderedComponentWithType(tree, Container)
216-
const node = container.getWrappedInstance().refs.button
217-
TestUtils.Simulate.click(node)
215+
const button = testRenderer.root.findByType('button')
216+
button.props.onClick()
218217
expect(childMapStateInvokes).toBe(3)
219218

220219
// Provider uses unstable_batchedUpdates() under the hood

0 commit comments

Comments
 (0)