|
| 1 | +import type { Page } from "@playwright/test" |
| 2 | +import { expect, sourceURL, story } from "../../stories/playwright/story" |
| 3 | + |
| 4 | +const fixture = sourceURL(new URL("./tab-strip-widths.fixture.tsx", import.meta.url)) |
| 5 | + |
| 6 | +const long = (name: string) => `a-file-name-long-enough-to-truncate-${name}.md` |
| 7 | + |
| 8 | +story.beforeEach(async ({ mount }) => { |
| 9 | + // Any story loads the app's styles; the fixture mounts the strip beside it. |
| 10 | + await mount("ui-line-comment--editor") |
| 11 | +}) |
| 12 | + |
| 13 | +async function mountStrip(page: Page, input: { titles: string[]; width: number; dir?: "ltr" | "rtl" }) { |
| 14 | + await page.evaluate( |
| 15 | + async ({ fixture, input }) => { |
| 16 | + const { mountTabStrip } = await import(fixture) |
| 17 | + mountTabStrip({ dir: "ltr", ...input }) |
| 18 | + await document.fonts.ready |
| 19 | + }, |
| 20 | + { fixture, input }, |
| 21 | + ) |
| 22 | + await expect(strip(page).locator("[data-pane-tab]")).toHaveCount(input.titles.length) |
| 23 | +} |
| 24 | + |
| 25 | +const strip = (page: Page) => page.getByTestId("tab-strip-fixture").locator('[data-slot="tabs-list"]') |
| 26 | + |
| 27 | +const closeButton = (page: Page, title: string) => |
| 28 | + strip(page).locator(`[data-pane-tab="${title}"] [data-slot="tabs-trigger-close-button"]`) |
| 29 | + |
| 30 | +async function center(page: Page, title: string) { |
| 31 | + const box = (await closeButton(page, title).boundingBox())! |
| 32 | + |
| 33 | + return { x: box.x + box.width / 2, y: box.y + box.height / 2 } |
| 34 | +} |
| 35 | + |
| 36 | +// The tab whose close button is at the point. |
| 37 | +const under = (page: Page, point: { x: number; y: number }) => |
| 38 | + page.evaluate( |
| 39 | + (point) => |
| 40 | + document |
| 41 | + .elementFromPoint(point.x, point.y) |
| 42 | + ?.closest('[data-slot="tabs-trigger-close-button"]') |
| 43 | + ?.closest<HTMLElement>("[data-pane-tab]")?.dataset.paneTab, |
| 44 | + point, |
| 45 | + ) |
| 46 | + |
| 47 | +const hold = (page: Page) => strip(page).evaluate((el) => el.style.paddingInlineEnd) |
| 48 | + |
| 49 | +const tabWidth = async (page: Page, title: string) => |
| 50 | + (await strip(page).locator(`[data-pane-tab="${title}"]`).boundingBox())!.width |
| 51 | + |
| 52 | +story( |
| 53 | + "a mouse close keeps the next close button under the pointer until the pointer leaves the strip", |
| 54 | + async ({ page }) => { |
| 55 | + const titles = [long("alpha"), "a.md", long("beta"), long("gamma"), "b.md", long("delta")] |
| 56 | + await mountStrip(page, { titles, width: 900 }) |
| 57 | + const width = await tabWidth(page, long("gamma")) |
| 58 | + |
| 59 | + const middle = await center(page, long("beta")) |
| 60 | + await page.mouse.click(middle.x, middle.y) |
| 61 | + await expect(strip(page).locator(`[data-pane-tab="${long("beta")}"]`)).toHaveCount(0) |
| 62 | + await expect.poll(() => under(page, middle)).toBe(long("gamma")) |
| 63 | + |
| 64 | + // The short tab before the last one keeps its width, so the long ones fill the closed tab's place. |
| 65 | + const last = await center(page, long("delta")) |
| 66 | + await page.mouse.click(last.x, last.y) |
| 67 | + await expect(strip(page).locator(`[data-pane-tab="${long("delta")}"]`)).toHaveCount(0) |
| 68 | + await expect.poll(() => under(page, last)).toBe("b.md") |
| 69 | + |
| 70 | + // The "+" stays a gap after the last tab, and the held strip shows no end fade. |
| 71 | + const gap = await strip(page).evaluate((el) => { |
| 72 | + const plus = el.lastElementChild!.getBoundingClientRect() |
| 73 | + const tab = el.querySelector('[data-pane-tab="b.md"]')!.getBoundingClientRect() |
| 74 | + |
| 75 | + return Math.round(plus.left - tab.right) |
| 76 | + }) |
| 77 | + |
| 78 | + expect(gap).toBe(8) |
| 79 | + expect(await strip(page).evaluate((el) => el.style.getPropertyValue("--tab-strip-fade-end"))).toBe("0px") |
| 80 | + |
| 81 | + await page.mouse.move(middle.x, middle.y + 200) |
| 82 | + await expect.poll(() => hold(page)).toBe("") |
| 83 | + await expect.poll(() => tabWidth(page, long("gamma"))).toBeGreaterThan(width + 1) |
| 84 | + }, |
| 85 | +) |
| 86 | + |
| 87 | +story("a middle click close holds the strip, and resizing the strip releases it", async ({ page }) => { |
| 88 | + const titles = [long("alpha"), long("beta"), long("gamma"), long("delta")] |
| 89 | + await mountStrip(page, { titles, width: 700 }) |
| 90 | + const width = await tabWidth(page, long("gamma")) |
| 91 | + |
| 92 | + await strip(page) |
| 93 | + .locator(`[data-pane-tab="${long("beta")}"] [role="tab"]`) |
| 94 | + .click({ button: "middle" }) |
| 95 | + await expect(strip(page).locator(`[data-pane-tab="${long("beta")}"]`)).toHaveCount(0) |
| 96 | + await expect.poll(() => tabWidth(page, long("gamma"))).toBeCloseTo(width, 0) |
| 97 | + |
| 98 | + // Held, the three tabs would shrink with the strip; released, they share the whole of it. |
| 99 | + await page.evaluate(async (fixture) => { |
| 100 | + const { setTabStripWidth } = await import(fixture) |
| 101 | + setTabStripWidth(650) |
| 102 | + }, fixture) |
| 103 | + await expect.poll(() => tabWidth(page, long("gamma"))).toBeGreaterThan(width + 1) |
| 104 | + expect(await hold(page)).toBe("") |
| 105 | +}) |
| 106 | + |
| 107 | +story("closing from the keyboard, or a middle click that closes nothing, resizes the tabs", async ({ page }) => { |
| 108 | + const titles = [long("alpha"), "pinned.md", long("beta"), long("gamma")] |
| 109 | + await mountStrip(page, { titles, width: 700 }) |
| 110 | + const width = await tabWidth(page, long("gamma")) |
| 111 | + |
| 112 | + // The middle click on a tab without a middle-click close arms nothing for the keyboard close that follows. |
| 113 | + await strip(page).locator('[data-pane-tab="pinned.md"] [role="tab"]').click({ button: "middle" }) |
| 114 | + await closeButton(page, "pinned.md").getByRole("button").focus() |
| 115 | + await page.keyboard.press("Enter") |
| 116 | + await expect(strip(page).locator('[data-pane-tab="pinned.md"]')).toHaveCount(0) |
| 117 | + await expect.poll(() => tabWidth(page, long("gamma"))).toBeGreaterThan(width + 1) |
| 118 | + |
| 119 | + const next = await tabWidth(page, long("gamma")) |
| 120 | + await closeButton(page, long("alpha")).getByRole("button").focus() |
| 121 | + await page.keyboard.press("Enter") |
| 122 | + await expect(strip(page).locator(`[data-pane-tab="${long("alpha")}"]`)).toHaveCount(0) |
| 123 | + await expect.poll(() => tabWidth(page, long("gamma"))).toBeGreaterThan(next + 1) |
| 124 | +}) |
| 125 | + |
| 126 | +story("in a right-to-left strip the next close button slides under the pointer", async ({ page }) => { |
| 127 | + const titles = [long("alpha"), long("beta"), long("gamma"), long("delta")] |
| 128 | + await mountStrip(page, { titles, width: 700, dir: "rtl" }) |
| 129 | + |
| 130 | + const middle = await center(page, long("beta")) |
| 131 | + await page.mouse.click(middle.x, middle.y) |
| 132 | + await expect(strip(page).locator(`[data-pane-tab="${long("beta")}"]`)).toHaveCount(0) |
| 133 | + await expect.poll(() => under(page, middle)).toBe(long("gamma")) |
| 134 | +}) |
| 135 | + |
| 136 | +story( |
| 137 | + "an overflowing strip is never held, so closing its last tab keeps the other tabs at their width", |
| 138 | + async ({ page }) => { |
| 139 | + const titles = Array.from({ length: 8 }, (_, index) => long(String(index))) |
| 140 | + await mountStrip(page, { titles, width: 400 }) |
| 141 | + expect(await strip(page).evaluate((el) => el.scrollWidth > el.clientWidth)).toBe(true) |
| 142 | + const width = await tabWidth(page, titles[6]) |
| 143 | + |
| 144 | + await strip(page).evaluate((el) => (el.scrollLeft = el.scrollWidth)) |
| 145 | + const last = await center(page, titles[7]) |
| 146 | + await page.mouse.click(last.x, last.y) |
| 147 | + await expect(strip(page).locator("[data-pane-tab]")).toHaveCount(7) |
| 148 | + expect(await hold(page)).toBe("") |
| 149 | + expect(await tabWidth(page, titles[6])).toBeCloseTo(width, 0) |
| 150 | + }, |
| 151 | +) |
| 152 | + |
| 153 | +story.describe("touch", () => { |
| 154 | + story.use({ hasTouch: true }) |
| 155 | + |
| 156 | + story("a tap on a close button never holds the strip", async ({ page }) => { |
| 157 | + const titles = [long("alpha"), long("beta"), long("gamma"), long("delta")] |
| 158 | + await mountStrip(page, { titles, width: 700 }) |
| 159 | + const width = await tabWidth(page, long("gamma")) |
| 160 | + |
| 161 | + await closeButton(page, long("beta")).getByRole("button").tap() |
| 162 | + await expect(strip(page).locator(`[data-pane-tab="${long("beta")}"]`)).toHaveCount(0) |
| 163 | + await expect.poll(() => tabWidth(page, long("gamma"))).toBeGreaterThan(width + 1) |
| 164 | + }) |
| 165 | +}) |
0 commit comments