penpot/frontend/playwright/ui/specs/variants.spec.js
Andrey Antukh 52750d14de
♻️ Reduce the frontend e2e suite by merging equivalent specs (#13094)
* ♻️ Merge duplicated e2e loads and harden ten spec groups

Collapse multi-case Playwright specs into one test with a named step per
case, so the app or document loads once per group instead of once per
case. No case is dropped. In scope, the affected cases fall from 85 to
52 tests and from about 1300 to 760 worker-seconds.

- stroke-to-path and toolbar (15 to 2 tests, -80%): each step anchors a
  layer row to its exact name, checks the count grows by one and that the
  clicked row is selected before the context menu. The skipped path case
  now uses the tool's click-per-node and Enter gesture. The curve step
  counts exact Path rows; the update-file override that returned a whole
  file is gone; the toolbar test runs on the DOM renderer because the
  prebuilt WASM bundle creates no text.
- dimension tokens and colorpicker (12 to 6, -45%): steps assert the
  resolved pill value, not just the pill; the width/height step detaches
  the extra pill. HSB now awaits isVisible on real labels, gradient steps
  count stops and the radial case switches its own type.
- export frames and multiselection (11 to 3, -70%): rows use aria-checked
  instead of a 500 ms sleep, and steps wait for the sidebar before editing
  so a stale render cannot write to the previous selection. The frames
  entry is checked by row count and the "file" item by id.
- variants and workspace (41 to 37, -29%): waits now follow rendered
  state, the duplicated setupEmptyFile is gone, and the layer tree derives
  its children container from the row's own id. The copy/paste flake is
  fixed with a selectRow retry, Escape to deselect, hover instead of
  clicks and idempotent container expansion.
- grid editor and sidebar scroll (6 to 4, -24%): the editor opens once and
  blind 200/500 ms waits are gone; hover checks computed opacity because
  the button is hidden with opacity: 0. Only the second scroll test stays;
  the store has nine unit tests in ui/sidebar_scroll_test.cljs.

Also drop a dead binding, restore two Taiga links and fix comments that
cited the wrong drawing.cljs ranges and cancel-drawing's docstring.

AI-assisted-by: deepseek-v4.1-flash

* ♻️ Close the e2e merge review findings

Restore the renderer coverage the merge lost and make the merged steps
measure their own work.

The five shape tools run under WASM again, and the text tool runs there
too with the v2 editor flag: WasmWorkspacePage already enables it, so
the toolbar gesture keeps its drag and typing assertions. Curve now
compares the exact Path rows with their own baseline.

WorkspacePage gains layerRows, and layerRow delegates to it; toolbar,
stroke-to-path and export-frames use them instead of local copies. The
variants setup waits for the component icon and the variant rows
instead of two fixed sleeps, selectRow clicks once, and the cut/paste
step asserts its own delta. The stale comments lose their line numbers,
Path is no longer called the pencil, the orphan fixture is deleted, and
the sidebar smoke regains the Assets round trip.

Document the local e2e bundle prerequisite in the frontend testing
memory.

Closes #13093

AI-assisted-by: deepseek-v4.1-flash

* 🐛 Wait for the layer selection in the token spec

The dimension-token test clicked a layer row and went back to the Tokens
tab without proving the selection. A lost click left the previous shape
selected, and the border-radius step could pass on that shape. The
selection now goes through WorkspacePage.selectRow, which clicks once and
waits for aria-checked.

selectRow moves from variants.spec.js into the page object, and
selectLayer delegates to it, so the click-and-wait selection lives in one
place.

AI-assisted-by: deepseek-v4.1-flash
2026-11-20 18:55:19 +01:00

510 lines
17 KiB
JavaScript

import { test, expect } from "@playwright/test";
import { WasmWorkspacePage } from "../pages/WasmWorkspacePage";
import { BaseWebSocketPage } from "../pages/BaseWebSocketPage";
import { Clipboard } from "../../helpers/Clipboard";
test.beforeEach(async ({ page, context }) => {
await Clipboard.enable(context, Clipboard.Permission.ALL);
await WasmWorkspacePage.init(page);
await WasmWorkspacePage.mockConfigFlags(page, ["enable-feature-variants"]);
});
test.afterEach(async ({ context }) => {
await context.clearPermissions();
});
// One `setupEmptyFile` only: a second call registered `get-team?id=*` again and,
// because the route added last wins, it cancelled the variants team fixture
// that used to sit between the two calls. These tests have always run with the
// default team, so that fixture is gone and the effective mocks stay the same.
const setupVariantsFile = async (workspacePage) => {
await workspacePage.setupEmptyFile();
await workspacePage.mockRPC(
/get\-file\?/,
"workspace/get-file-not-empty.json",
);
await workspacePage.mockRPC(
"update-file?id=*",
"workspace/update-file-create-rect.json",
);
await workspacePage.goToWorkspace({
fileId: "6191cd35-bb1f-81f7-8004-7cc63d087374",
pageId: "6191cd35-bb1f-81f7-8004-7cc63d087375",
});
};
// A shape is only a component once its layer row shows the component icon; this
// is the condition the fixed three seconds used to stand in for.
const waitForComponentIcon = async (workspacePage, name) => {
await expect(
workspacePage.layers
.getByTestId("layer-row")
.filter({ hasText: name })
.getByTestId("icon-component"),
).toBeVisible();
};
const setupVariantsFileWithVariant = async (workspacePage) => {
await setupVariantsFile(workspacePage);
await workspacePage.selectLayer("Rectangle");
await workspacePage.page.keyboard.press("ControlOrMeta+k");
await waitForComponentIcon(workspacePage, "Rectangle");
await workspacePage.page.keyboard.press("ControlOrMeta+k");
// The second shortcut turns the main component into a variant container and
// its children into variants; wait for those rows instead of a fixed delay.
const variant = await findVariant(workspacePage, 0);
await expect(variant.variant1).toBeVisible();
await expect(variant.variant2).toBeVisible();
};
const findVariant = async (workspacePage, index) => {
const container = workspacePage.layers
.getByTestId("layer-row")
.filter({ hasText: "Rectangle" })
.filter({ has: workspacePage.page.getByTestId("icon-component") })
.nth(index);
const variant1 = workspacePage.layers
.getByTestId("layer-row")
.filter({ hasText: "Value 1" })
.filter({ has: workspacePage.page.getByTestId("icon-variant") })
.nth(index);
const variant2 = workspacePage.layers
.getByTestId("layer-row")
.filter({ hasText: "Value 2" })
.filter({ has: workspacePage.page.getByTestId("icon-variant") })
.nth(index);
await container.waitFor();
return {
container: container,
variant1: variant1,
variant2: variant2,
};
};
const findVariantNoWait = (workspacePage, index) => {
const container = workspacePage.layers
.getByTestId("layer-row")
.filter({ hasText: "Rectangle" })
.filter({ has: workspacePage.page.getByTestId("icon-component") })
.nth(index);
const variant1 = workspacePage.layers
.getByTestId("layer-row")
.filter({ hasText: "Value 1" })
.nth(index);
const variant2 = workspacePage.layers
.getByTestId("layer-row")
.filter({ hasText: "Value 2" })
.nth(index);
return {
container: container,
variant1: variant1,
variant2: variant2,
};
};
// A container keeps its children rows in the tree only while it is expanded, and
// clicking the toggle of an already expanded container collapses it: a duplicate
// or a paste can inherit the expanded state, so check it before clicking.
const expandContainer = async (container) => {
const toggle = container.getByTestId("toggle-content");
if ((await toggle.getAttribute("aria-expanded")) !== "true") {
await toggle.click();
}
await expect(toggle).toHaveAttribute("aria-expanded", "true");
};
const validateVariant = async (variant) => {
//The variant container exists and is visible
await expect(variant.container).toBeVisible();
//The variants exists and are visible
await expect(variant.variant1).toBeVisible();
await expect(variant.variant2).toBeVisible();
// variant1 and variant2 are items inside the childs of variant_container
const parent_id = "children-" + (await variant.container.getAttribute("id"));
await expect(variant.variant1.locator("xpath=..")).toHaveAttribute(
"data-testid",
parent_id,
);
await expect(variant.variant2.locator("xpath=..")).toHaveAttribute(
"data-testid",
parent_id,
);
};
test("User creates a variant", async ({ page }) => {
const workspacePage = new WasmWorkspacePage(page);
await setupVariantsFileWithVariant(workspacePage);
await workspacePage.clickLeafLayer("Rectangle");
const variant = await findVariant(workspacePage, 0);
// The variant is valid
await validateVariant(variant);
// Extra validators
await workspacePage.selectRow(variant.container);
const variants = await workspacePage.layers
.getByTestId("layer-row")
.filter({ has: workspacePage.page.getByTestId("icon-variant") })
.all();
// There are exactly two variants
expect(variants.length).toBe(2);
// The design tab shows the variant properties
await expect(
workspacePage.page.getByTitle("Property 1: Value 1, Value 2"),
).toBeVisible();
});
test("User duplicates a variant container", async ({ page }) => {
const workspacePage = new WasmWorkspacePage(page);
await setupVariantsFileWithVariant(workspacePage);
const variant = await findVariant(workspacePage, 0);
// Select the variant container; Ctrl+D duplicates whatever is selected.
await workspacePage.selectRow(variant.container);
//Duplicate the variant container
await workspacePage.page.keyboard.press("ControlOrMeta+d");
// Assert the second container appears before building the locators, so a slow
// duplication fails here with a count, not with a long wait inside a helper.
const containerRows = workspacePage.layers
.getByTestId("layer-row")
.filter({ hasText: "Rectangle" })
.filter({ has: page.getByTestId("icon-component") });
await expect(containerRows).toHaveCount(2);
// The copy is inserted above the original: index 0 is the duplicate and index
// 1 is the original.
const variant_original = await findVariant(workspacePage, 1);
const variant_duplicate = await findVariant(workspacePage, 0);
// Expand the layers; the original may have lost its expansion in the meantime.
await expandContainer(variant_duplicate.container);
await expandContainer(variant_original.container);
// The variants are valid
await validateVariant(variant_original);
await validateVariant(variant_duplicate);
});
test("User copy paste a variant container", async ({ page }) => {
const workspacePage = new WasmWorkspacePage(page);
// Access to the read/write clipboard necesary for this functionality
await setupVariantsFileWithVariant(workspacePage);
await workspacePage.mockRPC(
/create-file-object-thumbnail.*/,
"workspace/create-file-object-thumbnail.json",
);
const variant = findVariantNoWait(workspacePage, 0);
// Copy the variant container itself. `clickLeafLayer` matches the text as a
// substring and clicks the first match, and this tree also holds the shapes
// named Rectangle inside the variants, so it can select the wrong one.
await workspacePage.selectRow(variant.container);
await workspacePage.copy("keyboard");
// Deselect and place the pointer without clicking: a click here could select a
// shape and send the paste inside it.
await page.keyboard.press("Escape");
await workspacePage.viewport.hover({ position: { x: 400, y: 400 } });
await workspacePage.paste("keyboard");
const variants = workspacePage.layers.getByText("Rectangle");
await expect(variants).toHaveCount(2);
// As with the duplicate, the pasted container is inserted above the original.
const variantDuplicate = findVariantNoWait(workspacePage, 0);
const variantOriginal = findVariantNoWait(workspacePage, 1);
// Expand the layers
await variantDuplicate.container.waitFor();
await expandContainer(variantDuplicate.container);
await validateVariant(variantOriginal);
await validateVariant(variantDuplicate);
});
test("User cut paste a variant container", async ({ page }) => {
const workspacePage = new WasmWorkspacePage(page);
await setupVariantsFileWithVariant(workspacePage);
const variant = await findVariant(workspacePage, 0);
// Select the variant container; Ctrl+X cuts whatever is selected.
await workspacePage.selectRow(variant.container);
//Cut the variant container
await workspacePage.cut("keyboard");
// Deselect and place the pointer in empty canvas without clicking, so the paste
// cannot land inside a shape that the click selected.
await page.keyboard.press("Escape");
await workspacePage.viewport.hover({ position: { x: 500, y: 500 } });
await workspacePage.paste("keyboard");
const variantPasted = await findVariant(workspacePage, 0);
// Expand the layers
await expandContainer(variantPasted.container);
// The variants are valid
await validateVariant(variantPasted);
});
test("User cut paste a variant container into a board, and undo twice", async ({
page,
}) => {
const workspacePage = new WasmWorkspacePage(page);
await setupVariantsFileWithVariant(workspacePage);
const variant = await findVariant(workspacePage, 0);
//Create a board
await workspacePage.boardButton.click();
// NOTE: this board should not intersect the existing variants, otherwise
// this test is flaky
await workspacePage.clickWithDragViewportAt(200, 200, 100, 100);
await workspacePage.clickAt(495, 495);
const board = await workspacePage.rootShape.locator("Board");
// Select the variant container
// await variant.container.click();
await workspacePage.clickLeafLayer("Rectangle");
//Cut the variant container
await workspacePage.cut("keyboard");
await expect(variant.container).not.toBeVisible();
//Select the board
await workspacePage.clickLeafLayer("Board");
//Paste the variant container inside the board
await workspacePage.paste("keyboard");
await expect(variant.container).toBeVisible();
//Undo twice
await workspacePage.page.keyboard.press("ControlOrMeta+z");
await expect(variant.container).not.toBeVisible();
await workspacePage.page.keyboard.press("ControlOrMeta+z");
await expect(variant.container).toBeVisible();
const variantAfterUndo = await findVariant(workspacePage, 0);
// The variants are valid
await validateVariant(variantAfterUndo);
});
test("User copy paste a variant", async ({ page }) => {
const workspacePage = new WasmWorkspacePage(page);
await setupVariantsFileWithVariant(workspacePage);
const variant = await findVariant(workspacePage, 0);
// Select the variant1
await workspacePage.selectRow(variant.variant1);
// Copy the variant
await workspacePage.copy("keyboard");
// Deselect and place the pointer in empty canvas, for the same reason as above.
await page.keyboard.press("Escape");
await workspacePage.viewport.hover({ position: { x: 500, y: 500 } });
await workspacePage.paste("keyboard");
const copy = await workspacePage.layers
.getByTestId("layer-row")
.filter({ has: workspacePage.page.getByText("Rectangle") })
.filter({ has: workspacePage.page.getByTestId("icon-component-copy") });
//The copy exists and is visible
await expect(copy).toBeVisible();
});
test("User cut paste a variant outside the container", async ({ page }) => {
const workspacePage = new WasmWorkspacePage(page);
await setupVariantsFileWithVariant(workspacePage);
const variant = await findVariant(workspacePage, 0);
// Select the variant1
await workspacePage.selectRow(variant.variant1);
//Cut the variant
await workspacePage.cut("keyboard");
// Deselect and place the pointer in empty canvas, so the paste does not land
// inside a shape that the click could have selected.
await page.keyboard.press("Escape");
await workspacePage.viewport.hover({ position: { x: 500, y: 500 } });
await workspacePage.paste("keyboard");
const component = await workspacePage.layers
.getByTestId("layer-row")
.filter({ has: workspacePage.page.getByText("Rectangle / Value 1") })
.filter({ has: workspacePage.page.getByTestId("icon-component") });
//The component exists and is visible
await expect(component).toBeVisible();
});
test("User drag and drop a variant outside the container", async ({ page }) => {
const workspacePage = new WasmWorkspacePage(page);
await setupVariantsFileWithVariant(workspacePage);
const variant = await findVariant(workspacePage, 0);
// Drag and drop the variant
// FIXME: to make this test more resilient, we should get the bounding box of the Value 1 variant
// and use it to calculate the target position
await workspacePage.clickWithDragViewportAt(600, 500, 0, 300);
await expect(
workspacePage.layers.getByText("Rectangle / Value 1"),
).toBeVisible();
});
test("User cut paste a component inside a variant", async ({ page }) => {
const workspacePage = new WasmWorkspacePage(page);
await setupVariantsFileWithVariant(workspacePage);
const variant = await findVariant(workspacePage, 0);
//Create a component
await workspacePage.selectToolFromFlyout(workspacePage, {
triggerToolName: "Rectangle (R)",
targetToolName: "Ellipse (E)",
});
await workspacePage.clickWithDragViewportAt(500, 500, 20, 20);
await workspacePage.clickLeafLayer("Ellipse");
await workspacePage.page.keyboard.press("ControlOrMeta+k");
//Cut the component
await workspacePage.cut("keyboard");
//Paste the component inside the variant
await workspacePage.selectRow(variant.container);
await workspacePage.paste("keyboard");
const variant3 = await workspacePage.layers
.getByTestId("layer-row")
.filter({ has: workspacePage.page.getByText("Ellipse") })
.filter({ has: workspacePage.page.getByTestId("icon-variant") })
.first();
//The new variant exists and is visible
await expect(variant3).toBeVisible();
});
test("User takes a component with a path into a variant", async ({ page }) => {
const workspacePage = new WasmWorkspacePage(page);
await setupVariantsFileWithVariant(workspacePage);
const variant = await findVariant(workspacePage, 0);
const variantNamed = (name) =>
workspacePage.layers
.getByTestId("layer-row")
.filter({ has: workspacePage.page.getByText(name) })
.filter({ has: workspacePage.page.getByTestId("icon-variant") });
const createNamedComponent = async () => {
await workspacePage.selectToolFromFlyout(workspacePage, {
triggerToolName: "Rectangle (R)",
targetToolName: "Ellipse (E)",
});
await workspacePage.clickWithDragViewportAt(500, 500, 20, 20);
await workspacePage.clickLeafLayer("Ellipse");
await workspacePage.page.keyboard.press("ControlOrMeta+k");
await waitForComponentIcon(workspacePage, "Ellipse");
await workspacePage.layers.getByText("Ellipse").dblclick();
await workspacePage.page
.getByTestId("layer-item")
.getByRole("textbox")
.pressSequentially("button / hover");
await workspacePage.page.keyboard.press("Enter");
await expect(
workspacePage.layers.getByText("button / hover"),
).toBeVisible();
};
// The drag moves the component away from where it was created, so the second
// one can be drawn at the same coordinates. The row count is what proves each
// step put its own component into the container.
await test.step("drag and drop", async () => {
await createNamedComponent();
await workspacePage.clickWithDragViewportAt(510, 510, 200, 0);
await expect(variantNamed("button, hover")).toHaveCount(1);
});
await test.step("cut and paste", async () => {
const before = await variantNamed("button, hover").count();
await createNamedComponent();
await workspacePage.cut("keyboard");
await workspacePage.selectRow(variant.container);
await workspacePage.paste("keyboard");
await expect(variantNamed("button, hover")).toHaveCount(before + 1);
});
});
test("User cut paste a variant into another container", async ({ page }) => {
const workspacePage = new WasmWorkspacePage(page);
await setupVariantsFileWithVariant(workspacePage);
// Create anothe variant
await workspacePage.selectToolFromFlyout(workspacePage, {
triggerToolName: "Rectangle (R)",
targetToolName: "Ellipse (E)",
});
await workspacePage.clickWithDragViewportAt(500, 500, 20, 20);
await workspacePage.clickLeafLayer("Ellipse");
await workspacePage.page.keyboard.press("ControlOrMeta+k");
await workspacePage.page.keyboard.press("ControlOrMeta+k");
const variantOrigin = await findVariantNoWait(workspacePage, 1);
// Select the variant1
await variantOrigin.variant1.waitFor();
await variantOrigin.variant1.click();
await variantOrigin.variant1.click();
//Cut the variant
await workspacePage.cut("keyboard");
//Paste the variant
await workspacePage.layers.getByText("Ellipse").first().click();
await workspacePage.paste("keyboard");
const variant3 = workspacePage.layers
.getByTestId("layer-row")
.filter({ has: workspacePage.page.getByText("Value 1, rectangle") })
.filter({ has: workspacePage.page.getByTestId("icon-variant") })
.first();
//The new variant exists and is visible
await expect(variant3).toBeVisible();
});