Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit c7edfc7

Browse files
Shi Shucopybara-github
authored andcommitted
refactor(tooltip): Refactors the tooltip foundation tests for checking whether or not show and hide have been called.
PiperOrigin-RevId: 345222681
1 parent 9775856 commit c7edfc7

File tree

1 file changed

+89
-135
lines changed

1 file changed

+89
-135
lines changed

packages/mdc-tooltip/test/foundation.test.ts

Lines changed: 89 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import {createMouseEvent, emitEvent} from '../../../testing/dom/events';
2525
import {verifyDefaultAdapter} from '../../../testing/helpers/foundation';
2626
import {setUpFoundationTest, setUpMdcTestEnvironment} from '../../../testing/helpers/setup';
27+
import {MDCTooltipAdapter} from '../adapter';
2728
import {AnchorBoundaryType, attributes, CssClasses, numbers, XPosition, YPosition} from '../constants';
2829
import {MDCTooltipFoundation} from '../foundation';
2930

@@ -32,6 +33,80 @@ const ESC_EVENTS = [
3233
{type: 'keydown', keyCode: 27, target: {}},
3334
];
3435

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+
35110
describe('MDCTooltipFoundation', () => {
36111
setUpMdcTestEnvironment();
37112

@@ -439,19 +514,7 @@ describe('MDCTooltipFoundation', () => {
439514
foundation.show();
440515
foundation.handleDocumentClick(mockClickEvent);
441516

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);
455518
});
456519

457520
it('#handleDocumentClick hides the tooltip immediately for default rich tooltips',
@@ -465,21 +528,7 @@ describe('MDCTooltipFoundation', () => {
465528
foundation.show();
466529
foundation.handleDocumentClick(mockClickEvent);
467530

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);
483532
});
484533

485534
it('#handleDocumentClick hides the tooltip immediately for persistent rich tooltips if there is no event target',
@@ -495,21 +544,7 @@ describe('MDCTooltipFoundation', () => {
495544
foundation.show();
496545
foundation.handleDocumentClick(mockClickEvent);
497546

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);
513548
});
514549

515550
it('#handleDocumentClick hides the tooltip immediately for persistent rich tooltips if event target is not HTMLElement',
@@ -528,21 +563,7 @@ describe('MDCTooltipFoundation', () => {
528563
foundation.show();
529564
foundation.handleDocumentClick(mockClickEvent);
530565

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);
546567
});
547568

548569
it('#handleDocumentClick hides the tooltip immediately for persistent rich tooltips if event target is not within anchorElement',
@@ -562,21 +583,7 @@ describe('MDCTooltipFoundation', () => {
562583
foundation.show();
563584
foundation.handleDocumentClick(mockClickEvent);
564585

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);
580587
});
581588

582589
it('#handleDocumentClick does not hide the tooltip for persistent rich tooltips if event target is within anchorElement',
@@ -624,29 +631,15 @@ describe('MDCTooltipFoundation', () => {
624631
expect(foundation.hideTimeout).not.toEqual(null);
625632

626633
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);
634635
});
635636

636637
it(`#handleAnchorBlur hides the tooltip immediately`, () => {
637638
const {foundation, mockAdapter} = setUpFoundationTest(MDCTooltipFoundation);
638639
foundation.show();
639640
foundation.handleAnchorBlur();
640641

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);
650643
});
651644

652645
it(`#handleDocumentClick hides the tooltip immediately`, () => {
@@ -656,14 +649,7 @@ describe('MDCTooltipFoundation', () => {
656649
foundation.show();
657650
foundation.handleDocumentClick(mockClickEvent);
658651

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);
667653
});
668654

669655
it(`#handleAnchorMouseEnter shows the tooltip after a ${
@@ -675,11 +661,7 @@ describe('MDCTooltipFoundation', () => {
675661
expect(foundation.showTimeout).not.toEqual(null);
676662

677663
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);
683665
});
684666

685667
it(`#handleAnchorFocus shows the tooltip after a ${
@@ -691,11 +673,7 @@ describe('MDCTooltipFoundation', () => {
691673
expect(foundation.showTimeout).not.toEqual(null);
692674

693675
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);
699677
});
700678

701679
it(`#handleAnchorClick shows the tooltip immediately when tooltip is hidden`,
@@ -708,13 +686,7 @@ describe('MDCTooltipFoundation', () => {
708686
expect(foundation.isShown).toBe(false);
709687
foundation.handleAnchorClick();
710688

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);
718690
});
719691

720692
it(`#handleAnchorClick hides the tooltip immediately when tooltip is shown`,
@@ -727,13 +699,7 @@ describe('MDCTooltipFoundation', () => {
727699
foundation.show();
728700
foundation.handleAnchorClick();
729701

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);
737703
});
738704

739705

@@ -744,13 +710,7 @@ describe('MDCTooltipFoundation', () => {
744710

745711
foundation.handleRichTooltipMouseEnter();
746712

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);
754714
});
755715

756716
it(`#handleRichTooltipMouseLeave hides the tooltip after a ${
@@ -766,13 +726,7 @@ describe('MDCTooltipFoundation', () => {
766726
expect(foundation.hideTimeout).not.toEqual(null);
767727
jasmine.clock().tick(numbers.HIDE_DELAY_MS);
768728

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);
776730
});
777731

778732
it('#handleRichTooltipMouseLeave clears any pending showTimeout', () => {

0 commit comments

Comments
 (0)