|
| 1 | +import { expect, Locator, test } from "@playwright/test"; |
| 2 | +import { getUrl } from "../src/utils"; |
| 3 | + |
| 4 | +// Regression test for #13444. The code copy button sits in the top-right |
| 5 | +// corner of the code block, where the annotation gutter shows the marker of |
| 6 | +// an annotated first line. The button must stay clear of the gutter so the |
| 7 | +// marker can still be hovered (or clicked) to show its annotation. |
| 8 | + |
| 9 | +const rightEdge = async (locator: Locator) => { |
| 10 | + const box = await locator.boundingBox(); |
| 11 | + expect(box).not.toBeNull(); |
| 12 | + return box!.x + box!.width; |
| 13 | +}; |
| 14 | + |
| 15 | +const testCases = [ |
| 16 | + { name: "hover", url: "html/code-annotation-copy-button.html" }, |
| 17 | + { name: "select", url: "html/code-annotation-copy-button-select.html" }, |
| 18 | + { |
| 19 | + name: "hover with a custom code block font size", |
| 20 | + url: "html/code-annotation-copy-button-font-size.html", |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "hover with a code block font size in percent", |
| 24 | + url: "html/code-annotation-copy-button-font-size-percent.html", |
| 25 | + }, |
| 26 | +]; |
| 27 | + |
| 28 | +test.describe("code copy button and code annotation marker", () => { |
| 29 | + for (const { name, url } of testCases) { |
| 30 | + test(`${name} — copy button does not cover the marker`, async ({ page }) => { |
| 31 | + await page.goto(getUrl(url), { waitUntil: "load" }); |
| 32 | + |
| 33 | + const scaffold = page.locator(".code-copy-outer-scaffold").filter({ |
| 34 | + has: page.locator("pre.code-annotated"), |
| 35 | + }); |
| 36 | + const anchor = scaffold.locator("button.code-annotation-anchor"); |
| 37 | + await expect(anchor).toHaveCount(1); |
| 38 | + const box = await anchor.boundingBox(); |
| 39 | + expect(box).not.toBeNull(); |
| 40 | + const x = box!.x + box!.width / 2; |
| 41 | + const y = box!.y + box!.height / 2; |
| 42 | + |
| 43 | + // Hovering the code block reveals the copy button, so check the marker |
| 44 | + // is still the top-most element at its own center while the button is |
| 45 | + // shown. |
| 46 | + await page.mouse.move(x, y); |
| 47 | + const hit = await page.evaluate( |
| 48 | + ([px, py]) => |
| 49 | + document.elementFromPoint(px, py)?.closest( |
| 50 | + ".code-annotation-anchor, .code-copy-button", |
| 51 | + )?.className ?? null, |
| 52 | + [x, y], |
| 53 | + ); |
| 54 | + expect(hit).toContain("code-annotation-anchor"); |
| 55 | + |
| 56 | + // The button sits the default 5px left of the gutter, whatever the code |
| 57 | + // block font size the gutter width is derived from. |
| 58 | + const gutterLeft = (await scaffold.locator(".code-annotation-gutter") |
| 59 | + .boundingBox())!.x; |
| 60 | + const buttonRight = await rightEdge( |
| 61 | + scaffold.locator(".code-copy-button"), |
| 62 | + ); |
| 63 | + expect(Math.abs(gutterLeft - buttonRight - 5)).toBeLessThanOrEqual(1); |
| 64 | + |
| 65 | + if (name === "select") { |
| 66 | + await page.mouse.click(x, y); |
| 67 | + } |
| 68 | + await expect(page.locator(".code-annotation-tip-content")).toBeVisible(); |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + test("copy button of a block without annotations is unchanged", async ({ |
| 73 | + page, |
| 74 | + }) => { |
| 75 | + await page.goto(getUrl("html/code-annotation-copy-button.html"), { |
| 76 | + waitUntil: "load", |
| 77 | + }); |
| 78 | + |
| 79 | + const scaffold = page.locator(".code-copy-outer-scaffold").filter({ |
| 80 | + hasNot: page.locator("pre.code-annotated"), |
| 81 | + }); |
| 82 | + await expect(scaffold).toHaveCount(1); |
| 83 | + const button = scaffold.locator(".code-copy-button"); |
| 84 | + await expect(button).toHaveCSS("right", "0px"); |
| 85 | + await expect(button).toHaveCSS("margin-right", "5px"); |
| 86 | + expect((await rightEdge(scaffold)) - (await rightEdge(button))) |
| 87 | + .toBeCloseTo(5, 0); |
| 88 | + }); |
| 89 | +}); |
0 commit comments