|
1 | 1 | import { expect, story } from "../../stories/playwright/story" |
2 | 2 |
|
3 | | -story("opens a neighbouring tooltip without the open delay", async ({ mount, page }) => { |
| 3 | +story.beforeEach(async ({ page }) => { |
| 4 | + await page.clock.install({ time: new Date("2026-01-01T00:00:00Z") }) |
| 5 | + await page.clock.pauseAt(new Date("2026-01-01T00:00:01Z")) |
| 6 | +}) |
| 7 | + |
| 8 | +story("cancels a pending hover on click and opens again on re-entry", async ({ mount, page }) => { |
| 9 | + const root = await mount("ui-tooltip--with-button") |
| 10 | + |
| 11 | + const trigger = root.getByRole("button", { name: "Add" }) |
| 12 | + |
| 13 | + const tooltip = page.getByRole("tooltip", { name: "Add one to the count" }) |
| 14 | + |
| 15 | + await trigger.hover() |
| 16 | + await page.clock.runFor(300) |
| 17 | + await trigger.click() |
| 18 | + await expect(root.getByRole("status")).toHaveText("Count: 1") |
| 19 | + await expect(tooltip).toBeHidden() |
| 20 | + await page.clock.runFor(600) |
| 21 | + await expect(tooltip).toBeHidden() |
| 22 | + |
| 23 | + await page.mouse.move(0, 0) |
| 24 | + await trigger.hover() |
| 25 | + await page.clock.runFor(600) |
| 26 | + await expect(tooltip).toBeVisible() |
| 27 | +}) |
| 28 | + |
| 29 | +story("dismisses an open tooltip on pointerdown before the button activates", async ({ mount, page }) => { |
| 30 | + const root = await mount("ui-tooltip--with-button") |
| 31 | + |
| 32 | + const tooltip = page.getByRole("tooltip", { name: "Add one to the count" }) |
| 33 | + |
| 34 | + await root.getByRole("button", { name: "Add" }).hover() |
| 35 | + await page.clock.runFor(600) |
| 36 | + await expect(tooltip).toBeVisible() |
| 37 | + await page.mouse.down() |
| 38 | + await expect(tooltip).toBeHidden() |
| 39 | + await expect(root.getByRole("status")).toHaveText("Count: 0") |
| 40 | + await page.mouse.up() |
| 41 | + await expect(root.getByRole("status")).toHaveText("Count: 1") |
| 42 | + await page.clock.runFor(600) |
| 43 | + await expect(tooltip).toBeHidden() |
| 44 | +}) |
| 45 | + |
| 46 | +story("shows the wrapped button's tooltip on Tab and Shift+Tab, even after a click", async ({ mount, page }) => { |
| 47 | + const root = await mount("ui-tooltip--with-button") |
| 48 | + |
| 49 | + const trigger = root.getByRole("button", { name: "Add" }) |
| 50 | + |
| 51 | + const tooltip = page.getByRole("tooltip", { name: "Add one to the count" }) |
| 52 | + |
| 53 | + await page.keyboard.press("Tab") |
| 54 | + await expect(trigger).toBeFocused() |
| 55 | + await expect(tooltip).toBeVisible() |
| 56 | + |
| 57 | + await trigger.click() |
| 58 | + await expect(tooltip).toBeHidden() |
| 59 | + await page.keyboard.press("Tab") |
| 60 | + await expect(root.getByRole("button", { name: "Reset" })).toBeFocused() |
| 61 | + await page.keyboard.press("Shift+Tab") |
| 62 | + await expect(trigger).toBeFocused() |
| 63 | + await expect(tooltip).toBeVisible() |
| 64 | + await page.keyboard.press("Escape") |
| 65 | + await expect(tooltip).toBeHidden() |
| 66 | + await expect(trigger).toBeFocused() |
| 67 | +}) |
| 68 | + |
| 69 | +story("restored focus does not open a tooltip after Tab leaves the window", async ({ mount, page }) => { |
| 70 | + const root = await mount("ui-tooltip--with-button") |
| 71 | + |
| 72 | + await page.keyboard.down("Tab") |
| 73 | + await expect(root.getByRole("button", { name: "Add" })).toBeFocused() |
| 74 | + // The page loses focus before receiving Tab's keyup. |
| 75 | + await page.evaluate(() => window.dispatchEvent(new FocusEvent("blur"))) |
| 76 | + await root.getByRole("button", { name: "Reset" }).focus() |
| 77 | + await expect(page.getByRole("tooltip", { name: "Reset the count" })).toBeHidden() |
| 78 | + await page.keyboard.up("Tab") |
| 79 | +}) |
| 80 | + |
| 81 | +story.describe("touch", () => { |
| 82 | + story.use({ hasTouch: true, isMobile: true, viewport: { width: 390, height: 844 } }) |
| 83 | + |
| 84 | + story("tapping the button preserves the tooltip's hover state", async ({ mount, page }) => { |
| 85 | + const root = await mount("ui-tooltip--with-button") |
| 86 | + |
| 87 | + const trigger = root.getByRole("button", { name: "Add" }) |
| 88 | + |
| 89 | + const tooltip = page.getByRole("tooltip", { name: "Add one to the count" }) |
| 90 | + |
| 91 | + expect(await page.evaluate(() => matchMedia("(hover: none)").matches)).toBe(true) |
| 92 | + await trigger.tap() |
| 93 | + await expect(root.getByRole("status")).toHaveText("Count: 1") |
| 94 | + await expect(tooltip).toBeHidden() |
| 95 | + await page.clock.runFor(600) |
| 96 | + await expect(tooltip).toBeHidden() |
| 97 | + |
| 98 | + await trigger.hover() |
| 99 | + await page.clock.runFor(600) |
| 100 | + await expect(tooltip).toBeVisible() |
| 101 | + await trigger.tap() |
| 102 | + await expect(root.getByRole("status")).toHaveText("Count: 2") |
| 103 | + await expect(tooltip).toBeVisible() |
| 104 | + }) |
| 105 | +}) |
| 106 | + |
| 107 | +story("waits 600 ms on first hover and opens a neighbouring tooltip immediately", async ({ mount, page }) => { |
4 | 108 | const root = await mount("ui-tooltip--appearances") |
| 109 | + |
| 110 | + const tooltip = page.getByRole("tooltip", { name: "Standard tooltip" }) |
| 111 | + |
5 | 112 | await root.getByText("Standard", { exact: true }).hover() |
6 | | - await expect(page.getByRole("tooltip", { name: "Standard tooltip" })).toBeVisible() |
| 113 | + await page.clock.runFor(599) |
| 114 | + await expect(tooltip).toBeHidden() |
| 115 | + await page.clock.runFor(1) |
| 116 | + await expect(tooltip).toBeVisible() |
7 | 117 | // With timers frozen, the next tooltip can only open through the skip-delay window that closing this one starts. |
8 | | - await page.clock.install({ time: new Date("2026-01-01T00:00:00Z") }) |
9 | | - await page.clock.pauseAt(new Date("2026-01-01T00:00:01Z")) |
10 | 118 | await root.getByText("Compact", { exact: true }).hover() |
11 | 119 | await expect(page.getByRole("tooltip", { name: "Compact tooltip" })).toBeVisible() |
12 | 120 | }) |
13 | 121 |
|
14 | 122 | story("keeps a menu trigger's tooltip hidden when the menu closes under a still pointer", async ({ mount, page }) => { |
15 | 123 | const root = await mount("ui-tooltip--with-menu") |
16 | | - await page.clock.install() |
| 124 | + |
17 | 125 | const trigger = root.getByRole("button", { name: "Actions" }) |
| 126 | + |
18 | 127 | const tooltip = page.getByRole("tooltip", { name: "More actions" }) |
| 128 | + |
19 | 129 | await trigger.hover() |
| 130 | + await page.clock.runFor(600) |
20 | 131 | await expect(tooltip).toBeVisible() |
21 | 132 | await trigger.click() |
22 | 133 | await expect(page.getByRole("menu")).toBeVisible() |
23 | 134 | await expect(tooltip).toBeHidden() |
24 | 135 | await page.keyboard.press("Escape") |
25 | 136 | await expect(page.getByRole("menu")).toBeHidden() |
| 137 | + // The menu restores focus in a zero-delay task after unmounting. |
| 138 | + await page.clock.runFor(0) |
26 | 139 | await expect(trigger).toBeFocused() |
27 | | - // The menu's closing layer hands the still pointer a fresh pointerenter, which would open the tooltip 400 ms later. |
28 | | - await page.clock.runFor(1000) |
| 140 | + // The menu's closing layer can hand the still pointer a fresh pointerenter. |
| 141 | + await page.clock.runFor(600) |
29 | 142 | await expect(tooltip).toBeHidden() |
30 | 143 | await page.mouse.move(0, 0) |
31 | 144 | await trigger.hover() |
| 145 | + await page.clock.runFor(600) |
32 | 146 | await expect(tooltip).toBeVisible() |
33 | 147 | }) |
34 | 148 |
|
35 | 149 | story("opens a menu trigger's tooltip on the next hover after the menu closes", async ({ mount, page }) => { |
36 | 150 | const root = await mount("ui-tooltip--with-menu") |
| 151 | + |
37 | 152 | const trigger = root.getByRole("button", { name: "Actions" }) |
| 153 | + |
38 | 154 | const tooltip = page.getByRole("tooltip", { name: "More actions" }) |
| 155 | + |
39 | 156 | await trigger.hover() |
| 157 | + await page.clock.runFor(600) |
40 | 158 | await expect(tooltip).toBeVisible() |
41 | 159 | await trigger.click() |
42 | 160 | await expect(page.getByRole("menu")).toBeVisible() |
43 | 161 | await page.mouse.move(0, 0) |
44 | 162 | await page.keyboard.press("Escape") |
45 | 163 | await expect(page.getByRole("menu")).toBeHidden() |
| 164 | + await page.clock.runFor(0) |
46 | 165 | await expect(trigger).toBeFocused() |
47 | 166 | await trigger.hover() |
| 167 | + await page.clock.runFor(600) |
48 | 168 | await expect(tooltip).toBeVisible() |
49 | 169 | }) |
0 commit comments