1
- import { mount , shallow } from 'enzyme' ;
2
- import { Collapsible } from '@edx/paragon' ;
1
+ /* eslint-disable import/no-extraneous-dependencies */
2
+ import { fireEvent , render , screen } from '@testing-library/react' ;
3
+ import { shallow } from '@edx/react-unit-test-utils' ;
3
4
import React from 'react' ;
4
- import renderer from 'react-test-renderer' ;
5
5
import { IntlProvider } from 'react-intl' ;
6
6
import { ConsolePage } from './ConsolePage' ;
7
- import ConnectedReportSection from '../report/reportSection' ;
8
7
9
8
let apiData = [ ] ;
10
9
@@ -41,13 +40,11 @@ describe('ConsolePage', () => {
41
40
/>
42
41
</ IntlProvider >
43
42
) ;
44
- const wrapper = mount ( consolePageComponent ) ;
45
- const tree = renderer . create ( consolePageComponent ) ;
43
+ const { container } = render ( consolePageComponent ) ;
46
44
47
- expect ( tree ) . toMatchSnapshot ( ) ;
48
- expect ( wrapper . exists ( '.alert.alert-warning.show' ) ) . toEqual ( false ) ;
49
- expect ( wrapper . exists ( '.alert.alert-danger.show' ) ) . toEqual ( false ) ;
50
- wrapper . unmount ( ) ;
45
+ expect ( container ) . toMatchSnapshot ( ) ;
46
+ expect ( container . querySelector ( '.alert-warning.show' ) ) . toBeNull ( ) ;
47
+ expect ( container . querySelector ( '.alert-danger.show' ) ) . toBeNull ( ) ;
51
48
} ) ;
52
49
53
50
it ( 'renders a warning banner if there is not an authorized user' , ( ) => {
@@ -66,13 +63,11 @@ describe('ConsolePage', () => {
66
63
/>
67
64
</ IntlProvider >
68
65
) ;
69
- const wrapper = mount ( consolePageComponent ) ;
70
- const tree = renderer . create ( consolePageComponent ) ;
66
+ const { container } = render ( consolePageComponent ) ;
71
67
72
- expect ( tree ) . toMatchSnapshot ( ) ;
73
- expect ( wrapper . exists ( '.alert.alert-warning.show' ) ) . toEqual ( true ) ;
74
- expect ( wrapper . exists ( '.alert.alert-danger.show' ) ) . toEqual ( false ) ;
75
- wrapper . unmount ( ) ;
68
+ expect ( container ) . toMatchSnapshot ( ) ;
69
+ expect ( container . querySelector ( '.alert-warning.show' ) ) . toBeInTheDocument ( ) ;
70
+ expect ( container . querySelector ( '.alert-danger.show' ) ) . toBeNull ( ) ;
76
71
} ) ;
77
72
78
73
it ( 'renders an error banner when there was an error loading programs' , ( ) => {
@@ -92,13 +87,11 @@ describe('ConsolePage', () => {
92
87
/>
93
88
</ IntlProvider >
94
89
) ;
95
- const wrapper = mount ( consolePageComponent ) ;
96
- const tree = renderer . create ( consolePageComponent ) ;
90
+ const { container } = render ( consolePageComponent ) ;
97
91
98
- expect ( tree ) . toMatchSnapshot ( ) ;
99
- expect ( wrapper . exists ( '.alert.alert-warning.show' ) ) . toEqual ( false ) ;
100
- expect ( wrapper . exists ( '.alert.alert-danger.show' ) ) . toEqual ( true ) ;
101
- wrapper . unmount ( ) ;
92
+ expect ( container ) . toMatchSnapshot ( ) ;
93
+ expect ( container . querySelector ( '.alert-warning.show' ) ) . toBeNull ( ) ;
94
+ expect ( container . querySelector ( '.alert-danger.show' ) ) . toBeInTheDocument ( ) ;
102
95
} ) ;
103
96
104
97
it ( 'renders programs when there is data passed in' , ( ) => {
@@ -117,20 +110,17 @@ describe('ConsolePage', () => {
117
110
/>
118
111
</ IntlProvider >
119
112
) ;
120
- const wrapper = mount ( consolePageComponent ) ;
121
- const tree = renderer . create ( consolePageComponent ) . toJSON ( ) ;
113
+ const { container } = render ( consolePageComponent ) ;
122
114
123
- expect ( tree ) . toMatchSnapshot ( ) ;
115
+ expect ( container ) . toMatchSnapshot ( ) ;
124
116
apiData . forEach ( ( program , idx ) => {
125
- expect ( wrapper . find ( 'h2' ) . at ( idx ) . text ( ) ) . toEqual ( program . programTitle ) ;
126
- expect ( wrapper . find ( Collapsible ) . at ( idx ) . prop ( 'defaultOpen' ) ) . toEqual ( true ) ;
117
+ expect ( screen . getByText ( program . programTitle ) ) . toBeInTheDocument ( ) ;
118
+ expect ( container . querySelectorAll ( '.pgn_collapsible' ) [ idx ] ) . toHaveClass ( 'is-open' ) ;
127
119
} ) ;
128
120
129
- expect ( wrapper . exists ( '.alert-danger.show' ) ) . toEqual ( false ) ;
130
- expect ( wrapper . exists ( '.alert-info.show' ) ) . toEqual ( false ) ;
131
- expect ( wrapper . exists ( '.alert-warning.show' ) ) . toEqual ( false ) ;
132
-
133
- wrapper . unmount ( ) ;
121
+ expect ( container . querySelector ( '.alert-danger.show' ) ) . toBeNull ( ) ;
122
+ expect ( container . querySelector ( '.alert-info.show' ) ) . toBeNull ( ) ;
123
+ expect ( container . querySelector ( '.alert-warning.show' ) ) . toBeNull ( ) ;
134
124
} ) ;
135
125
136
126
it ( 'passes isFirstSection=true to report section when there is no enrollment section' , ( ) => {
@@ -157,20 +147,18 @@ describe('ConsolePage', () => {
157
147
158
148
// shallow render ConsolePage to avoid fully rendering the ConnectedReportSection,
159
149
// which requires a Redux store
160
- const wrapper = shallow ( consolePageComponent ) ;
150
+ const { instance } = shallow ( consolePageComponent ) ;
161
151
162
- expect ( wrapper . find ( 'h2' ) . text ( ) ) . toEqual ( apiData [ 0 ] . programTitle ) ;
152
+ expect ( instance . findByType ( 'h2' ) [ 0 ] . el . children [ 0 ] ) . toEqual ( apiData [ 0 ] . programTitle ) ;
163
153
164
154
// we don't expect to see the enrollment section
165
- expect ( wrapper . exists ( Collapsible ) ) . toEqual ( false ) ;
166
-
167
- expect ( wrapper . find ( ConnectedReportSection ) . prop ( 'isFirstSection' ) ) . toEqual ( true ) ;
155
+ expect ( instance . findByTestId ( 'collapsible' ) ) . toEqual ( [ ] ) ;
168
156
169
- expect ( wrapper . exists ( '.alert-danger' ) ) . toEqual ( false ) ;
170
- expect ( wrapper . exists ( '.alert-info' ) ) . toEqual ( false ) ;
171
- expect ( wrapper . exists ( '.alert-warning.show' ) ) . toEqual ( false ) ;
157
+ expect ( instance . findByTestId ( 'report-section' ) [ 0 ] . props . isFirstSection ) . toEqual ( true ) ;
172
158
173
- wrapper . unmount ( ) ;
159
+ expect ( instance . findByTestId ( 'alert-danger' ) [ 0 ] . props . show ) . toEqual ( false ) ;
160
+ expect ( instance . findByTestId ( 'alert-info' ) ) . toEqual ( [ ] ) ;
161
+ expect ( instance . findByTestId ( 'alert-warning' ) [ 0 ] . props . show ) . toEqual ( false ) ;
174
162
} ) ;
175
163
176
164
it ( 'passes isFirstSection=false to report section when there is an enrollment section' , ( ) => {
@@ -197,21 +185,19 @@ describe('ConsolePage', () => {
197
185
198
186
// shallow render ConsolePage to avoid fully rendering the ConnectedReportSection,
199
187
// which requires a Redux store
200
- const wrapper = shallow ( consolePageComponent ) ;
201
-
202
- expect ( wrapper . find ( 'h2' ) . text ( ) ) . toEqual ( apiData [ 0 ] . programTitle ) ;
188
+ const { instance } = shallow ( consolePageComponent ) ;
203
189
204
- const collapsible = wrapper . find ( Collapsible ) ;
205
- expect ( collapsible . prop ( 'title' ) ) . toEqual ( 'Manage Enrollments' ) ;
206
- expect ( collapsible . prop ( 'defaultOpen' ) ) . toEqual ( true ) ;
190
+ expect ( instance . findByType ( 'h2' ) [ 0 ] . el . children [ 0 ] ) . toEqual ( apiData [ 0 ] . programTitle ) ;
207
191
208
- expect ( wrapper . find ( ConnectedReportSection ) . prop ( 'isFirstSection' ) ) . toEqual ( false ) ;
192
+ const collapsible = instance . findByTestId ( 'collapsible' ) ;
193
+ expect ( collapsible [ 0 ] . props . title ) . toEqual ( 'Manage Enrollments' ) ;
194
+ expect ( collapsible [ 0 ] . props . defaultOpen ) . toEqual ( true ) ;
209
195
210
- expect ( wrapper . exists ( '.alert-danger' ) ) . toEqual ( false ) ;
211
- expect ( wrapper . exists ( '.alert-info' ) ) . toEqual ( false ) ;
212
- expect ( wrapper . exists ( '.alert-warning.show' ) ) . toEqual ( false ) ;
196
+ expect ( instance . findByTestId ( 'report-section' ) [ 0 ] . props . isFirstSection ) . toEqual ( false ) ;
213
197
214
- wrapper . unmount ( ) ;
198
+ expect ( instance . findByTestId ( 'alert-danger' ) [ 0 ] . props . show ) . toEqual ( false ) ;
199
+ expect ( instance . findByTestId ( 'alert-info' ) ) . toEqual ( [ ] ) ;
200
+ expect ( instance . findByTestId ( 'alert-warning' ) [ 0 ] . props . show ) . toEqual ( false ) ;
215
201
} ) ;
216
202
217
203
it ( 'renders program banners when they are included' , ( ) => {
@@ -242,22 +228,19 @@ describe('ConsolePage', () => {
242
228
/>
243
229
</ IntlProvider >
244
230
) ;
245
- const wrapper = mount ( consolePageComponent ) ;
246
- const tree = renderer . create ( consolePageComponent ) . toJSON ( ) ;
231
+ const { container } = render ( consolePageComponent ) ;
247
232
248
- expect ( tree ) . toMatchSnapshot ( ) ;
249
- expect ( wrapper . find ( '.alert-danger.show .modal-alert' ) . at ( 0 ) . text ( ) ) . toEqual ( 'Sorry something went wrong ' ) ;
250
- expect ( wrapper . find ( '.alert-success.show .modal-alert' ) . at ( 0 ) . text ( ) ) . toEqual ( 'You did it! ' ) ;
251
-
252
- wrapper . unmount ( ) ;
233
+ expect ( container ) . toMatchSnapshot ( ) ;
234
+ expect ( container . querySelector ( '.alert-danger.show' ) ) . toHaveTextContent ( 'Sorry something went wrong ' ) ;
235
+ expect ( container . querySelector ( '.alert-success.show' ) ) . toHaveTextContent ( 'You did it! ' ) ;
253
236
} ) ;
254
237
255
238
it ( 'calls the fetchPrograms function on pageload' , ( ) => {
256
239
const mock = jest . fn ( ) ;
257
240
258
241
expect ( mock ) . not . toHaveBeenCalled ( ) ;
259
242
260
- mount (
243
+ render (
261
244
< IntlProvider locale = "en" >
262
245
< ConsolePage
263
246
authorized
@@ -281,7 +264,7 @@ describe('ConsolePage', () => {
281
264
282
265
expect ( mock ) . not . toHaveBeenCalled ( ) ;
283
266
284
- const wrapper = mount (
267
+ const { container } = render (
285
268
< IntlProvider locale = "en" >
286
269
< ConsolePage
287
270
authorized
@@ -297,8 +280,8 @@ describe('ConsolePage', () => {
297
280
</ IntlProvider > ,
298
281
) ;
299
282
300
- const filterForm = wrapper . find ( 'form' ) ;
301
- filterForm . simulate ( ' submit' ) ;
283
+ const filterForm = container . querySelector ( 'form' ) ;
284
+ fireEvent . submit ( filterForm ) ;
302
285
expect ( mock ) . toHaveBeenCalled ( ) ;
303
286
} ) ;
304
287
@@ -319,19 +302,17 @@ describe('ConsolePage', () => {
319
302
/>
320
303
</ IntlProvider >
321
304
) ;
322
- const wrapper = mount ( consolePageComponent ) ;
323
- const tree = renderer . create ( consolePageComponent ) ;
305
+ const { container : tree } = render ( consolePageComponent ) ;
324
306
325
307
expect ( tree ) . toMatchSnapshot ( ) ;
326
- const alert = wrapper . find ( '[data-testid="filter-alert"]' ) . first ( ) ;
327
- expect ( alert . text ( ) ) . toContain ( 'Invalid' ) ;
328
- wrapper . unmount ( ) ;
308
+ const alert = screen . getByTestId ( 'error-alert' ) ;
309
+ expect ( alert ) . toHaveTextContent ( 'Invalid' ) ;
329
310
} ) ;
330
311
331
312
it ( 'calls the correct action with the correct program key on download button clicks' , ( ) => {
332
313
const mock = jest . fn ( ) ;
333
314
334
- const wrapper = mount (
315
+ const { container } = render (
335
316
< IntlProvider locale = "en" >
336
317
< ConsolePage
337
318
authorized
@@ -347,31 +328,31 @@ describe('ConsolePage', () => {
347
328
</ IntlProvider > ,
348
329
) ;
349
330
350
- const programADownloadProgramButton = wrapper . find ( 'button.btn.btn-outline-primary' ) . at ( 0 ) ;
351
- expect ( programADownloadProgramButton . text ( ) ) . toEqual ( 'Download Program Enrollments' ) ;
352
- programADownloadProgramButton . simulate ( ' click' ) ;
331
+ const programADownloadProgramButton = container . querySelectorAll ( 'button.btn.btn-outline-primary' ) [ 0 ] ;
332
+ expect ( programADownloadProgramButton ) . toHaveTextContent ( 'Download Program Enrollments' ) ;
333
+ fireEvent . click ( programADownloadProgramButton ) ;
353
334
expect ( mock ) . toHaveBeenCalledWith ( 'a' , false ) ;
354
335
355
- const programADownloadCourseButton = wrapper . find ( 'button.btn.btn-outline-primary' ) . at ( 1 ) ;
356
- expect ( programADownloadCourseButton . text ( ) ) . toEqual ( 'Download Course Enrollments' ) ;
357
- programADownloadCourseButton . simulate ( ' click' ) ;
336
+ const programADownloadCourseButton = container . querySelectorAll ( 'button.btn.btn-outline-primary' ) [ 1 ] ;
337
+ expect ( programADownloadCourseButton ) . toHaveTextContent ( 'Download Course Enrollments' ) ;
338
+ fireEvent . click ( programADownloadCourseButton ) ;
358
339
expect ( mock ) . toHaveBeenCalledWith ( 'a' , true ) ;
359
340
360
- const programBDownloadProgramButton = wrapper . find ( 'button.btn.btn-outline-primary' ) . at ( 2 ) ;
361
- expect ( programBDownloadProgramButton . text ( ) ) . toEqual ( 'Download Program Enrollments' ) ;
362
- programBDownloadProgramButton . simulate ( ' click' ) ;
341
+ const programBDownloadProgramButton = container . querySelectorAll ( 'button.btn.btn-outline-primary' ) [ 2 ] ;
342
+ expect ( programBDownloadProgramButton ) . toHaveTextContent ( 'Download Program Enrollments' ) ;
343
+ fireEvent . click ( programBDownloadProgramButton ) ;
363
344
expect ( mock ) . toHaveBeenCalledWith ( 'b' , false ) ;
364
345
365
- const programBDownloadCourseButton = wrapper . find ( 'button.btn.btn-outline-primary' ) . at ( 3 ) ;
366
- expect ( programBDownloadCourseButton . text ( ) ) . toEqual ( 'Download Course Enrollments' ) ;
367
- programBDownloadCourseButton . simulate ( ' click' ) ;
346
+ const programBDownloadCourseButton = container . querySelectorAll ( 'button.btn.btn-outline-primary' ) [ 3 ] ;
347
+ expect ( programBDownloadCourseButton ) . toHaveTextContent ( 'Download Course Enrollments' ) ;
348
+ fireEvent . click ( programBDownloadCourseButton ) ;
368
349
expect ( mock ) . toHaveBeenCalledWith ( 'b' , true ) ;
369
350
} ) ;
370
351
371
352
it ( 'calls the correct action with the correct program key onchange of the file inputs' , ( ) => {
372
353
const mock = jest . fn ( ) ;
373
354
374
- const wrapper = mount (
355
+ const { container } = render (
375
356
< IntlProvider locale = "en" >
376
357
< ConsolePage
377
358
authorized
@@ -390,13 +371,18 @@ describe('ConsolePage', () => {
390
371
const file = new File ( [ ] , 'test' ) ;
391
372
392
373
expect ( mock ) . not . toHaveBeenCalled ( ) ;
393
- wrapper . find ( '.input-overlay-hack' ) . at ( 0 ) . simulate ( 'change' , { target : { name : 'pollName' , files : [ file ] } } ) ;
374
+ const fileInputs = container . querySelectorAll ( '.input-overlay-hack' ) ;
375
+
376
+ fireEvent . change ( fileInputs [ 0 ] , { target : { files : [ file ] } } ) ;
394
377
expect ( mock ) . toHaveBeenCalledWith ( 'a' , false , file ) ;
395
- wrapper . find ( '.input-overlay-hack' ) . at ( 1 ) . simulate ( 'change' , { target : { name : 'pollName' , files : [ file ] } } ) ;
378
+
379
+ fireEvent . change ( fileInputs [ 1 ] , { target : { files : [ file ] } } ) ;
396
380
expect ( mock ) . toHaveBeenCalledWith ( 'a' , true , file ) ;
397
- wrapper . find ( '.input-overlay-hack' ) . at ( 2 ) . simulate ( 'change' , { target : { name : 'pollName' , files : [ file ] } } ) ;
381
+
382
+ fireEvent . change ( fileInputs [ 2 ] , { target : { files : [ file ] } } ) ;
398
383
expect ( mock ) . toHaveBeenCalledWith ( 'b' , false , file ) ;
399
- wrapper . find ( '.input-overlay-hack' ) . at ( 3 ) . simulate ( 'change' , { target : { name : 'pollName' , files : [ file ] } } ) ;
384
+
385
+ fireEvent . change ( fileInputs [ 3 ] , { target : { files : [ file ] } } ) ;
400
386
expect ( mock ) . toHaveBeenCalledWith ( 'b' , true , file ) ;
401
387
} ) ;
402
388
@@ -428,10 +414,9 @@ describe('ConsolePage', () => {
428
414
/>
429
415
</ IntlProvider >
430
416
) ;
431
- const wrapperOne = mount ( consolePageComponentOne ) ;
432
- const programTitleZero = wrapperOne . find ( 'h2' ) . first ( ) ;
433
- expect ( programTitleZero . text ( ) ) . toEqual ( 'program 0' ) ;
434
- wrapperOne . unmount ( ) ;
417
+ const wrapperOne = render ( consolePageComponentOne ) ;
418
+ const programTitleZero = wrapperOne . container . querySelector ( 'h2' ) ;
419
+ expect ( programTitleZero ) . toHaveTextContent ( 'program 0' ) ;
435
420
436
421
const consolePageComponentTwo = (
437
422
< IntlProvider locale = "en" >
@@ -449,9 +434,8 @@ describe('ConsolePage', () => {
449
434
/>
450
435
</ IntlProvider >
451
436
) ;
452
- const wrapperTwo = mount ( consolePageComponentTwo ) ;
453
- const programTitleTen = wrapperTwo . find ( 'h2' ) . first ( ) ;
454
- expect ( programTitleTen . text ( ) ) . toEqual ( 'program 10' ) ;
455
- wrapperTwo . unmount ( ) ;
437
+ const wrapperTwo = render ( consolePageComponentTwo ) ;
438
+ const programTitleTen = wrapperTwo . container . querySelector ( 'h2' ) ;
439
+ expect ( programTitleTen ) . toHaveTextContent ( 'program 10' ) ;
456
440
} ) ;
457
441
} ) ;
0 commit comments