Files
editor/e2e/sprite-picker.spec.ts
T
FantasticSPMandFantasticSPM 1a2c3363ce fix: preview sprites for all sprite-backed properties (#2364)
## Description

The sprite picker added in #2359 only replaced the input for
`icon-image`. Maputnik already treats `background-pattern`,
`fill-pattern`, `line-pattern`, `fill-extrusion-pattern`, and
`icon-image` as sprite-backed properties through the shared
`iconProperties` list. The other four properties still rendered the
text-only autocomplete, so their dropdowns could not preview the sprite
atlas even though they use the same sprite catalog in the style spec.

This change routes every property in `iconProperties` through
`InputSprite`. Non-sprite string fields continue to use `InputString`,
and free-form values, search, keyboard selection, and expression editing
keep the behavior introduced in #2359. Having one special case for
`icon-image` and another branch for the remaining sprite properties was
redundant and left the UI inconsistent.

This is a follow-up to #2359 and completes the sprite-preview behavior
related to #1294 for all sprite-backed style properties.

## Visuals

No new visual treatment is introduced. Before this change, the four
pattern fields displayed the text-only autocomplete; after this change
they display the same thumbnail/search/keyboard picker already shown for
`icon-image` in #2359.

## Verification

- Added Playwright coverage for `background-pattern`, `fill-pattern`,
`line-pattern`, and `fill-extrusion-pattern`. Each case opens the
picker, verifies rendered atlas thumbnails, selects a sprite, and
asserts the persisted style value.
- `npm run lint`
- `npm run build`
- `npm run test-unit -- --run` (55 tests passed)
- `npm run test -- e2e/sprite-picker.spec.ts --workers=1` (19 tests
passed)
- Confirmed the new assertion fails when the expected option count is
deliberately mutated, then restored the assertion.
- Local `.npmrc` changes were not included in the commit.

## Launch Checklist

- [x] Briefly describe the changes in this PR.
- [x] Link to related issues.
- [x] Include before/after visuals or gifs if this PR includes visual
changes.
- [x] Write tests for all new functionality.
- [x] Add an entry to `CHANGELOG.md` under the `## main` section.

Co-authored-by: FantasticSPM <asd507172@gmail.com>
2026-11-25 10:43:11 +02:00

167 lines
7.2 KiB
TypeScript

import { describe, beforeEach, test } from "./utils/fixtures";
import { MaputnikDriver } from "./maputnik-driver";
describe("sprite picker", () => {
const { given, when, get, then } = new MaputnikDriver();
beforeEach(async () => {
await given.setupMockBackedResponses();
await when.setStyle("sprite");
await when.openSpritePicker();
});
test("shows the catalog names and atlas crops in the option rows", async () => {
await then(get.spriteOptions()).shouldHaveLength(4);
await then(get.spritePixel("red", 8, 8)).shouldDeepNestedInclude([255, 0, 0, 255]);
await then(get.spritePixel("green", 8, 8)).shouldDeepNestedInclude([0, 128, 0, 255]);
});
test("keeps the menu in the viewport when the field is near the bottom", async () => {
await when.scrollSpritePickerToBottom();
await then(get.elementByTestId("sprite-preview:red")).shouldBeInViewport();
await then(get.elementByTestId("sprite-preview:retina-square")).shouldBeInViewport();
});
test("filters names without case sensitivity", async () => {
await when.searchSprites("GrEeN");
await then(get.spriteOptions()).shouldHaveLength(1);
await then(get.spriteOptions()).shouldHaveText("green");
});
test("stores the selected sprite ID", async () => {
await when.chooseSprite("green");
await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({ layers: [{ id: "symbols", layout: { "icon-image": "green" } }] });
});
test("selects a filtered sprite with the keyboard", async () => {
await when.searchSprites("sdf");
await when.chooseSpriteWithKeyboard();
await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({ layers: [{ id: "symbols", layout: { "icon-image": "sdf-circle" } }] });
await then(get.spriteOptions()).shouldNotExist();
});
test("renders an SDF silhouette with transparent outside pixels", async () => {
await then(get.spritePixel("sdf-circle", 16, 16)).shouldDeepNestedInclude([0, 0, 0, 255]);
await then(get.spritePixel("sdf-circle", 0, 0)).shouldDeepNestedInclude([0, 0, 0, 0]);
});
test("respects the sprite pixel ratio", async () => {
await then(get.elementByTestId("sprite-preview:retina-square")).shouldHaveCss("width", "16px");
await then(get.spritePixel("retina-square", 16, 16)).shouldDeepNestedInclude([0, 0, 255, 255]);
});
test("keeps free-form sprite names", async () => {
await when.searchSprites("custom-{type}");
await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({ layers: [{ id: "symbols", layout: { "icon-image": "custom-{type}" } }] });
});
test("Escape closes the menu without selecting an option", async () => {
await when.searchSprites("gr");
await when.closeSpritePicker();
await then(get.spriteOptions()).shouldNotExist();
await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({ layers: [{ id: "symbols", layout: { "icon-image": "gr" } }] });
});
test("keeps expressions in the existing expression editor", async () => {
await when.chooseSprite("green");
await when.makeExpression("icon-image");
await then(get.element("[data-wd-key='spec-field-container:icon-image'] .maputnik-expression-editor")).shouldBeVisible();
await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({ layers: [{ id: "symbols", layout: { "icon-image": ["literal", "green"] } }] });
await then(get.elementByTestId("spec-field-input:icon-image")).shouldNotExist();
});
test("replaces the catalog when a different sprite source is opened", async () => {
await when.openSpriteStyle("sprite-alternate-style");
await when.openSpritePicker();
await then(get.spriteOptions()).shouldHaveLength(1);
await then(get.spriteOptions()).shouldHaveText("blue");
});
test("clears the catalog when the next style has no sprite", async () => {
await when.openSpriteStyle("sprite-empty-style");
await when.openSpritePicker();
await then(get.spriteOptions()).shouldHaveLength(0);
});
});
describe("sprite-backed properties", () => {
const { given, when, get, then } = new MaputnikDriver();
const spriteProperties = [
{ layerType: "background", source: undefined, layerId: "background-pattern", property: "background-pattern" },
{ layerType: "fill", source: "points", layerId: "fill-pattern", property: "fill-pattern" },
{ layerType: "line", source: "points", layerId: "line-pattern", property: "line-pattern" },
{ layerType: "fill-extrusion", source: "points", layerId: "fill-extrusion-pattern", property: "fill-extrusion-pattern" },
] as const;
beforeEach(async () => {
await given.setupMockBackedResponses();
await when.setStyle("sprite");
});
for (const { layerType, source, layerId, property } of spriteProperties) {
test(`${property} uses the sprite preview picker`, async () => {
await when.modal.open();
await when.modal.fillLayers({ type: layerType, layer: source, id: layerId });
await when.openSpritePicker(layerId, property);
await then(get.spriteOptions()).shouldHaveLength(4);
await then(get.spritePixel("red", 8, 8)).shouldDeepNestedInclude([255, 0, 0, 255]);
await when.chooseSprite("green");
await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({
layers: [
{ id: "symbols", layout: { "icon-image": "red" } },
{ id: layerId, paint: { [property]: "green" } },
],
});
});
}
});
describe("namespaced sprite picker", () => {
const { given, when, get, then } = new MaputnikDriver();
beforeEach(async () => {
await given.setupMockBackedResponses();
await when.setStyle("sprite_namespaced");
await when.openSpritePicker();
});
test("retains MapLibre sprite namespaces when selecting an ID", async () => {
await when.chooseSprite("extra:blue");
await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({ layers: [{ id: "symbols", layout: { "icon-image": "extra:blue" } }] });
});
});
describe("sprite image failures", () => {
const { given, when, get, then } = new MaputnikDriver();
beforeEach(async () => {
await given.setupMockBackedResponses();
await given.interceptAndMockResponse({ url: /sprite-atlas\.png/, response: {} });
await when.setStyle("sprite");
await when.openSpritePicker();
});
test("can still select a name when the atlas fails to load", async () => {
await when.chooseSprite("green");
await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({ layers: [{ id: "symbols", layout: { "icon-image": "green" } }] });
});
});
describe("late sprite metadata", () => {
const { given, when, get, then } = new MaputnikDriver();
beforeEach(async () => {
await given.setupMockBackedResponses();
await given.holdResponse(/sprite-atlas\.json/, "sprite-atlas.json", "old-sprite");
await when.setStyle("sprite");
await when.waitForResponse("old-sprite");
await when.openSpriteStyle("sprite-alternate-style");
await when.openSpritePicker();
await then(get.spriteOptions()).shouldHaveText("blue");
});
test("does not restore an old catalog after the source changes", async () => {
await when.releaseResponse("old-sprite");
await then(get.spriteOptions()).shouldHaveLength(1);
await then(get.spriteOptions()).shouldHaveText("blue");
});
});