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

Commit 30fdfd0

Browse files
Shi Shucopybara-github
authored andcommitted
test(tooltip): Change show/hide tooltip unit tests to only check for styling and attribute changes as this is more in line with unit test best practices and focuses on the specific behavior of showing/hiding the tooltip.
PiperOrigin-RevId: 347855420
1 parent eabf9d5 commit 30fdfd0

File tree

1 file changed

+37
-96
lines changed

1 file changed

+37
-96
lines changed

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

Lines changed: 37 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -37,103 +37,53 @@ const ESC_EVENTS = [
3737
const ANIMATION_FRAME = 1;
3838

3939
// This function assumes that the foundation has already been initialized for
40-
// rich tooltips. If isRich and isPersistent have not been initialized, the
41-
// checks for rich tooltip and persistent rich tooltips will not be called. This
42-
// function also assumes that isShown is false, since the checks for isShown
43-
// being true is trivial.
44-
function expectShowToBeCalled(
40+
// interactive rich tooltips. If isRich and isInteractive have not been
41+
// initialized, the checks for interactive rich tooltips will not be called.
42+
function expectTooltipToHaveBeenShown(
4543
foundation: MDCTooltipFoundation,
4644
mockAdapter: jasmine.SpyObj<MDCTooltipAdapter>) {
47-
expect(foundation['hideTimeout']).toEqual(null);
48-
expect(foundation['showTimeout']).toEqual(null);
49-
5045
if (foundation.getIsRich()) {
51-
expect(mockAdapter.registerEventHandler)
52-
.toHaveBeenCalledWith('focusout', jasmine.any(Function));
5346
if (foundation['isInteractive']) {
5447
expect(mockAdapter.setAnchorAttribute)
5548
.toHaveBeenCalledWith('aria-expanded', 'true');
5649
}
57-
if (!foundation.getIsPersistent()) {
58-
expect(mockAdapter.registerEventHandler)
59-
.toHaveBeenCalledWith('mouseenter', jasmine.any(Function));
60-
expect(mockAdapter.registerEventHandler)
61-
.toHaveBeenCalledWith('mouseleave', jasmine.any(Function));
62-
}
6350
}
6451

6552
expect(mockAdapter.setAttribute).toHaveBeenCalledWith('aria-hidden', 'false');
6653
expect(mockAdapter.removeClass).toHaveBeenCalledWith(CssClasses.HIDE);
6754
expect(mockAdapter.addClass).toHaveBeenCalledWith(CssClasses.SHOWING);
68-
69-
expect(mockAdapter.registerDocumentEventHandler)
70-
.toHaveBeenCalledWith('click', jasmine.any(Function));
71-
expect(mockAdapter.registerDocumentEventHandler)
72-
.toHaveBeenCalledWith('keydown', jasmine.any(Function));
73-
74-
expect(mockAdapter.registerWindowEventHandler)
75-
.toHaveBeenCalledWith('scroll', jasmine.any(Function));
76-
expect(mockAdapter.registerWindowEventHandler)
77-
.toHaveBeenCalledWith('resize', jasmine.any(Function));
7855
}
7956

8057
// This function assumes that the foundation has already been initialized for
81-
// rich tooltips. If isRich and isPersistent have not been initialized, the
82-
// checks for rich tooltip and persistent rich tooltips will not be called. This
83-
// function also assumes that isShown is true, since the checks for isShown
84-
// being false is trivial.
85-
function expectHideToBeCalled(
58+
// interactive rich tooltips. If isRich and isInteractive have not been
59+
// initialized, the checks for interactive rich tooltips will not be called.
60+
function expectTooltipToHaveBeenHidden(
8661
foundation: MDCTooltipFoundation,
8762
mockAdapter: jasmine.SpyObj<MDCTooltipAdapter>) {
88-
expect(foundation['hideTimeout']).toEqual(null);
89-
expect(foundation['showTimeout']).toEqual(null);
90-
9163
if (foundation.getIsRich()) {
92-
expect(mockAdapter.deregisterEventHandler)
93-
.toHaveBeenCalledWith('focusout', jasmine.any(Function));
9464
if (foundation['isInteractive']) {
9565
expect(mockAdapter.setAnchorAttribute)
9666
.toHaveBeenCalledWith('aria-expanded', 'false');
9767
}
98-
if (!foundation.getIsPersistent()) {
99-
expect(mockAdapter.deregisterEventHandler)
100-
.toHaveBeenCalledWith('mouseenter', jasmine.any(Function));
101-
expect(mockAdapter.deregisterEventHandler)
102-
.toHaveBeenCalledWith('mouseleave', jasmine.any(Function));
103-
}
10468
}
10569

10670
expect(mockAdapter.setAttribute).toHaveBeenCalledWith('aria-hidden', 'true');
10771
expect(mockAdapter.addClass).toHaveBeenCalledWith(CssClasses.HIDE);
10872
expect(mockAdapter.addClass).toHaveBeenCalledWith(CssClasses.HIDE_TRANSITION);
10973
expect(mockAdapter.removeClass).toHaveBeenCalledWith(CssClasses.SHOWN);
110-
111-
expect(mockAdapter.deregisterDocumentEventHandler)
112-
.toHaveBeenCalledWith('click', jasmine.any(Function));
113-
expect(mockAdapter.deregisterDocumentEventHandler)
114-
.toHaveBeenCalledWith('keydown', jasmine.any(Function));
115-
expect(mockAdapter.deregisterWindowEventHandler)
116-
.toHaveBeenCalledWith('scroll', jasmine.any(Function));
117-
expect(mockAdapter.deregisterWindowEventHandler)
118-
.toHaveBeenCalledWith('resize', jasmine.any(Function));
11974
}
12075

121-
function expectHideNotToBeCalled(
76+
// This function assumes that the foundation has already been initialized for
77+
// interactive rich tooltips. If isRich and isInteractive have not been
78+
// initialized, the checks for interactive rich tooltips will not be called.
79+
function expectTooltipNotToHaveBeenHidden(
12280
foundation: MDCTooltipFoundation,
12381
mockAdapter: jasmine.SpyObj<MDCTooltipAdapter>) {
12482
if (foundation.getIsRich()) {
125-
expect(mockAdapter.deregisterEventHandler)
126-
.not.toHaveBeenCalledWith('focusout', jasmine.any(Function));
12783
if (foundation['isInteractive']) {
12884
expect(mockAdapter.setAnchorAttribute)
12985
.not.toHaveBeenCalledWith('aria-expanded', 'false');
13086
}
131-
if (!foundation.getIsPersistent()) {
132-
expect(mockAdapter.deregisterEventHandler)
133-
.not.toHaveBeenCalledWith('mouseenter', jasmine.any(Function));
134-
expect(mockAdapter.deregisterEventHandler)
135-
.not.toHaveBeenCalledWith('mouseleave', jasmine.any(Function));
136-
}
13787
}
13888

13989
expect(mockAdapter.setAttribute)
@@ -142,15 +92,6 @@ function expectHideNotToBeCalled(
14292
expect(mockAdapter.addClass)
14393
.not.toHaveBeenCalledWith(CssClasses.HIDE_TRANSITION);
14494
expect(mockAdapter.removeClass).not.toHaveBeenCalledWith(CssClasses.SHOWN);
145-
146-
expect(mockAdapter.deregisterDocumentEventHandler)
147-
.not.toHaveBeenCalledWith('click', jasmine.any(Function));
148-
expect(mockAdapter.deregisterDocumentEventHandler)
149-
.not.toHaveBeenCalledWith('keydown', jasmine.any(Function));
150-
expect(mockAdapter.deregisterWindowEventHandler)
151-
.not.toHaveBeenCalledWith('scroll', jasmine.any(Function));
152-
expect(mockAdapter.deregisterWindowEventHandler)
153-
.not.toHaveBeenCalledWith('resize', jasmine.any(Function));
15495
}
15596

15697
function setUpFoundationTestForRichTooltip(
@@ -557,7 +498,7 @@ describe('MDCTooltipFoundation', () => {
557498
foundation.show();
558499
foundation.handleKeydown({type: 'keydown', key: 'Space'});
559500

560-
expectHideNotToBeCalled(foundation, mockAdapter);
501+
expectTooltipNotToHaveBeenHidden(foundation, mockAdapter);
561502
});
562503

563504
it('#handleKeydown does not restore focus to the anchorElement if the activeElement is not a HTMLElement',
@@ -606,7 +547,7 @@ describe('MDCTooltipFoundation', () => {
606547
foundation.show();
607548
foundation.handleDocumentClick(mockClickEvent);
608549

609-
expectHideToBeCalled(foundation, mockAdapter);
550+
expectTooltipToHaveBeenHidden(foundation, mockAdapter);
610551
});
611552

612553
it('#handleDocumentClick hides the tooltip immediately for default rich tooltips',
@@ -618,7 +559,7 @@ describe('MDCTooltipFoundation', () => {
618559
foundation.show();
619560
foundation.handleDocumentClick(mockClickEvent);
620561

621-
expectHideToBeCalled(foundation, mockAdapter);
562+
expectTooltipToHaveBeenHidden(foundation, mockAdapter);
622563
});
623564

624565
it('#handleDocumentClick hides the tooltip immediately for persistent rich tooltips if there is no event target',
@@ -630,7 +571,7 @@ describe('MDCTooltipFoundation', () => {
630571
foundation.show();
631572
foundation.handleDocumentClick(mockClickEvent);
632573

633-
expectHideToBeCalled(foundation, mockAdapter);
574+
expectTooltipToHaveBeenHidden(foundation, mockAdapter);
634575
});
635576

636577
it('#handleDocumentClick hides the tooltip immediately for persistent rich tooltips if event target is not HTMLElement',
@@ -645,7 +586,7 @@ describe('MDCTooltipFoundation', () => {
645586
foundation.show();
646587
foundation.handleDocumentClick(mockClickEvent);
647588

648-
expectHideToBeCalled(foundation, mockAdapter);
589+
expectTooltipToHaveBeenHidden(foundation, mockAdapter);
649590
});
650591

651592
it('#handleDocumentClick hides the tooltip immediately for persistent rich tooltips if event target is not within anchorElement',
@@ -661,7 +602,7 @@ describe('MDCTooltipFoundation', () => {
661602
foundation.show();
662603
foundation.handleDocumentClick(mockClickEvent);
663604

664-
expectHideToBeCalled(foundation, mockAdapter);
605+
expectTooltipToHaveBeenHidden(foundation, mockAdapter);
665606
});
666607

667608
it('#handleDocumentClick does not hide the tooltip for persistent rich tooltips if event target is within anchorElement',
@@ -677,7 +618,7 @@ describe('MDCTooltipFoundation', () => {
677618
foundation.show();
678619
foundation.handleDocumentClick(mockClickEvent);
679620

680-
expectHideNotToBeCalled(foundation, mockAdapter);
621+
expectTooltipNotToHaveBeenHidden(foundation, mockAdapter);
681622
});
682623

683624

@@ -691,7 +632,7 @@ describe('MDCTooltipFoundation', () => {
691632
expect(foundation.hideTimeout).not.toEqual(null);
692633

693634
jasmine.clock().tick(numbers.HIDE_DELAY_MS);
694-
expectHideToBeCalled(foundation, mockAdapter);
635+
expectTooltipToHaveBeenHidden(foundation, mockAdapter);
695636
});
696637

697638
it(`#handleAnchorBlur hides the tooltip immediately for plain tooltips`,
@@ -702,7 +643,7 @@ describe('MDCTooltipFoundation', () => {
702643
foundation.show();
703644
foundation.handleAnchorBlur();
704645

705-
expectHideToBeCalled(foundation, mockAdapter);
646+
expectTooltipToHaveBeenHidden(foundation, mockAdapter);
706647
});
707648

708649
it(`#handleAnchorBlur hides the tooltip immediately when focus changes to non-HTMLElement related target for default rich tooltips`,
@@ -713,7 +654,7 @@ describe('MDCTooltipFoundation', () => {
713654
foundation.show();
714655
foundation.handleAnchorBlur({});
715656

716-
expectHideToBeCalled(foundation, mockAdapter);
657+
expectTooltipToHaveBeenHidden(foundation, mockAdapter);
717658
});
718659

719660
it(`#handleAnchorBlur hides the tooltip immediately when focus changes to related target not within tooltip for default rich tooltips`,
@@ -726,7 +667,7 @@ describe('MDCTooltipFoundation', () => {
726667
foundation.show();
727668
foundation.handleAnchorBlur(mockFocusEvent);
728669

729-
expectHideToBeCalled(foundation, mockAdapter);
670+
expectTooltipToHaveBeenHidden(foundation, mockAdapter);
730671
});
731672

732673
it(`#handleAnchorBlur doesn't hide the tooltip when focus changes to related target not within tooltip for default rich tooltips`,
@@ -739,7 +680,7 @@ describe('MDCTooltipFoundation', () => {
739680
foundation.show();
740681
foundation.handleAnchorBlur(mockFocusEvent);
741682

742-
expectHideNotToBeCalled(foundation, mockAdapter);
683+
expectTooltipNotToHaveBeenHidden(foundation, mockAdapter);
743684
});
744685

745686
it(`#handleAnchorBlur hides the tooltip immediately when focus changes to non-HTMLElement related target for persistent rich tooltips`,
@@ -750,7 +691,7 @@ describe('MDCTooltipFoundation', () => {
750691
foundation.show();
751692
foundation.handleAnchorBlur({});
752693

753-
expectHideToBeCalled(foundation, mockAdapter);
694+
expectTooltipToHaveBeenHidden(foundation, mockAdapter);
754695
});
755696

756697
it(`#handleAnchorBlur hides the tooltip immediately when focus changes to related target not within tooltip for persistent rich tooltips`,
@@ -763,7 +704,7 @@ describe('MDCTooltipFoundation', () => {
763704
foundation.show();
764705
foundation.handleAnchorBlur(mockFocusEvent);
765706

766-
expectHideToBeCalled(foundation, mockAdapter);
707+
expectTooltipToHaveBeenHidden(foundation, mockAdapter);
767708
});
768709

769710
it(`#handleAnchorBlur doesn't hide the tooltip when focus changes to related target within tooltip for persistent rich tooltips`,
@@ -776,7 +717,7 @@ describe('MDCTooltipFoundation', () => {
776717
foundation.show();
777718
foundation.handleAnchorBlur(mockFocusEvent);
778719

779-
expectHideNotToBeCalled(foundation, mockAdapter);
720+
expectTooltipNotToHaveBeenHidden(foundation, mockAdapter);
780721
});
781722

782723
it(`#handleDocumentClick hides the tooltip immediately`, () => {
@@ -786,7 +727,7 @@ describe('MDCTooltipFoundation', () => {
786727
foundation.show();
787728
foundation.handleDocumentClick(mockClickEvent);
788729

789-
expectHideToBeCalled(foundation, mockAdapter);
730+
expectTooltipToHaveBeenHidden(foundation, mockAdapter);
790731
});
791732

792733
it(`#handleAnchorMouseEnter shows the tooltip after a ${
@@ -798,7 +739,7 @@ describe('MDCTooltipFoundation', () => {
798739
expect(foundation.showTimeout).not.toEqual(null);
799740

800741
jasmine.clock().tick(numbers.SHOW_DELAY_MS);
801-
expectShowToBeCalled(foundation, mockAdapter);
742+
expectTooltipToHaveBeenShown(foundation, mockAdapter);
802743
});
803744

804745
it(`#handleAnchorFocus shows the tooltip after a ${
@@ -811,7 +752,7 @@ describe('MDCTooltipFoundation', () => {
811752
expect(foundation.showTimeout).not.toEqual(null);
812753
jasmine.clock().tick(numbers.SHOW_DELAY_MS);
813754

814-
expectShowToBeCalled(foundation, mockAdapter);
755+
expectTooltipToHaveBeenShown(foundation, mockAdapter);
815756
});
816757

817758
it(`#handleAnchorFocus shows the tooltip if the relatedTarget is not within tooltip`,
@@ -825,7 +766,7 @@ describe('MDCTooltipFoundation', () => {
825766
expect(foundation.showTimeout).not.toEqual(null);
826767
jasmine.clock().tick(numbers.SHOW_DELAY_MS);
827768

828-
expectShowToBeCalled(foundation, mockAdapter);
769+
expectTooltipToHaveBeenShown(foundation, mockAdapter);
829770
});
830771

831772
it(`#handleAnchorFocus doesn't show the tooltip if the relatedTarget is within tooltip`,
@@ -855,7 +796,7 @@ describe('MDCTooltipFoundation', () => {
855796
expect(foundation.isShown).toBe(false);
856797
foundation.handleAnchorClick();
857798

858-
expectShowToBeCalled(foundation, mockAdapter);
799+
expectTooltipToHaveBeenShown(foundation, mockAdapter);
859800
});
860801

861802
it(`#handleAnchorClick hides the tooltip immediately when tooltip is shown for persistent rich tooltips`,
@@ -866,7 +807,7 @@ describe('MDCTooltipFoundation', () => {
866807
foundation.show();
867808
foundation.handleAnchorClick();
868809

869-
expectHideToBeCalled(foundation, mockAdapter);
810+
expectTooltipToHaveBeenHidden(foundation, mockAdapter);
870811
});
871812

872813

@@ -876,7 +817,7 @@ describe('MDCTooltipFoundation', () => {
876817

877818
foundation.handleRichTooltipMouseEnter();
878819

879-
expectShowToBeCalled(foundation, mockAdapter);
820+
expectTooltipToHaveBeenShown(foundation, mockAdapter);
880821
});
881822

882823
it(`#handleRichTooltipMouseLeave hides the tooltip after a ${
@@ -890,7 +831,7 @@ describe('MDCTooltipFoundation', () => {
890831
expect(foundation.hideTimeout).not.toEqual(null);
891832
jasmine.clock().tick(numbers.HIDE_DELAY_MS);
892833

893-
expectHideToBeCalled(foundation, mockAdapter);
834+
expectTooltipToHaveBeenHidden(foundation, mockAdapter);
894835
});
895836

896837
it('#handleRichTooltipMouseLeave clears any pending showTimeout', () => {
@@ -917,7 +858,7 @@ describe('MDCTooltipFoundation', () => {
917858
foundation.show();
918859
foundation.handleRichTooltipFocusOut({});
919860

920-
expectHideToBeCalled(foundation, mockAdapter);
861+
expectTooltipToHaveBeenHidden(foundation, mockAdapter);
921862
});
922863

923864
it('#handleRichTooltipFocusOut hides the tooltip immediately if related target is not within the anchor or the tooltip',
@@ -931,7 +872,7 @@ describe('MDCTooltipFoundation', () => {
931872
foundation.handleRichTooltipFocusOut(
932873
{relatedTarget: document.createElement('div')});
933874

934-
expectHideToBeCalled(foundation, mockAdapter);
875+
expectTooltipToHaveBeenHidden(foundation, mockAdapter);
935876
});
936877

937878
it('#handleRichTooltipFocusOut does not hide the tooltip if related target is within the anchor',
@@ -945,7 +886,7 @@ describe('MDCTooltipFoundation', () => {
945886
foundation.handleRichTooltipFocusOut(
946887
{relatedTarget: document.createElement('div')});
947888

948-
expectHideNotToBeCalled(foundation, mockAdapter);
889+
expectTooltipNotToHaveBeenHidden(foundation, mockAdapter);
949890
});
950891

951892
it('#handleRichTooltipFocusOut does not hide the tooltip if related target is within the tooltip',
@@ -959,7 +900,7 @@ describe('MDCTooltipFoundation', () => {
959900
foundation.handleRichTooltipFocusOut(
960901
{relatedTarget: document.createElement('div')});
961902

962-
expectHideNotToBeCalled(foundation, mockAdapter);
903+
expectTooltipNotToHaveBeenHidden(foundation, mockAdapter);
963904
});
964905

965906
it(`does not re-animate a tooltip already shown in the dom (from focus)`,

0 commit comments

Comments
 (0)