@@ -549,3 +549,139 @@ describe('onFirstSelect callback : ', () => {
549
549
} ) ;
550
550
} ) ;
551
551
} ) ;
552
+ describe ( 'callbacks should be called with immutable parameters : ' , ( ) => {
553
+ test ( 'onSelect parameters should be immutable' , ( ) => {
554
+ renderApp ( ) ;
555
+ const onSelect1 = jest . fn ( ( param ) => {
556
+ param . previousSelectedTabId = 19 ;
557
+ } ) ;
558
+ const onSelect2 = jest . fn ( ( param ) => {
559
+ param . previousSelectedTabId = 20 ;
560
+ } ) ;
561
+ act ( ( ) => {
562
+ instance . setOption ( 'onSelect' , ( param ) => {
563
+ param . currentSelectedTabId = 10 ;
564
+ } ) ;
565
+ instance . on ( 'onSelect' , onSelect1 ) ;
566
+ instance . on ( 'onSelect' , onSelect2 ) ;
567
+ instance . select ( '2' ) ;
568
+ } ) ;
569
+ expect ( onSelect2 . mock . calls [ 0 ] [ 0 ] ) . toEqual ( {
570
+ currentSelectedTabId : '2' ,
571
+ perviousSelectedTabId : '1' ,
572
+ previousSelectedTabId : '1' ,
573
+ } ) ;
574
+ } ) ;
575
+ test ( 'onFirstSelect parameters should be immutable' , ( ) => {
576
+ renderApp ( ) ;
577
+ const onFirstSelect1 = jest . fn ( ( param ) => {
578
+ param . previousSelectedTabId = 19 ;
579
+ } ) ;
580
+ const onFirstSelect2 = jest . fn ( ( param ) => {
581
+ param . previousSelectedTabId = 20 ;
582
+ } ) ;
583
+ act ( ( ) => {
584
+ instance . setOption ( 'onFirstSelect' , ( param ) => {
585
+ param . currentSelectedTabId = 10 ;
586
+ } ) ;
587
+ instance . on ( 'onFirstSelect' , onFirstSelect1 ) ;
588
+ instance . on ( 'onFirstSelect' , onFirstSelect2 ) ;
589
+ instance . select ( '2' ) ;
590
+ } ) ;
591
+ expect ( onFirstSelect2 . mock . calls [ 0 ] [ 0 ] ) . toEqual ( {
592
+ currentSelectedTabId : '2' ,
593
+ previousSelectedTabId : '1' ,
594
+ } ) ;
595
+ } ) ;
596
+ test ( 'onOpen parameters should be immutable' , ( ) => {
597
+ renderApp ( ) ;
598
+ const onOpen1 = jest . fn ( ( openedTabIDs ) => {
599
+ openedTabIDs . push ( '5' ) ;
600
+ } ) ;
601
+ const onOpen2 = jest . fn ( ( openedTabIDs ) => {
602
+ openedTabIDs . push ( '6' ) ;
603
+ } ) ;
604
+ act ( ( ) => {
605
+ instance . setOption ( 'onOpen' , ( openedTabIDs ) => {
606
+ openedTabIDs . push ( '4' ) ;
607
+ } ) ;
608
+ instance . on ( 'onOpen' , onOpen1 ) ;
609
+ instance . on ( 'onOpen' , onOpen2 ) ;
610
+ instance . open ( { id : '3' } ) ;
611
+ } ) ;
612
+ expect ( onOpen2 . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ '3' ] ) ;
613
+ } ) ;
614
+ test ( 'onClose parameters should be immutable' , ( ) => {
615
+ renderApp ( ) ;
616
+ const onClose1 = jest . fn ( ( closedTabIDs ) => {
617
+ closedTabIDs . push ( '4' ) ;
618
+ } ) ;
619
+ const onClose2 = jest . fn ( ( closedTabIDs ) => {
620
+ closedTabIDs . push ( '5' ) ;
621
+ } ) ;
622
+ act ( ( ) => {
623
+ instance . setOption ( 'onClose' , ( closedTabIDs ) => {
624
+ closedTabIDs . push ( '3' ) ;
625
+ } ) ;
626
+ instance . on ( 'onClose' , onClose1 ) ;
627
+ instance . on ( 'onClose' , onClose2 ) ;
628
+ instance . close ( '2' ) ;
629
+ } ) ;
630
+ expect ( onClose2 . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ '2' ] ) ;
631
+ } ) ;
632
+ test ( 'onChange parameters should be immutable' , ( ) => {
633
+ renderApp ( ) ;
634
+ const onChange1 = jest . fn ( ( { currentData, previousData, closedTabIDs, openedTabIDs} ) => {
635
+ closedTabIDs . push ( '6' ) ;
636
+ openedTabIDs . push ( '6' ) ;
637
+ currentData . selectedTabID = '6' ;
638
+ previousData . selectedTabID = '6' ;
639
+ } ) ;
640
+ const onChange2 = jest . fn ( ( { currentData, previousData, closedTabIDs, openedTabIDs} ) => {
641
+ closedTabIDs . push ( '7' ) ;
642
+ openedTabIDs . push ( '7' ) ;
643
+ currentData . selectedTabID = '7' ;
644
+ previousData . selectedTabID = '7' ;
645
+ } ) ;
646
+ const onOpen = jest . fn ( ( ) => { } ) ;
647
+ const onClose = jest . fn ( ( ) => { } ) ;
648
+ const onSelect = jest . fn ( ( ) => { } ) ;
649
+ const onFirstSelect = jest . fn ( ( ) => { } ) ;
650
+ act ( ( ) => {
651
+ instance . setOption ( 'onChange' , ( { currentData, previousData, closedTabIDs, openedTabIDs} ) => {
652
+ closedTabIDs . push ( '5' ) ;
653
+ openedTabIDs . push ( '5' ) ;
654
+ currentData . selectedTabID = '5' ;
655
+ previousData . selectedTabID = '5' ;
656
+ } ) ;
657
+ instance . one ( 'onChange' , onChange1 ) ;
658
+ instance . on ( 'onChange' , onChange2 ) ;
659
+ instance . on ( 'onOpen' , onOpen ) ;
660
+ instance . on ( 'onClose' , onClose ) ;
661
+ instance . on ( 'onSelect' , onSelect ) ;
662
+ instance . on ( 'onFirstSelect' , onFirstSelect ) ;
663
+ instance . close ( '2' ) ;
664
+ instance . open ( { id : '3' } ) ;
665
+ instance . select ( '3' ) ;
666
+ } ) ;
667
+
668
+ expect ( onSelect . mock . calls [ 0 ] [ 0 ] ) . toEqual ( {
669
+ currentSelectedTabId : '3' ,
670
+ perviousSelectedTabId : '1' ,
671
+ previousSelectedTabId : '1' ,
672
+ } ) ;
673
+ expect ( onFirstSelect . mock . calls [ 0 ] [ 0 ] ) . toEqual ( {
674
+ currentSelectedTabId : '3' ,
675
+ previousSelectedTabId : '1' ,
676
+ } ) ;
677
+ expect ( onClose . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ '2' ] ) ;
678
+ expect ( onOpen . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ '3' ] ) ;
679
+ expect ( onChange2 . mock . calls [ 0 ] [ 0 ] ) . toEqual ( {
680
+ currentData : { selectedTabID : '3' , openTabIDs : [ '1' , '3' ] } ,
681
+ previousData : { selectedTabID : '1' , openTabIDs : [ '1' , '2' ] } ,
682
+ perviousData : { selectedTabID : '1' , openTabIDs : [ '1' , '2' ] } ,
683
+ closedTabIDs : [ '2' ] ,
684
+ openedTabIDs : [ '3' ] ,
685
+ } ) ;
686
+ } ) ;
687
+ } ) ;
0 commit comments