Skip to content

Commit

Permalink
fix: prevent duplicate warn up when opening same tooltip twice (#8135)
Browse files Browse the repository at this point in the history
* fix: prevent duplicate warn up when opening same tooltip twice

* test: reset global state in afterEach to make tests reliable

* fix: also clear timeout references when resetting global state

* test: change the tooltip opened check back to false

* refactor: simplify tooltip warm up condition logic
  • Loading branch information
web-padawan authored Nov 12, 2024
1 parent e609352 commit d5df9f5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/tooltip/src/vaadin-tooltip-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export function resetGlobalTooltipState() {
warmedUp = false;
clearTimeout(warmUpTimeout);
clearTimeout(cooldownTimeout);
warmUpTimeout = null;
cooldownTimeout = null;
closing.clear();
}

Expand Down Expand Up @@ -145,12 +147,12 @@ class TooltipStateController {
/** @private */
__warmupTooltip(isFocus) {
if (!this._isOpened()) {
// First tooltip is opened, warm up.
if (!warmedUp) {
this.__scheduleWarmUp(isFocus);
} else {
// Warmed up, show another tooltip.
if (warmedUp) {
// Warmed up, show the tooltip.
this.__showTooltip();
} else if (warmUpTimeout == null) {
// Ensure there is no duplicate warm up.
this.__scheduleWarmUp(isFocus);
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions packages/tooltip/test/tooltip-timers.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,10 @@ describe('timers', () => {
controller = tooltip._stateController;
});

afterEach(() => {
resetGlobalTooltipState();
});

it('should not clear opened state on the tooltip when closing scheduled twice', async () => {
controller.open({ hover: true });
await aTimeout(2);
Expand All @@ -737,5 +741,16 @@ describe('timers', () => {
await aTimeout(2);
expect(spy).to.not.be.called;
});

it('should not keep opened state after closing when open() is called twice', async () => {
// Emulate opening on different targets when reusing tooltip instance
controller.open({ hover: true });
controller.open({ hover: true });
await aTimeout(1);
// Close during warm up timeout and then wait for it to finish
controller.close();
await aTimeout(1);
expect(tooltip.opened).to.be.false;
});
});
});

0 comments on commit d5df9f5

Please sign in to comment.