24
24
import { createMouseEvent , emitEvent } from '../../../testing/dom/events' ;
25
25
import { verifyDefaultAdapter } from '../../../testing/helpers/foundation' ;
26
26
import { setUpFoundationTest , setUpMdcTestEnvironment } from '../../../testing/helpers/setup' ;
27
+ import { MDCTooltipAdapter } from '../adapter' ;
27
28
import { AnchorBoundaryType , attributes , CssClasses , numbers , XPosition , YPosition } from '../constants' ;
28
29
import { MDCTooltipFoundation } from '../foundation' ;
29
30
@@ -32,6 +33,80 @@ const ESC_EVENTS = [
32
33
{ type : 'keydown' , keyCode : 27 , target : { } } ,
33
34
] ;
34
35
36
+ // This function assumes that the foundation has already been initialized for
37
+ // rich tooltips. If isRich and isPersistent have not been initialized, the
38
+ // checks for rich tooltip and persistent rich tooltips will not be called. This
39
+ // function also assumes that isShown is false, since the checks for isShown
40
+ // being true is trivial.
41
+ function expectShowToBeCalled (
42
+ foundation : MDCTooltipFoundation ,
43
+ mockAdapter : jasmine . SpyObj < MDCTooltipAdapter > ) {
44
+ expect ( foundation [ 'hideTimeout' ] ) . toEqual ( null ) ;
45
+ expect ( foundation [ 'showTimeout' ] ) . toEqual ( null ) ;
46
+
47
+ if ( foundation . getIsRich ( ) ) {
48
+ expect ( mockAdapter . setAnchorAttribute )
49
+ . toHaveBeenCalledWith ( 'aria-expanded' , 'true' ) ;
50
+ if ( ! foundation . getIsPersistent ( ) ) {
51
+ expect ( mockAdapter . registerEventHandler )
52
+ . toHaveBeenCalledWith ( 'mouseenter' , jasmine . any ( Function ) ) ;
53
+ expect ( mockAdapter . registerEventHandler )
54
+ . toHaveBeenCalledWith ( 'mouseleave' , jasmine . any ( Function ) ) ;
55
+ }
56
+ }
57
+
58
+ expect ( mockAdapter . setAttribute ) . toHaveBeenCalledWith ( 'aria-hidden' , 'false' ) ;
59
+ expect ( mockAdapter . removeClass ) . toHaveBeenCalledWith ( CssClasses . HIDE ) ;
60
+ expect ( mockAdapter . addClass ) . toHaveBeenCalledWith ( CssClasses . SHOWING ) ;
61
+
62
+ expect ( mockAdapter . registerDocumentEventHandler )
63
+ . toHaveBeenCalledWith ( 'click' , jasmine . any ( Function ) ) ;
64
+ expect ( mockAdapter . registerDocumentEventHandler )
65
+ . toHaveBeenCalledWith ( 'keydown' , jasmine . any ( Function ) ) ;
66
+
67
+ expect ( mockAdapter . registerWindowEventHandler )
68
+ . toHaveBeenCalledWith ( 'scroll' , jasmine . any ( Function ) ) ;
69
+ expect ( mockAdapter . registerWindowEventHandler )
70
+ . toHaveBeenCalledWith ( 'resize' , jasmine . any ( Function ) ) ;
71
+ }
72
+
73
+ // This function assumes that the foundation has already been initialized for
74
+ // rich tooltips. If isRich and isPersistent have not been initialized, the
75
+ // checks for rich tooltip and persistent rich tooltips will not be called. This
76
+ // function also assumes that isShown is true, since the checks for isShown
77
+ // being false is trivial.
78
+ function expectHideToBeCalled (
79
+ foundation : MDCTooltipFoundation ,
80
+ mockAdapter : jasmine . SpyObj < MDCTooltipAdapter > ) {
81
+ expect ( foundation [ 'hideTimeout' ] ) . toEqual ( null ) ;
82
+ expect ( foundation [ 'showTimeout' ] ) . toEqual ( null ) ;
83
+
84
+ if ( foundation . getIsRich ( ) ) {
85
+ expect ( mockAdapter . setAnchorAttribute )
86
+ . toHaveBeenCalledWith ( 'aria-expanded' , 'false' ) ;
87
+ if ( ! foundation . getIsPersistent ( ) ) {
88
+ expect ( mockAdapter . deregisterEventHandler )
89
+ . toHaveBeenCalledWith ( 'mouseenter' , jasmine . any ( Function ) ) ;
90
+ expect ( mockAdapter . deregisterEventHandler )
91
+ . toHaveBeenCalledWith ( 'mouseleave' , jasmine . any ( Function ) ) ;
92
+ }
93
+ }
94
+
95
+ expect ( mockAdapter . setAttribute ) . toHaveBeenCalledWith ( 'aria-hidden' , 'true' ) ;
96
+ expect ( mockAdapter . addClass ) . toHaveBeenCalledWith ( CssClasses . HIDE ) ;
97
+ expect ( mockAdapter . addClass ) . toHaveBeenCalledWith ( CssClasses . HIDE_TRANSITION ) ;
98
+ expect ( mockAdapter . removeClass ) . toHaveBeenCalledWith ( CssClasses . SHOWN ) ;
99
+
100
+ expect ( mockAdapter . deregisterDocumentEventHandler )
101
+ . toHaveBeenCalledWith ( 'click' , jasmine . any ( Function ) ) ;
102
+ expect ( mockAdapter . deregisterDocumentEventHandler )
103
+ . toHaveBeenCalledWith ( 'keydown' , jasmine . any ( Function ) ) ;
104
+ expect ( mockAdapter . deregisterWindowEventHandler )
105
+ . toHaveBeenCalledWith ( 'scroll' , jasmine . any ( Function ) ) ;
106
+ expect ( mockAdapter . deregisterWindowEventHandler )
107
+ . toHaveBeenCalledWith ( 'resize' , jasmine . any ( Function ) ) ;
108
+ }
109
+
35
110
describe ( 'MDCTooltipFoundation' , ( ) => {
36
111
setUpMdcTestEnvironment ( ) ;
37
112
@@ -439,19 +514,7 @@ describe('MDCTooltipFoundation', () => {
439
514
foundation . show ( ) ;
440
515
foundation . handleDocumentClick ( mockClickEvent ) ;
441
516
442
- expect ( foundation . hideTimeout ) . toEqual ( null ) ;
443
- expect ( mockAdapter . setAttribute )
444
- . toHaveBeenCalledWith ( 'aria-hidden' , 'true' ) ;
445
- expect ( mockAdapter . addClass ) . toHaveBeenCalledWith ( CssClasses . HIDE ) ;
446
- expect ( mockAdapter . addClass )
447
- . toHaveBeenCalledWith ( CssClasses . HIDE_TRANSITION ) ;
448
- expect ( mockAdapter . removeClass ) . toHaveBeenCalledWith ( CssClasses . SHOWN ) ;
449
- expect ( mockAdapter . removeClass )
450
- . toHaveBeenCalledWith ( CssClasses . SHOWING_TRANSITION ) ;
451
- expect ( mockAdapter . deregisterDocumentEventHandler )
452
- . toHaveBeenCalledWith ( 'click' , jasmine . any ( Function ) ) ;
453
- expect ( mockAdapter . deregisterDocumentEventHandler )
454
- . toHaveBeenCalledWith ( 'keydown' , jasmine . any ( Function ) ) ;
517
+ expectHideToBeCalled ( foundation , mockAdapter ) ;
455
518
} ) ;
456
519
457
520
it ( '#handleDocumentClick hides the tooltip immediately for default rich tooltips' ,
@@ -465,21 +528,7 @@ describe('MDCTooltipFoundation', () => {
465
528
foundation . show ( ) ;
466
529
foundation . handleDocumentClick ( mockClickEvent ) ;
467
530
468
- expect ( foundation . hideTimeout ) . toEqual ( null ) ;
469
- expect ( mockAdapter . setAttribute )
470
- . toHaveBeenCalledWith ( 'aria-hidden' , 'true' ) ;
471
- expect ( mockAdapter . setAnchorAttribute )
472
- . toHaveBeenCalledWith ( 'aria-expanded' , 'false' ) ;
473
- expect ( mockAdapter . addClass ) . toHaveBeenCalledWith ( CssClasses . HIDE ) ;
474
- expect ( mockAdapter . addClass )
475
- . toHaveBeenCalledWith ( CssClasses . HIDE_TRANSITION ) ;
476
- expect ( mockAdapter . removeClass ) . toHaveBeenCalledWith ( CssClasses . SHOWN ) ;
477
- expect ( mockAdapter . removeClass )
478
- . toHaveBeenCalledWith ( CssClasses . SHOWING_TRANSITION ) ;
479
- expect ( mockAdapter . deregisterDocumentEventHandler )
480
- . toHaveBeenCalledWith ( 'click' , jasmine . any ( Function ) ) ;
481
- expect ( mockAdapter . deregisterDocumentEventHandler )
482
- . toHaveBeenCalledWith ( 'keydown' , jasmine . any ( Function ) ) ;
531
+ expectHideToBeCalled ( foundation , mockAdapter ) ;
483
532
} ) ;
484
533
485
534
it ( '#handleDocumentClick hides the tooltip immediately for persistent rich tooltips if there is no event target' ,
@@ -495,21 +544,7 @@ describe('MDCTooltipFoundation', () => {
495
544
foundation . show ( ) ;
496
545
foundation . handleDocumentClick ( mockClickEvent ) ;
497
546
498
- expect ( foundation . hideTimeout ) . toEqual ( null ) ;
499
- expect ( mockAdapter . setAttribute )
500
- . toHaveBeenCalledWith ( 'aria-hidden' , 'true' ) ;
501
- expect ( mockAdapter . setAnchorAttribute )
502
- . toHaveBeenCalledWith ( 'aria-expanded' , 'false' ) ;
503
- expect ( mockAdapter . addClass ) . toHaveBeenCalledWith ( CssClasses . HIDE ) ;
504
- expect ( mockAdapter . addClass )
505
- . toHaveBeenCalledWith ( CssClasses . HIDE_TRANSITION ) ;
506
- expect ( mockAdapter . removeClass ) . toHaveBeenCalledWith ( CssClasses . SHOWN ) ;
507
- expect ( mockAdapter . removeClass )
508
- . toHaveBeenCalledWith ( CssClasses . SHOWING_TRANSITION ) ;
509
- expect ( mockAdapter . deregisterDocumentEventHandler )
510
- . toHaveBeenCalledWith ( 'click' , jasmine . any ( Function ) ) ;
511
- expect ( mockAdapter . deregisterDocumentEventHandler )
512
- . toHaveBeenCalledWith ( 'keydown' , jasmine . any ( Function ) ) ;
547
+ expectHideToBeCalled ( foundation , mockAdapter ) ;
513
548
} ) ;
514
549
515
550
it ( '#handleDocumentClick hides the tooltip immediately for persistent rich tooltips if event target is not HTMLElement' ,
@@ -528,21 +563,7 @@ describe('MDCTooltipFoundation', () => {
528
563
foundation . show ( ) ;
529
564
foundation . handleDocumentClick ( mockClickEvent ) ;
530
565
531
- expect ( foundation . hideTimeout ) . toEqual ( null ) ;
532
- expect ( mockAdapter . setAttribute )
533
- . toHaveBeenCalledWith ( 'aria-hidden' , 'true' ) ;
534
- expect ( mockAdapter . setAnchorAttribute )
535
- . toHaveBeenCalledWith ( 'aria-expanded' , 'false' ) ;
536
- expect ( mockAdapter . addClass ) . toHaveBeenCalledWith ( CssClasses . HIDE ) ;
537
- expect ( mockAdapter . addClass )
538
- . toHaveBeenCalledWith ( CssClasses . HIDE_TRANSITION ) ;
539
- expect ( mockAdapter . removeClass ) . toHaveBeenCalledWith ( CssClasses . SHOWN ) ;
540
- expect ( mockAdapter . removeClass )
541
- . toHaveBeenCalledWith ( CssClasses . SHOWING_TRANSITION ) ;
542
- expect ( mockAdapter . deregisterDocumentEventHandler )
543
- . toHaveBeenCalledWith ( 'click' , jasmine . any ( Function ) ) ;
544
- expect ( mockAdapter . deregisterDocumentEventHandler )
545
- . toHaveBeenCalledWith ( 'keydown' , jasmine . any ( Function ) ) ;
566
+ expectHideToBeCalled ( foundation , mockAdapter ) ;
546
567
} ) ;
547
568
548
569
it ( '#handleDocumentClick hides the tooltip immediately for persistent rich tooltips if event target is not within anchorElement' ,
@@ -562,21 +583,7 @@ describe('MDCTooltipFoundation', () => {
562
583
foundation . show ( ) ;
563
584
foundation . handleDocumentClick ( mockClickEvent ) ;
564
585
565
- expect ( foundation . hideTimeout ) . toEqual ( null ) ;
566
- expect ( mockAdapter . setAttribute )
567
- . toHaveBeenCalledWith ( 'aria-hidden' , 'true' ) ;
568
- expect ( mockAdapter . setAnchorAttribute )
569
- . toHaveBeenCalledWith ( 'aria-expanded' , 'false' ) ;
570
- expect ( mockAdapter . addClass ) . toHaveBeenCalledWith ( CssClasses . HIDE ) ;
571
- expect ( mockAdapter . addClass )
572
- . toHaveBeenCalledWith ( CssClasses . HIDE_TRANSITION ) ;
573
- expect ( mockAdapter . removeClass ) . toHaveBeenCalledWith ( CssClasses . SHOWN ) ;
574
- expect ( mockAdapter . removeClass )
575
- . toHaveBeenCalledWith ( CssClasses . SHOWING_TRANSITION ) ;
576
- expect ( mockAdapter . deregisterDocumentEventHandler )
577
- . toHaveBeenCalledWith ( 'click' , jasmine . any ( Function ) ) ;
578
- expect ( mockAdapter . deregisterDocumentEventHandler )
579
- . toHaveBeenCalledWith ( 'keydown' , jasmine . any ( Function ) ) ;
586
+ expectHideToBeCalled ( foundation , mockAdapter ) ;
580
587
} ) ;
581
588
582
589
it ( '#handleDocumentClick does not hide the tooltip for persistent rich tooltips if event target is within anchorElement' ,
@@ -624,29 +631,15 @@ describe('MDCTooltipFoundation', () => {
624
631
expect ( foundation . hideTimeout ) . not . toEqual ( null ) ;
625
632
626
633
jasmine . clock ( ) . tick ( numbers . HIDE_DELAY_MS ) ;
627
- expect ( mockAdapter . addClass ) . toHaveBeenCalledWith ( CssClasses . HIDE ) ;
628
- expect ( mockAdapter . addClass )
629
- . toHaveBeenCalledWith ( CssClasses . HIDE_TRANSITION ) ;
630
- expect ( mockAdapter . removeClass ) . toHaveBeenCalledWith ( CssClasses . SHOWN ) ;
631
- expect ( mockAdapter . removeClass )
632
- . toHaveBeenCalledWith ( CssClasses . SHOWING_TRANSITION ) ;
633
- expect ( foundation . hideTimeout ) . toEqual ( null ) ;
634
+ expectHideToBeCalled ( foundation , mockAdapter ) ;
634
635
} ) ;
635
636
636
637
it ( `#handleAnchorBlur hides the tooltip immediately` , ( ) => {
637
638
const { foundation, mockAdapter} = setUpFoundationTest ( MDCTooltipFoundation ) ;
638
639
foundation . show ( ) ;
639
640
foundation . handleAnchorBlur ( ) ;
640
641
641
- expect ( foundation . hideTimeout ) . toEqual ( null ) ;
642
- expect ( mockAdapter . addClass ) . toHaveBeenCalledWith ( CssClasses . HIDE ) ;
643
- expect ( mockAdapter . addClass )
644
- . toHaveBeenCalledWith ( CssClasses . HIDE_TRANSITION ) ;
645
- expect ( mockAdapter . removeClass ) . toHaveBeenCalledWith ( CssClasses . SHOWN ) ;
646
- expect ( mockAdapter . removeClass )
647
- . toHaveBeenCalledWith ( CssClasses . SHOWING_TRANSITION ) ;
648
-
649
- expect ( foundation . hideTimeout ) . toEqual ( null ) ;
642
+ expectHideToBeCalled ( foundation , mockAdapter ) ;
650
643
} ) ;
651
644
652
645
it ( `#handleDocumentClick hides the tooltip immediately` , ( ) => {
@@ -656,14 +649,7 @@ describe('MDCTooltipFoundation', () => {
656
649
foundation . show ( ) ;
657
650
foundation . handleDocumentClick ( mockClickEvent ) ;
658
651
659
- expect ( foundation . hideTimeout ) . toEqual ( null ) ;
660
- expect ( mockAdapter . addClass ) . toHaveBeenCalledWith ( CssClasses . HIDE ) ;
661
- expect ( mockAdapter . addClass )
662
- . toHaveBeenCalledWith ( CssClasses . HIDE_TRANSITION ) ;
663
- expect ( mockAdapter . removeClass ) . toHaveBeenCalledWith ( CssClasses . SHOWN ) ;
664
- expect ( mockAdapter . removeClass )
665
- . toHaveBeenCalledWith ( CssClasses . SHOWING_TRANSITION ) ;
666
- expect ( foundation . hideTimeout ) . toEqual ( null ) ;
652
+ expectHideToBeCalled ( foundation , mockAdapter ) ;
667
653
} ) ;
668
654
669
655
it ( `#handleAnchorMouseEnter shows the tooltip after a ${
@@ -675,11 +661,7 @@ describe('MDCTooltipFoundation', () => {
675
661
expect ( foundation . showTimeout ) . not . toEqual ( null ) ;
676
662
677
663
jasmine . clock ( ) . tick ( numbers . SHOW_DELAY_MS ) ;
678
- expect ( mockAdapter . setAttribute )
679
- . toHaveBeenCalledWith ( 'aria-hidden' , 'false' ) ;
680
- expect ( mockAdapter . removeClass ) . toHaveBeenCalledWith ( CssClasses . HIDE ) ;
681
- expect ( mockAdapter . addClass ) . toHaveBeenCalledWith ( CssClasses . SHOWING ) ;
682
- expect ( foundation . showTimeout ) . toEqual ( null ) ;
664
+ expectShowToBeCalled ( foundation , mockAdapter ) ;
683
665
} ) ;
684
666
685
667
it ( `#handleAnchorFocus shows the tooltip after a ${
@@ -691,11 +673,7 @@ describe('MDCTooltipFoundation', () => {
691
673
expect ( foundation . showTimeout ) . not . toEqual ( null ) ;
692
674
693
675
jasmine . clock ( ) . tick ( numbers . SHOW_DELAY_MS ) ;
694
- expect ( mockAdapter . setAttribute )
695
- . toHaveBeenCalledWith ( 'aria-hidden' , 'false' ) ;
696
- expect ( mockAdapter . removeClass ) . toHaveBeenCalledWith ( CssClasses . HIDE ) ;
697
- expect ( mockAdapter . addClass ) . toHaveBeenCalledWith ( CssClasses . SHOWING ) ;
698
- expect ( foundation . showTimeout ) . toEqual ( null ) ;
676
+ expectShowToBeCalled ( foundation , mockAdapter ) ;
699
677
} ) ;
700
678
701
679
it ( `#handleAnchorClick shows the tooltip immediately when tooltip is hidden` ,
@@ -708,13 +686,7 @@ describe('MDCTooltipFoundation', () => {
708
686
expect ( foundation . isShown ) . toBe ( false ) ;
709
687
foundation . handleAnchorClick ( ) ;
710
688
711
- expect ( foundation . showTimeout ) . toEqual ( null ) ;
712
- expect ( mockAdapter . setAnchorAttribute )
713
- . toHaveBeenCalledWith ( 'aria-expanded' , 'true' ) ;
714
- expect ( mockAdapter . setAttribute )
715
- . toHaveBeenCalledWith ( 'aria-hidden' , 'false' ) ;
716
- expect ( mockAdapter . removeClass ) . toHaveBeenCalledWith ( CssClasses . HIDE ) ;
717
- expect ( mockAdapter . addClass ) . toHaveBeenCalledWith ( CssClasses . SHOWING ) ;
689
+ expectShowToBeCalled ( foundation , mockAdapter ) ;
718
690
} ) ;
719
691
720
692
it ( `#handleAnchorClick hides the tooltip immediately when tooltip is shown` ,
@@ -727,13 +699,7 @@ describe('MDCTooltipFoundation', () => {
727
699
foundation . show ( ) ;
728
700
foundation . handleAnchorClick ( ) ;
729
701
730
- expect ( foundation . hideTimeout ) . toEqual ( null ) ;
731
- expect ( mockAdapter . addClass ) . toHaveBeenCalledWith ( CssClasses . HIDE ) ;
732
- expect ( mockAdapter . addClass )
733
- . toHaveBeenCalledWith ( CssClasses . HIDE_TRANSITION ) ;
734
- expect ( mockAdapter . removeClass ) . toHaveBeenCalledWith ( CssClasses . SHOWN ) ;
735
- expect ( mockAdapter . removeClass )
736
- . toHaveBeenCalledWith ( CssClasses . SHOWING_TRANSITION ) ;
702
+ expectHideToBeCalled ( foundation , mockAdapter ) ;
737
703
} ) ;
738
704
739
705
@@ -744,13 +710,7 @@ describe('MDCTooltipFoundation', () => {
744
710
745
711
foundation . handleRichTooltipMouseEnter ( ) ;
746
712
747
- expect ( foundation . showTimeout ) . toEqual ( null ) ;
748
- expect ( mockAdapter . setAnchorAttribute )
749
- . toHaveBeenCalledWith ( 'aria-expanded' , 'true' ) ;
750
- expect ( mockAdapter . setAttribute )
751
- . toHaveBeenCalledWith ( 'aria-hidden' , 'false' ) ;
752
- expect ( mockAdapter . removeClass ) . toHaveBeenCalledWith ( CssClasses . HIDE ) ;
753
- expect ( mockAdapter . addClass ) . toHaveBeenCalledWith ( CssClasses . SHOWING ) ;
713
+ expectShowToBeCalled ( foundation , mockAdapter ) ;
754
714
} ) ;
755
715
756
716
it ( `#handleRichTooltipMouseLeave hides the tooltip after a ${
@@ -766,13 +726,7 @@ describe('MDCTooltipFoundation', () => {
766
726
expect ( foundation . hideTimeout ) . not . toEqual ( null ) ;
767
727
jasmine . clock ( ) . tick ( numbers . HIDE_DELAY_MS ) ;
768
728
769
- expect ( mockAdapter . addClass ) . toHaveBeenCalledWith ( CssClasses . HIDE ) ;
770
- expect ( mockAdapter . addClass )
771
- . toHaveBeenCalledWith ( CssClasses . HIDE_TRANSITION ) ;
772
- expect ( mockAdapter . removeClass ) . toHaveBeenCalledWith ( CssClasses . SHOWN ) ;
773
- expect ( mockAdapter . removeClass )
774
- . toHaveBeenCalledWith ( CssClasses . SHOWING_TRANSITION ) ;
775
- expect ( foundation . hideTimeout ) . toEqual ( null ) ;
729
+ expectHideToBeCalled ( foundation , mockAdapter ) ;
776
730
} ) ;
777
731
778
732
it ( '#handleRichTooltipMouseLeave clears any pending showTimeout' , ( ) => {
0 commit comments