@@ -5,22 +5,28 @@ import PropTypes from 'prop-types'
5
5
import semver from 'semver'
6
6
import { createStore } from 'redux'
7
7
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'
9
10
10
11
describe ( 'React' , ( ) => {
11
12
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
+ )
17
22
}
23
+ }
18
24
19
- Child . contextTypes = {
20
- [ storeKey ] : PropTypes . object . isRequired
21
- }
25
+ Child . contextTypes = {
26
+ [ storeKey ] : PropTypes . object . isRequired
27
+ }
22
28
23
- return Child
29
+ return Child
24
30
}
25
31
const Child = createChild ( ) ;
26
32
@@ -34,33 +40,33 @@ describe('React', () => {
34
40
const spy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
35
41
36
42
try {
37
- expect ( ( ) => enzyme . mount (
43
+ expect ( ( ) => rtl . render (
38
44
< Provider store = { store } >
39
45
< div />
40
46
</ Provider >
41
47
) ) . not . toThrow ( )
42
48
43
49
if ( semver . lt ( React . version , '15.0.0' ) ) {
44
- expect ( ( ) => enzyme . mount (
50
+ expect ( ( ) => rtl . render (
45
51
< Provider store = { store } >
46
52
</ Provider >
47
53
) ) . toThrow ( / c h i l d r e n w i t h e x a c t l y o n e c h i l d / )
48
54
} else {
49
- expect ( ( ) => enzyme . mount (
55
+ expect ( ( ) => rtl . render (
50
56
< Provider store = { store } >
51
57
</ Provider >
52
58
) ) . toThrow ( / a s i n g l e R e a c t e l e m e n t c h i l d / )
53
59
}
54
60
55
61
if ( semver . lt ( React . version , '15.0.0' ) ) {
56
- expect ( ( ) => enzyme . mount (
62
+ expect ( ( ) => rtl . render (
57
63
< Provider store = { store } >
58
64
< div />
59
65
< div />
60
66
</ Provider >
61
67
) ) . toThrow ( / c h i l d r e n w i t h e x a c t l y o n e c h i l d / )
62
68
} else {
63
- expect ( ( ) => enzyme . mount (
69
+ expect ( ( ) => rtl . render (
64
70
< Provider store = { store } >
65
71
< div />
66
72
< div />
@@ -75,47 +81,52 @@ describe('React', () => {
75
81
76
82
it ( 'should add the store to the child context' , ( ) => {
77
83
const store = createStore ( ( ) => ( { } ) )
84
+ store . mine = 'hi'
78
85
79
86
const spy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
80
- const testRenderer = enzyme . mount (
87
+ const tester = rtl . render (
81
88
< Provider store = { store } >
82
89
< Child />
83
90
</ Provider >
84
91
)
85
92
expect ( spy ) . toHaveBeenCalledTimes ( 0 )
86
93
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' )
90
96
} )
91
97
92
98
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 ( )
105
112
106
- const child = testRenderer . find ( CustomChild ) . instance ( )
107
- expect ( child . context . customStoreKey ) . toBe ( store )
113
+ expect ( tester . getByTestId ( 'store' ) ) . toHaveTextContent ( 'customStoreKey - hi' )
108
114
} )
109
115
110
116
it ( 'should warn once when receiving a new store in props' , ( ) => {
111
117
const store1 = createStore ( ( state = 10 ) => state + 1 )
118
+ store1 . mine = '1'
112
119
const store2 = createStore ( ( state = 10 ) => state * 2 )
120
+ store2 . mine = '2'
113
121
const store3 = createStore ( ( state = 10 ) => state * state )
122
+ store3 . mine = '3'
114
123
124
+ let externalSetState
115
125
class ProviderContainer extends Component {
116
126
constructor ( ) {
117
127
super ( )
118
128
this . state = { store : store1 }
129
+ externalSetState = this . setState . bind ( this )
119
130
}
120
131
render ( ) {
121
132
return (
@@ -126,14 +137,13 @@ describe('React', () => {
126
137
}
127
138
}
128
139
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' )
132
142
133
143
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' )
137
147
expect ( spy ) . toHaveBeenCalledTimes ( 1 )
138
148
expect ( spy . mock . calls [ 0 ] [ 0 ] ) . toBe (
139
149
'<Provider> does not support changing `store` on the fly. ' +
@@ -145,9 +155,9 @@ describe('React', () => {
145
155
spy . mockRestore ( )
146
156
147
157
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' )
151
161
expect ( spy ) . toHaveBeenCalledTimes ( 0 )
152
162
spy . mockRestore ( )
153
163
} )
@@ -168,7 +178,7 @@ describe('React', () => {
168
178
render ( ) { return < Provider store = { innerStore } > < Inner /> </ Provider > }
169
179
}
170
180
171
- enzyme . mount ( < Provider store = { outerStore } > < Outer /> </ Provider > )
181
+ rtl . render ( < Provider store = { outerStore } > < Outer /> </ Provider > )
172
182
expect ( innerMapStateToProps ) . toHaveBeenCalledTimes ( 1 )
173
183
174
184
innerStore . dispatch ( { type : 'INC' } )
@@ -216,7 +226,7 @@ describe('React', () => {
216
226
}
217
227
}
218
228
219
- const testRenderer = enzyme . mount (
229
+ const tester = rtl . render (
220
230
< Provider store = { store } >
221
231
< Container />
222
232
</ Provider >
@@ -229,8 +239,8 @@ describe('React', () => {
229
239
expect ( childMapStateInvokes ) . toBe ( 2 )
230
240
231
241
// 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 )
234
244
expect ( childMapStateInvokes ) . toBe ( 3 )
235
245
236
246
// Provider uses unstable_batchedUpdates() under the hood
@@ -245,7 +255,7 @@ describe('React', () => {
245
255
const spy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
246
256
const store = createStore ( ( ) => ( { } ) )
247
257
248
- TestRenderer . create (
258
+ rtl . render (
249
259
< React . StrictMode >
250
260
< Provider store = { store } >
251
261
< div />
0 commit comments