1
- import { PageStateManager , Dispatcher } from '@ima/core' ;
2
- import { PageContext } from '@ima/react-page-renderer' ;
3
- import { shallow , mount } from 'enzyme' ;
4
- import { PureComponent , createElement , createRef } from 'react' ;
5
- import { toMockedInstance , setGlobalMockMethod } from 'to-mock' ;
1
+ import { getContextValue , renderWithContext } from '@ima/testing-library' ;
2
+ import { PureComponent , createRef } from 'react' ;
6
3
7
4
import forwardedSelect , {
8
5
createStateSelector ,
@@ -12,8 +9,6 @@ import forwardedSelect, {
12
9
hoistNonReactStatic ,
13
10
} from '../select' ;
14
11
15
- setGlobalMockMethod ( jest . fn ) ;
16
-
17
12
describe ( 'plugin-select:' , ( ) => {
18
13
const appState = {
19
14
media : {
@@ -22,16 +17,7 @@ describe('plugin-select:', () => {
22
17
} ,
23
18
title : 'title' ,
24
19
} ;
25
- const componentContext = {
26
- $Utils : {
27
- $PageStateManager : toMockedInstance ( PageStateManager , {
28
- getState : ( ) => {
29
- return appState ;
30
- } ,
31
- } ) ,
32
- $Dispatcher : toMockedInstance ( Dispatcher ) ,
33
- } ,
34
- } ;
20
+ let componentContext ;
35
21
const selectorMethods = [
36
22
state => {
37
23
return {
@@ -64,14 +50,14 @@ describe('plugin-select:', () => {
64
50
settings : Object . assign ( { } , props . settings , { newSettingsProp : true } ) ,
65
51
} ) ;
66
52
67
- beforeEach ( ( ) => {
68
- global . $Debug = true ;
53
+ beforeEach ( async ( ) => {
54
+ componentContext = await getContextValue ( ) ;
69
55
70
- setCreatorOfStateSelector ( createStateSelector ) ;
71
- } ) ;
56
+ jest
57
+ . spyOn ( componentContext . $Utils . $PageStateManager , 'getState' )
58
+ . mockReturnValue ( appState ) ;
72
59
73
- afterEach ( ( ) => {
74
- delete global . $Debug ;
60
+ setCreatorOfStateSelector ( createStateSelector ) ;
75
61
} ) ;
76
62
77
63
describe ( 'createStateSelector' , ( ) => {
@@ -127,7 +113,6 @@ describe('plugin-select:', () => {
127
113
} ) ;
128
114
129
115
describe ( 'select' , ( ) => {
130
- let wrapper = null ;
131
116
const defaultProps = {
132
117
props : 'props' ,
133
118
multiplier : 0.5 ,
@@ -146,88 +131,96 @@ describe('plugin-select:', () => {
146
131
}
147
132
148
133
render ( ) {
149
- return < h1 > text </ h1 > ;
134
+ return < div > { JSON . stringify ( this . props , null , 2 ) } </ div > ;
150
135
}
151
136
}
152
137
153
- const MockContextProvider = ( { children } ) => (
154
- < PageContext . Provider value = { componentContext } >
155
- { children }
156
- </ PageContext . Provider >
157
- ) ;
158
-
159
- it ( 'should render component' , ( ) => {
160
- wrapper = shallow ( createElement ( Component , defaultProps ) , {
161
- context : componentContext ,
162
- } ) ;
138
+ it ( 'should render component' , async ( ) => {
139
+ const { container } = await renderWithContext (
140
+ < Component { ...defaultProps } /> ,
141
+ {
142
+ contextValue : componentContext ,
143
+ }
144
+ ) ;
163
145
164
- expect ( wrapper ) . toMatchSnapshot ( ) ;
146
+ expect ( container . firstChild ) . toMatchSnapshot ( ) ;
165
147
} ) ;
166
148
167
- it ( 'should render component with extraProps' , ( ) => {
149
+ it ( 'should render component with extraProps' , async ( ) => {
168
150
let EnhancedComponent = select ( ...selectorMethods ) ( Component ) ;
169
151
170
- wrapper = mount ( createElement ( EnhancedComponent , defaultProps ) , {
171
- context : componentContext ,
172
- wrappingComponent : MockContextProvider ,
173
- } ) ;
152
+ const { container } = await renderWithContext (
153
+ < EnhancedComponent { ...defaultProps } /> ,
154
+ {
155
+ contextValue : componentContext ,
156
+ }
157
+ ) ;
174
158
175
- expect ( wrapper ) . toMatchSnapshot ( ) ;
159
+ expect ( container . firstChild ) . toMatchSnapshot ( ) ;
176
160
} ) ;
177
161
178
- it ( 'should render component with extraProps modifies by ownProps' , ( ) => {
162
+ it ( 'should render component with extraProps modifies by ownProps' , async ( ) => {
179
163
let EnhancedComponent = select (
180
164
...selectorMethods ,
181
165
selectorUsingProps
182
166
) ( Component ) ;
183
167
184
- wrapper = mount ( createElement ( EnhancedComponent , defaultProps ) , {
185
- context : componentContext ,
186
- wrappingComponent : MockContextProvider ,
187
- } ) ;
168
+ const { container } = await renderWithContext (
169
+ < EnhancedComponent { ...defaultProps } /> ,
170
+ {
171
+ contextValue : componentContext ,
172
+ }
173
+ ) ;
188
174
189
- expect ( wrapper ) . toMatchSnapshot ( ) ;
175
+ expect ( container . firstChild ) . toMatchSnapshot ( ) ;
190
176
} ) ;
191
177
192
- it ( 'should render component with extraProps replaced by ownProps' , ( ) => {
178
+ it ( 'should render component with extraProps replaced by ownProps' , async ( ) => {
193
179
let EnhancedComponent = select (
194
180
...selectorMethods ,
195
181
selectorReplaceProps
196
182
) ( Component ) ;
197
183
198
- wrapper = mount ( createElement ( EnhancedComponent , defaultProps ) , {
199
- context : componentContext ,
200
- wrappingComponent : MockContextProvider ,
201
- } ) ;
184
+ const { container } = await renderWithContext (
185
+ < EnhancedComponent { ...defaultProps } /> ,
186
+ {
187
+ contextValue : componentContext ,
188
+ }
189
+ ) ;
202
190
203
- expect ( wrapper ) . toMatchSnapshot ( ) ;
191
+ expect ( container . firstChild ) . toMatchSnapshot ( ) ;
204
192
} ) ;
205
193
206
- it ( 'should add listener to dispatcher after mounting to DOM' , ( ) => {
194
+ it ( 'should add listener to dispatcher after mounting to DOM' , async ( ) => {
207
195
let EnhancedComponent = select ( ...selectorMethods ) ( Component ) ;
208
196
209
- wrapper = mount ( createElement ( EnhancedComponent , defaultProps ) , {
210
- context : componentContext ,
211
- wrappingComponent : MockContextProvider ,
197
+ jest . spyOn ( componentContext . $Utils . $Dispatcher , 'listen' ) ;
198
+
199
+ await renderWithContext ( < EnhancedComponent { ...defaultProps } /> , {
200
+ contextValue : componentContext ,
212
201
} ) ;
213
202
214
203
expect ( componentContext . $Utils . $Dispatcher . listen ) . toHaveBeenCalled ( ) ;
215
204
} ) ;
216
205
217
- it ( 'should remove listener to dispatcher before unmounting from DOM' , ( ) => {
206
+ it ( 'should remove listener to dispatcher before unmounting from DOM' , async ( ) => {
218
207
let EnhancedComponent = select ( ...selectorMethods ) ( Component ) ;
219
208
220
- wrapper = mount ( createElement ( EnhancedComponent , defaultProps ) , {
221
- context : componentContext ,
222
- wrappingComponent : MockContextProvider ,
223
- } ) ;
209
+ jest . spyOn ( componentContext . $Utils . $Dispatcher , 'unlisten' ) ;
210
+
211
+ const { unmount } = await renderWithContext (
212
+ < EnhancedComponent { ...defaultProps } /> ,
213
+ {
214
+ contextValue : componentContext ,
215
+ }
216
+ ) ;
224
217
225
- wrapper . unmount ( ) ;
218
+ unmount ( ) ;
226
219
227
220
expect ( componentContext . $Utils . $Dispatcher . unlisten ) . toHaveBeenCalled ( ) ;
228
221
} ) ;
229
222
230
- it ( 'should render component with extraProps and own createStateSelector' , ( ) => {
223
+ it ( 'should render component with extraProps and own createStateSelector' , async ( ) => {
231
224
setCreatorOfStateSelector ( ( ...selectors ) => {
232
225
return ( state , context ) => {
233
226
return selectors . reduce ( ( result , selector ) => {
@@ -237,28 +230,32 @@ describe('plugin-select:', () => {
237
230
} ) ;
238
231
let EnhancedComponent = select ( ...selectorMethods ) ( Component ) ;
239
232
240
- wrapper = mount ( createElement ( EnhancedComponent , defaultProps ) , {
241
- context : componentContext ,
242
- wrappingComponent : MockContextProvider ,
243
- } ) ;
233
+ const { container } = await renderWithContext (
234
+ < EnhancedComponent { ...defaultProps } /> ,
235
+ {
236
+ contextValue : componentContext ,
237
+ }
238
+ ) ;
244
239
245
- expect ( wrapper ) . toMatchSnapshot ( ) ;
240
+ expect ( container . firstChild ) . toMatchSnapshot ( ) ;
246
241
} ) ;
247
242
248
- it ( 'should render component with changed props' , ( ) => {
243
+ it ( 'should render component with changed props' , async ( ) => {
249
244
let EnhancedComponent = select ( selectorUsingProps ) ( Component ) ;
250
245
251
- wrapper = mount ( createElement ( EnhancedComponent , defaultProps ) , {
252
- context : componentContext ,
253
- wrappingComponent : MockContextProvider ,
254
- } ) ;
246
+ const { container, rerender } = await renderWithContext (
247
+ < EnhancedComponent { ...defaultProps } /> ,
248
+ {
249
+ contextValue : componentContext ,
250
+ }
251
+ ) ;
255
252
256
- wrapper . setProps ( { multiplier : 3 } ) ;
253
+ rerender ( < EnhancedComponent { ... defaultProps } multiplier = { 3 } /> ) ;
257
254
258
- expect ( wrapper ) . toMatchSnapshot ( ) ;
255
+ expect ( container . firstChild ) . toMatchSnapshot ( ) ;
259
256
} ) ;
260
257
261
- it ( 'should render component with extraProps and own static methods' , ( ) => {
258
+ it ( 'should render component with extraProps and own static methods' , async ( ) => {
262
259
setHoistStaticMethod ( ( TargetComponent , Original ) => {
263
260
const keys = Object . getOwnPropertyNames ( Original ) ;
264
261
@@ -275,33 +272,34 @@ describe('plugin-select:', () => {
275
272
} ) ;
276
273
let EnhancedComponent = select ( ...selectorMethods ) ( Component ) ;
277
274
278
- wrapper = mount ( createElement ( EnhancedComponent , defaultProps ) , {
279
- context : componentContext ,
280
- wrappingComponent : MockContextProvider ,
281
- } ) ;
275
+ const { container } = await renderWithContext (
276
+ < EnhancedComponent { ...defaultProps } /> ,
277
+ {
278
+ contextValue : componentContext ,
279
+ }
280
+ ) ;
282
281
283
282
expect ( typeof EnhancedComponent . myCustom === 'function' ) . toBeTruthy ( ) ;
284
283
expect ( typeof EnhancedComponent . defaultProps === 'function' ) . toBeTruthy ( ) ;
285
- expect ( wrapper ) . toMatchSnapshot ( ) ;
284
+ expect ( container . firstChild ) . toMatchSnapshot ( ) ;
286
285
} ) ;
287
286
288
- it ( 'should forward ref' , ( ) => {
287
+ it ( 'should forward ref' , async ( ) => {
289
288
let EnhancedComponent = forwardedSelect ( ...selectorMethods ) ( Component ) ;
289
+ const componentRef = createRef ( ) ;
290
290
291
- wrapper = shallow (
292
- createElement ( EnhancedComponent , {
293
- ...defaultProps ,
294
- ref : createRef ( ) ,
295
- } ) ,
291
+ const { container } = await renderWithContext (
292
+ < EnhancedComponent { ...defaultProps } ref = { componentRef } /> ,
296
293
{
297
- context : componentContext ,
294
+ contextValue : componentContext ,
298
295
}
299
296
) ;
300
297
301
- expect ( wrapper ) . toMatchSnapshot ( ) ;
298
+ expect ( componentRef . current ) . toBeInstanceOf ( Component ) ;
299
+ expect ( container . firstChild ) . toMatchSnapshot ( ) ;
302
300
} ) ;
303
301
304
- it ( 'should render component with extraProps and own static methods for forwardedSelect' , ( ) => {
302
+ it ( 'should render component with extraProps and own static methods for forwardedSelect' , async ( ) => {
305
303
setHoistStaticMethod ( ( TargetComponent , Original ) => {
306
304
const keys = Object . getOwnPropertyNames ( Original ) ;
307
305
@@ -318,14 +316,16 @@ describe('plugin-select:', () => {
318
316
} ) ;
319
317
let EnhancedComponent = forwardedSelect ( ...selectorMethods ) ( Component ) ;
320
318
321
- wrapper = mount ( createElement ( EnhancedComponent , defaultProps ) , {
322
- context : componentContext ,
323
- wrappingComponent : MockContextProvider ,
324
- } ) ;
319
+ const { container } = await renderWithContext (
320
+ < EnhancedComponent { ...defaultProps } /> ,
321
+ {
322
+ contextValue : componentContext ,
323
+ }
324
+ ) ;
325
325
326
326
expect ( typeof EnhancedComponent . myCustom === 'function' ) . toBeTruthy ( ) ;
327
327
expect ( typeof EnhancedComponent . defaultProps === 'function' ) . toBeTruthy ( ) ;
328
- expect ( wrapper ) . toMatchSnapshot ( ) ;
328
+ expect ( container . firstChild ) . toMatchSnapshot ( ) ;
329
329
} ) ;
330
330
} ) ;
331
331
} ) ;
0 commit comments