1
1
// Copyright (c) 2014 - 2023 UNICEF. All rights reserved.
2
2
3
3
import { fromJS , Map , OrderedMap } from "immutable" ;
4
- import Divider from "@material-ui/core/Divider" ;
5
- import CloseIcon from "@material-ui/icons/Close" ;
6
4
7
- import { APPROVALS , INCIDENT_FROM_CASE , REFERRAL , RECORD_INFORMATION_GROUP } from "../../../config" ;
8
- import { setupMountedComponent } from "../../../test" ;
5
+ import { APPROVALS , REFERRAL } from "../../../config" ;
6
+ import { fireEvent , mountedComponent , screen , setScreenSizeToMobile , waitFor } from "../../../test-utils " ;
9
7
import { FormSectionRecord , FieldRecord } from "../records" ;
10
- import { ConditionalWrapper } from "../../../libs" ;
11
8
import Actions from "../actions" ;
12
9
13
- import { NavGroup , RecordInformation } from "./components" ;
14
10
import Nav from "./component" ;
15
11
16
12
describe ( "<Nav />" , ( ) => {
17
- let component ;
18
-
19
13
const record = fromJS ( {
20
14
case_id : "12345" ,
21
15
case_id_display : "3c9d076" ,
@@ -186,59 +180,18 @@ describe("<Nav />", () => {
186
180
hasForms : true
187
181
} ;
188
182
189
- beforeEach ( ( ) => {
190
- ( { component } = setupMountedComponent ( Nav , props , initialState ) ) ;
183
+ beforeAll ( ( ) => {
184
+ setScreenSizeToMobile ( false ) ;
191
185
} ) ;
192
186
193
187
it ( "renders a CloseIcon component />" , ( ) => {
194
- expect ( component . find ( CloseIcon ) ) . to . have . lengthOf ( 1 ) ;
188
+ mountedComponent ( < Nav { ...props } /> , initialState ) ;
189
+ expect ( screen . getByTestId ( "close-icon" ) ) . toBeInTheDocument ( ) ;
195
190
} ) ;
196
191
197
192
it ( "renders a Nav component />" , ( ) => {
198
- expect ( component . find ( Nav ) ) . to . have . lengthOf ( 1 ) ;
199
- } ) ;
200
-
201
- it ( "renders a RecordInformation component />" , ( ) => {
202
- expect ( component . find ( RecordInformation ) ) . to . have . lengthOf ( 1 ) ;
203
- } ) ;
204
-
205
- it ( "renders a Divider component />" , ( ) => {
206
- expect ( component . find ( Divider ) ) . to . have . lengthOf ( 1 ) ;
207
- } ) ;
208
-
209
- it ( "renders a NavGroup component from record information and another one from the others forms groups />" , ( ) => {
210
- expect ( component . find ( NavGroup ) ) . to . have . lengthOf ( 2 ) ;
211
- } ) ;
212
-
213
- it ( "renders the NavGroup component for record information open" , ( ) => {
214
- expect ( component . find ( NavGroup ) . first ( ) . props ( ) . open ) . to . equal ( "record_information" ) ;
215
- } ) ;
216
-
217
- it ( "renders a ConditionalWrapper />" , ( ) => {
218
- expect ( component . find ( ConditionalWrapper ) ) . to . have . lengthOf ( 1 ) ;
219
- } ) ;
220
-
221
- it ( "should render valid props" , ( ) => {
222
- const navProps = { ...component . find ( Nav ) . props ( ) } ;
223
-
224
- expect ( component . find ( Nav ) ) . to . have . lengthOf ( 1 ) ;
225
- [
226
- "firstTab" ,
227
- "formNav" ,
228
- "handleToggleNav" ,
229
- "isNew" ,
230
- "mobileDisplay" ,
231
- "primeroModule" ,
232
- "recordType" ,
233
- "selectedForm" ,
234
- "selectedRecord" ,
235
- "toggleNav" ,
236
- "hasForms"
237
- ] . forEach ( property => {
238
- expect ( navProps ) . to . have . property ( property ) ;
239
- delete navProps [ property ] ;
240
- } ) ;
241
- expect ( navProps ) . to . be . empty ;
193
+ mountedComponent ( < Nav { ...props } /> , initialState ) ;
194
+ expect ( screen . getByTestId ( "nav-list" ) ) . toBeInTheDocument ( ) ;
242
195
} ) ;
243
196
244
197
describe ( "when is a new record" , ( ) => {
@@ -250,46 +203,9 @@ describe("<Nav />", () => {
250
203
isNew : true
251
204
} ;
252
205
253
- it ( "sets the firstTab as selectedForm" , ( ) => {
254
- const { component : newComp } = setupMountedComponent ( Nav , notSelectedProps , initialState ) ;
255
-
256
- const expectedAction = {
257
- type : Actions . SET_SELECTED_FORM ,
258
- payload : firstTab . unique_id
259
- } ;
260
-
261
- const setAction = newComp
262
- . props ( )
263
- . store . getActions ( )
264
- . find ( action => action . type === Actions . SET_SELECTED_FORM ) ;
265
-
266
- expect ( setAction ) . to . deep . equal ( expectedAction ) ;
267
- } ) ;
268
-
269
- it ( "opens the form_group_id of the firstTab" , ( ) => {
270
- const { component : newComp } = setupMountedComponent ( Nav , notSelectedProps , initialState ) ;
271
- const navGroup = newComp . find ( NavGroup ) . first ( ) ;
272
-
273
- expect ( navGroup . props ( ) . open ) . to . equal ( firstTab . form_group_id ) ;
274
- } ) ;
275
-
276
206
it ( "opens the selectedForm and group" , ( ) => {
277
- const { component : newComp } = setupMountedComponent (
278
- Nav ,
279
- { ...notSelectedProps , selectedForm : APPROVALS } ,
280
- initialState
281
- ) ;
282
-
283
- const setAction = newComp
284
- . props ( )
285
- . store . getActions ( )
286
- . find ( action => action . type === Actions . SET_SELECTED_FORM ) ;
287
-
288
- expect ( setAction ) . to . not . exist ;
289
-
290
- const navGroup = newComp . find ( NavGroup ) . first ( ) ;
291
-
292
- expect ( navGroup . props ( ) . open ) . to . equal ( RECORD_INFORMATION_GROUP ) ;
207
+ mountedComponent ( < Nav { ...{ ...notSelectedProps , selectedForm : APPROVALS } } /> , initialState ) ;
208
+ expect ( screen . getByText ( "forms.record_types.record_information" , { expanded : true } ) ) . toBeInTheDocument ( ) ;
293
209
} ) ;
294
210
} ) ;
295
211
@@ -303,28 +219,9 @@ describe("<Nav />", () => {
303
219
selectedForm : ""
304
220
} ;
305
221
306
- beforeEach ( ( ) => {
307
- ( { component } = setupMountedComponent ( Nav , notSelectedProps , initialState ) ) ;
308
- } ) ;
309
-
310
- it ( "sets the firstTab as selectedForm" , ( ) => {
311
- const expectedAction = {
312
- type : Actions . SET_SELECTED_FORM ,
313
- payload : "basic_identity"
314
- } ;
315
-
316
- const setAction = component
317
- . props ( )
318
- . store . getActions ( )
319
- . find ( action => action . type === Actions . SET_SELECTED_FORM ) ;
320
-
321
- expect ( setAction ) . to . deep . equal ( expectedAction ) ;
322
- } ) ;
323
-
324
222
it ( "opens the form_group_id of the firstTab" , ( ) => {
325
- const navGroup = component . find ( NavGroup ) . first ( ) ;
326
-
327
- expect ( navGroup . props ( ) . open ) . to . equal ( firstTab . form_group_id ) ;
223
+ mountedComponent ( < Nav { ...notSelectedProps } /> , initialState ) ;
224
+ expect ( screen . getByText ( "Basic Identity" , { expanded : true } ) ) . toBeInTheDocument ( ) ;
328
225
} ) ;
329
226
} ) ;
330
227
@@ -337,23 +234,15 @@ describe("<Nav />", () => {
337
234
selectedForm : REFERRAL
338
235
} ;
339
236
340
- beforeEach ( ( ) => {
341
- ( { component } = setupMountedComponent ( Nav , notSelectedProps , initialState ) ) ;
342
- } ) ;
343
-
344
237
it ( "should not select a different form" , ( ) => {
345
- const setAction = component
346
- . props ( )
347
- . store . getActions ( )
348
- . find ( action => action . type === Actions . SET_SELECTED_FORM ) ;
238
+ const { store } = mountedComponent ( < Nav { ...notSelectedProps } /> , initialState ) ;
349
239
350
- expect ( setAction ) . to . not . exist ;
240
+ expect ( store . getActions ( ) . find ( action => action . type === Actions . SET_SELECTED_FORM ) ) . toBeUndefined ( ) ;
351
241
} ) ;
352
242
353
243
it ( "opens the record_information group if it belongs to that group" , ( ) => {
354
- const navGroup = component . find ( NavGroup ) . first ( ) ;
355
-
356
- expect ( navGroup . props ( ) . open ) . to . equal ( RECORD_INFORMATION_GROUP ) ;
244
+ mountedComponent ( < Nav { ...notSelectedProps } /> , initialState ) ;
245
+ expect ( screen . getByText ( "forms.record_types.record_information" , { expanded : true } ) ) . toBeInTheDocument ( ) ;
357
246
} ) ;
358
247
} ) ;
359
248
@@ -366,28 +255,9 @@ describe("<Nav />", () => {
366
255
selectedForm : "unknown_form"
367
256
} ;
368
257
369
- beforeEach ( ( ) => {
370
- ( { component } = setupMountedComponent ( Nav , notSelectedProps , initialState ) ) ;
371
- } ) ;
372
-
373
- it ( "sets the firstTab as selectedForm" , ( ) => {
374
- const expectedAction = {
375
- type : Actions . SET_SELECTED_FORM ,
376
- payload : "basic_identity"
377
- } ;
378
-
379
- const setAction = component
380
- . props ( )
381
- . store . getActions ( )
382
- . find ( action => action . type === Actions . SET_SELECTED_FORM ) ;
383
-
384
- expect ( setAction ) . to . deep . equal ( expectedAction ) ;
385
- } ) ;
386
-
387
258
it ( "opens the form_group_id form the firstTab" , ( ) => {
388
- const navGroup = component . find ( NavGroup ) . first ( ) ;
389
-
390
- expect ( navGroup . props ( ) . open ) . to . equal ( firstTab . form_group_id ) ;
259
+ mountedComponent ( < Nav { ...notSelectedProps } /> , initialState ) ;
260
+ expect ( screen . getByText ( "Basic Identity" , { expanded : true } ) ) . toBeInTheDocument ( ) ;
391
261
} ) ;
392
262
} ) ;
393
263
@@ -447,14 +317,13 @@ describe("<Nav />", () => {
447
317
primeroModule : "primeromodule-cp"
448
318
} ;
449
319
450
- it ( "should select first form of the form group" , ( ) => {
451
- const { component : navComponent } = setupMountedComponent ( Nav , navGroupProps , initialState ) ;
452
- const registrationGroup = navComponent . find ( ".MuiListItem-gutters" ) . at ( 3 ) ;
320
+ it ( "should select first form of the form group" , async ( ) => {
321
+ mountedComponent ( < Nav { ... navGroupProps } /> , initialState ) ;
322
+ fireEvent . click ( screen . getByText ( "Identification / Registration" ) ) ;
453
323
454
- expect ( registrationGroup . text ( ) ) . to . be . equal ( "Identification / Registration" ) ;
455
-
456
- registrationGroup . simulate ( "click" ) ;
457
- expect ( navComponent . find ( ".Mui-selected" ) . at ( 0 ) . text ( ) ) . to . be . equal ( "Basic Identity" ) ;
324
+ await waitFor ( ( ) =>
325
+ expect ( screen . getByText ( "Basic Identity" ) . closest ( ".MuiButtonBase-root" ) ) . toHaveClass ( "navSelected" )
326
+ ) ;
458
327
} ) ;
459
328
} ) ;
460
329
@@ -469,40 +338,26 @@ describe("<Nav />", () => {
469
338
} ;
470
339
471
340
it ( "should open the record_information group if selectedForm belongs to that group " , ( ) => {
472
- const { component : navComp } = setupMountedComponent ( Nav , notSelectedProps , initialState ) ;
473
-
474
- const navGroup = navComp . find ( NavGroup ) . first ( ) ;
475
-
476
- expect ( navGroup . props ( ) . open ) . to . equal ( RECORD_INFORMATION_GROUP ) ;
341
+ mountedComponent ( < Nav { ...notSelectedProps } /> , initialState ) ;
342
+ expect ( screen . getByText ( "forms.record_types.record_information" , { expanded : true } ) ) . toBeInTheDocument ( ) ;
477
343
} ) ;
478
344
479
345
it ( "opens the record_information group and sets incidents froms if there is a incidentFromCase" , ( ) => {
480
346
const stateWithIncidentFromCase = initialState . setIn (
481
347
[ "records" , "cases" , "incidentFromCase" , "data" ] ,
482
348
fromJS ( { incident_case_id : "case-id-1" } )
483
349
) ;
484
- const { component : navComp } = setupMountedComponent (
485
- Nav ,
486
- { ...notSelectedProps , selectedForm : "" } ,
487
- stateWithIncidentFromCase
488
- ) ;
489
-
490
- const navGroup = navComp . find ( NavGroup ) . first ( ) ;
491
-
492
- expect ( navGroup . props ( ) . open ) . to . equal ( RECORD_INFORMATION_GROUP ) ;
493
-
494
- const expectedAction = {
495
- type : Actions . SET_SELECTED_FORM ,
496
- payload : INCIDENT_FROM_CASE
497
- } ;
498
350
499
- const setAction = navComp
500
- . props ( )
501
- . store . getActions ( )
502
- . filter ( action => action . type === Actions . SET_SELECTED_FORM )
503
- . pop ( ) ;
351
+ const tProps = { ...notSelectedProps , selectedForm : "" } ;
352
+ const { store } = mountedComponent ( < Nav { ...tProps } /> , stateWithIncidentFromCase ) ;
504
353
505
- expect ( setAction ) . to . deep . equal ( expectedAction ) ;
354
+ expect (
355
+ store
356
+ . getActions ( )
357
+ . filter ( action => action . type === Actions . SET_SELECTED_FORM )
358
+ . pop ( )
359
+ ) . toStrictEqual ( { type : "forms/SET_SELECTED_FORM" , payload : "incident_from_case" } ) ;
360
+ expect ( screen . getByText ( "forms.record_types.record_information" , { expanded : true } ) ) . toBeInTheDocument ( ) ;
506
361
} ) ;
507
362
508
363
it ( "opens the firstTab group and form when incident_from_case form is not found" , ( ) => {
@@ -511,52 +366,19 @@ describe("<Nav />", () => {
511
366
fromJS ( { incident_case_id : "case-id-1" } )
512
367
) ;
513
368
514
- const { component : navComp } = setupMountedComponent (
515
- Nav ,
516
- { ...notSelectedProps , recordType : "incidents" , selectedForm : "basic_identity" } ,
517
- stateWithIncidentFromCase
518
- ) ;
519
-
520
- const expectedAction = {
521
- type : Actions . SET_SELECTED_FORM ,
522
- payload : firstTab . unique_id
523
- } ;
524
-
525
- const setAction = navComp
526
- . props ( )
527
- . store . getActions ( )
528
- . filter ( action => action . type === Actions . SET_SELECTED_FORM )
529
- . pop ( ) ;
530
-
531
- const navGroup = navComp . find ( NavGroup ) . first ( ) ;
369
+ const tProps = { ...notSelectedProps , recordType : "incidents" , selectedForm : "basic_identity" } ;
370
+ const { store } = mountedComponent ( < Nav { ...tProps } /> , stateWithIncidentFromCase ) ;
532
371
533
- expect ( setAction ) . to . deep . equal ( expectedAction ) ;
534
- expect ( navGroup . props ( ) . open ) . to . equal ( firstTab . form_group_id ) ;
372
+ expect ( store . getActions ( ) ) . toStrictEqual ( [ { type : "forms/SET_SELECTED_FORM" , payload : "basic_identity" } ] ) ;
373
+ expect ( screen . getByText ( "Basic Identity" , { expanded : true } ) ) . toBeInTheDocument ( ) ;
535
374
} ) ;
536
375
537
376
it ( "opens the form_group_id and sets the selectedForm from the firstTab if the selected form is not found" , ( ) => {
538
- const { component : navComp } = setupMountedComponent (
539
- Nav ,
540
- { ...notSelectedProps , selectedForm : "unknown_form" } ,
541
- initialState
542
- ) ;
543
-
544
- const navGroup = navComp . find ( NavGroup ) . first ( ) ;
545
-
546
- expect ( navGroup . props ( ) . open ) . to . equal ( firstTab . form_group_id ) ;
547
-
548
- const expectedAction = {
549
- type : Actions . SET_SELECTED_FORM ,
550
- payload : firstTab . unique_id
551
- } ;
377
+ const tProps = { ...notSelectedProps , selectedForm : "unknown_form" } ;
378
+ const { store } = mountedComponent ( < Nav { ...tProps } /> , initialState ) ;
552
379
553
- const setAction = navComp
554
- . props ( )
555
- . store . getActions ( )
556
- . filter ( action => action . type === Actions . SET_SELECTED_FORM )
557
- . pop ( ) ;
558
-
559
- expect ( setAction ) . to . deep . equal ( expectedAction ) ;
380
+ expect ( store . getActions ( ) ) . toStrictEqual ( [ { type : "forms/SET_SELECTED_FORM" , payload : "basic_identity" } ] ) ;
381
+ expect ( screen . getByText ( "Basic Identity" , { expanded : true } ) ) . toBeInTheDocument ( ) ;
560
382
} ) ;
561
383
} ) ;
562
384
@@ -569,14 +391,9 @@ describe("<Nav />", () => {
569
391
hasForms : false
570
392
} ;
571
393
572
- beforeEach ( ( ) => {
573
- ( { component } = setupMountedComponent ( Nav , propsNoFirstTab , initialState ) ) ;
574
- } ) ;
575
-
576
394
it ( "should open record information" , ( ) => {
577
- const navGroup = component . find ( NavGroup ) . first ( ) ;
578
-
579
- expect ( navGroup . props ( ) . open ) . to . equal ( "record_information" ) ;
395
+ mountedComponent ( < Nav { ...propsNoFirstTab } /> , initialState ) ;
396
+ expect ( screen . getByText ( "forms.record_types.record_information" , { expanded : true } ) ) . toBeInTheDocument ( ) ;
580
397
} ) ;
581
398
} ) ;
582
399
} ) ;
0 commit comments