|
| 1 | +import { expect, test } from "@playwright/test"; |
| 2 | +import { bubble, focusedLabel, settled } from "./helpers"; |
| 3 | + |
| 4 | +// The open/close lifecycle: entering and docking, expanding into the row, |
| 5 | +// switching panels, collapsing, and programmatic activate(). |
| 6 | + |
| 7 | +test.beforeEach(async ({ page }) => { |
| 8 | + await page.goto("/"); |
| 9 | + await page.waitForFunction(() => !!window.bubbles); |
| 10 | +}); |
| 11 | + |
| 12 | +const state = (page: import("@playwright/test").Page) => |
| 13 | + page.evaluate(() => window.bubbles.state()); |
| 14 | +const active = (page: import("@playwright/test").Page) => |
| 15 | + page.evaluate(() => window.bubbles.active()); |
| 16 | + |
| 17 | +test("the first bubble flies in and docks against the edge", async ({ page }) => { |
| 18 | + await page.evaluate(() => { |
| 19 | + window.bubbles.create(); |
| 20 | + window.bubbles.add({ id: "a", label: "A" }); |
| 21 | + }); |
| 22 | + |
| 23 | + const a = bubble(page, "A"); |
| 24 | + await expect(a).toBeVisible(); |
| 25 | + await settled(page); |
| 26 | + |
| 27 | + // Default side is "right": it rests in the right half, near the edge. |
| 28 | + const box = await a.boundingBox(); |
| 29 | + const vp = page.viewportSize(); |
| 30 | + expect(box && vp).toBeTruthy(); |
| 31 | + expect(box!.x).toBeGreaterThan(vp!.width / 2); |
| 32 | + expect(box!.x + box!.width).toBeGreaterThan(vp!.width - 24); |
| 33 | +}); |
| 34 | + |
| 35 | +test("tap expands the stack and shows the panel; tapping the active bubble collapses", async ({ |
| 36 | + page |
| 37 | +}) => { |
| 38 | + await page.evaluate(() => { |
| 39 | + window.bubbles.create(); |
| 40 | + window.bubbles.add({ id: "a", label: "A", panelText: "Panel A" }); |
| 41 | + }); |
| 42 | + await settled(page); |
| 43 | + |
| 44 | + await bubble(page, "A").click(); |
| 45 | + await expect.poll(() => state(page)).toBe("open"); |
| 46 | + await expect(page.locator('[data-panel-content="a"]')).toBeVisible(); |
| 47 | + |
| 48 | + await bubble(page, "A").click(); |
| 49 | + await expect.poll(() => state(page)).toBe("docked"); |
| 50 | +}); |
| 51 | + |
| 52 | +test("tapping another bubble switches the active panel", async ({ page }) => { |
| 53 | + await page.evaluate(() => { |
| 54 | + window.bubbles.create(); |
| 55 | + window.bubbles.add({ id: "a", label: "A", panelText: "Panel A" }); |
| 56 | + window.bubbles.add({ id: "b", label: "B", panelText: "Panel B" }); |
| 57 | + window.bubbles.activate("a"); |
| 58 | + }); |
| 59 | + await expect.poll(() => active(page)).toBe("a"); |
| 60 | + await expect(page.locator('[data-panel-content="a"]')).toBeVisible(); |
| 61 | + |
| 62 | + await bubble(page, "B").click(); |
| 63 | + await expect.poll(() => active(page)).toBe("b"); |
| 64 | + await expect(page.locator('[data-panel-content="b"]')).toBeVisible(); |
| 65 | +}); |
| 66 | + |
| 67 | +test("Escape collapses the group and returns focus to the stack", async ({ page }) => { |
| 68 | + await page.evaluate(() => { |
| 69 | + window.bubbles.create(); |
| 70 | + window.bubbles.add({ id: "a", label: "A", panelText: "Panel A" }); |
| 71 | + window.bubbles.activate("a"); |
| 72 | + }); |
| 73 | + await expect.poll(() => state(page)).toBe("open"); |
| 74 | + await settled(page); |
| 75 | + |
| 76 | + await page.keyboard.press("Escape"); |
| 77 | + await expect.poll(() => state(page)).toBe("docked"); |
| 78 | + expect(await focusedLabel(page)).toBe("A"); |
| 79 | +}); |
| 80 | + |
| 81 | +test("activate() expands a docked group onto the chosen bubble", async ({ page }) => { |
| 82 | + await page.evaluate(() => { |
| 83 | + window.bubbles.create(); |
| 84 | + window.bubbles.add({ id: "a", label: "A", panelText: "A" }); |
| 85 | + window.bubbles.add({ id: "b", label: "B", panelText: "B" }); |
| 86 | + }); |
| 87 | + await settled(page); |
| 88 | + expect(await state(page)).toBe("docked"); |
| 89 | + |
| 90 | + await page.evaluate(() => window.bubbles.activate("a")); |
| 91 | + await expect.poll(() => state(page)).toBe("open"); |
| 92 | + await expect.poll(() => active(page)).toBe("a"); |
| 93 | + await expect(page.locator('[data-panel-content="a"]')).toBeVisible(); |
| 94 | +}); |
| 95 | + |
| 96 | +test("with reduced motion, expand and tap-away still reach their end states", async ({ page }) => { |
| 97 | + await page.emulateMedia({ reducedMotion: "reduce" }); |
| 98 | + // The library reads the preference live per animation, so confirm it took |
| 99 | + // — otherwise this would pass without exercising the reduced-motion paths. |
| 100 | + expect(await page.evaluate(() => matchMedia("(prefers-reduced-motion: reduce)").matches)).toBe( |
| 101 | + true |
| 102 | + ); |
| 103 | + |
| 104 | + await page.evaluate(() => { |
| 105 | + window.bubbles.create(); |
| 106 | + window.bubbles.add({ id: "a", label: "A", panelText: "Panel A" }); |
| 107 | + window.bubbles.activate("a"); |
| 108 | + }); |
| 109 | + await expect.poll(() => state(page)).toBe("open"); |
| 110 | + await expect(page.locator('[data-panel-content="a"]')).toBeVisible(); |
| 111 | + |
| 112 | + await page.locator("#outside").click(); |
| 113 | + await expect.poll(() => state(page)).toBe("docked"); |
| 114 | +}); |
0 commit comments