Skip to content

Commit 4a8305a

Browse files
committed
Temporarily revert use of Enzyme so I can test context
1 parent d8a86ca commit 4a8305a

File tree

2 files changed

+196
-234
lines changed

2 files changed

+196
-234
lines changed

test/components/Provider.spec.js

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
import React, { Component } from 'react'
44
import PropTypes from 'prop-types'
5-
import semver from 'semver'
5+
import TestRenderer from 'react-test-renderer'
66
import { createStore } from 'redux'
7-
import { Provider, createProvider, connect } from '../../src/index.js'
8-
import { TestRenderer, enzyme } from '../getTestDeps.js'
7+
import { Provider, createProvider, connect } from '../../src/index'
98

109
describe('React', () => {
1110
describe('Provider', () => {
@@ -34,39 +33,23 @@ describe('React', () => {
3433
const spy = jest.spyOn(console, 'error').mockImplementation(() => {})
3534

3635
try {
37-
expect(() => enzyme.mount(
36+
expect(() => TestRenderer.create(
3837
<Provider store={store}>
3938
<div />
4039
</Provider>
4140
)).not.toThrow()
4241

43-
if (semver.lt(React.version, '15.0.0')) {
44-
expect(() => enzyme.mount(
45-
<Provider store={store}>
46-
</Provider>
47-
)).toThrow(/children with exactly one child/)
48-
} else {
49-
expect(() => enzyme.mount(
50-
<Provider store={store}>
51-
</Provider>
52-
)).toThrow(/a single React element child/)
53-
}
42+
expect(() => TestRenderer.create(
43+
<Provider store={store}>
44+
</Provider>
45+
)).toThrow(/a single React element child/)
5446

55-
if (semver.lt(React.version, '15.0.0')) {
56-
expect(() => enzyme.mount(
57-
<Provider store={store}>
58-
<div />
59-
<div />
60-
</Provider>
61-
)).toThrow(/children with exactly one child/)
62-
} else {
63-
expect(() => enzyme.mount(
64-
<Provider store={store}>
65-
<div />
66-
<div />
67-
</Provider>
68-
)).toThrow(/a single React element child/)
69-
}
47+
expect(() => TestRenderer.create(
48+
<Provider store={store}>
49+
<div />
50+
<div />
51+
</Provider>
52+
)).toThrow(/a single React element child/)
7053
} finally {
7154
Provider.propTypes = propTypes
7255
spy.mockRestore()
@@ -77,15 +60,15 @@ describe('React', () => {
7760
const store = createStore(() => ({}))
7861

7962
const spy = jest.spyOn(console, 'error').mockImplementation(() => {})
80-
const testRenderer = enzyme.mount(
63+
const testRenderer = TestRenderer.create(
8164
<Provider store={store}>
8265
<Child />
8366
</Provider>
8467
)
8568
expect(spy).toHaveBeenCalledTimes(0)
8669
spy.mockRestore()
8770

88-
const child = testRenderer.find(Child).instance()
71+
const child = testRenderer.root.findByType(Child).instance
8972
expect(child.context.store).toBe(store)
9073
})
9174

@@ -95,15 +78,15 @@ describe('React', () => {
9578
const CustomChild = createChild('customStoreKey');
9679

9780
const spy = jest.spyOn(console, 'error').mockImplementation(() => {});
98-
const testRenderer = enzyme.mount(
81+
const testRenderer = TestRenderer.create(
9982
<CustomProvider store={store}>
10083
<CustomChild />
10184
</CustomProvider>
10285
)
10386
expect(spy).toHaveBeenCalledTimes(0)
10487
spy.mockRestore()
10588

106-
const child = testRenderer.find(CustomChild).instance()
89+
const child = testRenderer.root.findByType(CustomChild).instance
10790
expect(child.context.customStoreKey).toBe(store)
10891
})
10992

@@ -126,12 +109,12 @@ describe('React', () => {
126109
}
127110
}
128111

129-
const testRenderer = enzyme.mount(<ProviderContainer />)
130-
const child = testRenderer.find(Child).instance()
112+
const testRenderer = TestRenderer.create(<ProviderContainer />)
113+
const child = testRenderer.root.findByType(Child).instance
131114
expect(child.context.store.getState()).toEqual(11)
132115

133116
let spy = jest.spyOn(console, 'error').mockImplementation(() => {})
134-
testRenderer.setState({ store: store2 })
117+
testRenderer.root.instance.setState({ store: store2 })
135118

136119
expect(child.context.store.getState()).toEqual(11)
137120
expect(spy).toHaveBeenCalledTimes(1)
@@ -145,7 +128,7 @@ describe('React', () => {
145128
spy.mockRestore()
146129

147130
spy = jest.spyOn(console, 'error').mockImplementation(() => {})
148-
testRenderer.setState({ store: store3 })
131+
testRenderer.root.instance.setState({ store: store3 })
149132

150133
expect(child.context.store.getState()).toEqual(11)
151134
expect(spy).toHaveBeenCalledTimes(0)
@@ -168,7 +151,7 @@ describe('React', () => {
168151
render() { return <Provider store={innerStore}><Inner /></Provider> }
169152
}
170153

171-
enzyme.mount(<Provider store={outerStore}><Outer /></Provider>)
154+
TestRenderer.create(<Provider store={outerStore}><Outer /></Provider>)
172155
expect(innerMapStateToProps).toHaveBeenCalledTimes(1)
173156

174157
innerStore.dispatch({ type: 'INC'})
@@ -216,7 +199,7 @@ describe('React', () => {
216199
}
217200
}
218201

219-
const testRenderer = enzyme.mount(
202+
const testRenderer = TestRenderer.create(
220203
<Provider store={store}>
221204
<Container />
222205
</Provider>
@@ -229,19 +212,16 @@ describe('React', () => {
229212
expect(childMapStateInvokes).toBe(2)
230213

231214
// setState calls DOM handlers are batched
232-
const button = testRenderer.find('button')
233-
button.prop('onClick')()
215+
const button = testRenderer.root.findByType('button')
216+
button.props.onClick()
234217
expect(childMapStateInvokes).toBe(3)
235218

236219
// Provider uses unstable_batchedUpdates() under the hood
237220
store.dispatch({ type: 'APPEND', body: 'd' })
238221
expect(childMapStateInvokes).toBe(4)
239222
})
240223

241-
it('works in <StrictMode> without warnings (React 16.3+)', () => {
242-
if (!React.StrictMode) {
243-
return
244-
}
224+
it('works in <StrictMode> without warnings', () => {
245225
const spy = jest.spyOn(console, 'error').mockImplementation(() => {})
246226
const store = createStore(() => ({}))
247227

0 commit comments

Comments
 (0)