Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tooltip): prevent hide when focus is within tooltip #1811

Merged
merged 9 commits into from
Oct 30, 2024
Prev Previous commit
Next Next commit
Add tests
  • Loading branch information
dancormier committed Oct 29, 2024
commit 2a6c2f50e725b2bf4ce7b054b029b1eb4876aa27
59 changes: 46 additions & 13 deletions lib/components/popover/tooltip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,42 @@ describe("tooltip", () => {
expect(tooltip).to.be.visible;
});

// TODO write this test so it works
it.skip("should not hide the tooltip if focus is moved to the tooltip", async () => {
const tooltip = await fixture(html`
it("should continue to show the tooltip if focus is moved to an element within the tooltip", async () => {
await fixture(html`
<button
class="s-btn s-btn__filled"
role="button"
aria-describedby="tooltip-example"
data-controller="s-tooltip"
>
Hover tooltip popover
</button>
<div
class="s-popover s-popover__tooltip"
id="tooltip-example"
role="tooltip"
data-testid="tooltip"
>
<div class="s-popover--arrow"></div>
<div class="s-popover--content">
<a href="#">View more</a>
<a href="#" data-testid="link">View more</a>
</div>
</div>
`);

const trigger = await fixture(html`

const tooltip = screen.getByTestId("tooltip");
const link = screen.getByTestId("link");

await user.tab();
await new Promise(resolve => setTimeout(resolve, 300)); // wait for the tooltip to appear
await user.tab(); // tab to nested link within tooltip
expect(link).to.have.focus; // link within tooltip is focused
expect(tooltip).to.be.visible; // link within tooltip is focused, tooltip is visible
});

it("should hide the tooltip if focus is moved to an element outside the tooltip and trigger", async () => {
await fixture(html`
<button
class="s-btn s-btn__filled"
role="button"
Expand All @@ -84,16 +104,29 @@ describe("tooltip", () => {
>
Hover tooltip popover
</button>
<div
class="s-popover s-popover__tooltip"
id="tooltip-example"
role="tooltip"
data-testid="tooltip"
>
<div class="s-popover--arrow"></div>
<div class="s-popover--content">
<a href="#">View more</a>
</div>
</div>
<button data-testid="neutral-btn">Another button</button>
`);

const tooltip = screen.getByTestId("tooltip");
const neutralBtn = screen.getByTestId("neutral-btn");

await user.tab();
expect(tooltip).to.be.visible;
await expect(document.activeElement).to.equal(trigger);

const link = screen.getByRole("link");
await expect(document.activeElement).to.be.equal(link);
await user.tab();

expect(tooltip).not.to.be.visible;
await new Promise(resolve => setTimeout(resolve, 300)); // wait for the tooltip to appear
await user.tab(); // tab to nested link within tooltip
await user.tab(); // tab to button after tooltip
expect(neutralBtn).to.have.focus; // button after tooltip is focused
await new Promise(resolve => setTimeout(resolve, 300)); // wait for the tooltip to hide
expect(tooltip).not.to.have.class("is-visible"); // check that .is-visible class is removed
});
});
Loading