Files
editor/AGENTS.md
T
SHAIK KHADEER BASHA 07bc2bcc3b Add a line metrics checkbox to GeoJSON sources for line-gradient (#2403)
Fixes #292.

`line-gradient` only renders when the line's GeoJSON source has
`"lineMetrics": true`, and Maputnik had no way to set that. Nothing in
`src` set or showed `lineMetrics`, so a gradient could only be made to
work by editing the source in the code editor. GeoJSON sources (both the
URL and the inline JSON kind) now have a "Line metrics" checkbox in the
Data Sources modal.

The `line-gradient` property itself already works: it shows up under the
line layer's paint properties and is edited in the expression editor
with `["line-progress"]`. I checked that with a style using an
`interpolate` over `line-progress`. So the checkbox was the missing
piece.

**Before / after** (Data Sources → Add New Source → GeoJSON (URL)):

| Before | After |
|---|---|
| <img
src="https://raw.githubusercontent.com/KhadeerBasha1232/maputnik/pr-assets/pr-292/before-geojson-source.png"
width="400"> | <img
src="https://raw.githubusercontent.com/KhadeerBasha1232/maputnik/pr-assets/pr-292/after-geojson-source.png"
width="400"> |

**Performance:** no impact on Maputnik itself. `lineMetrics` only
changes how MapLibre tiles that GeoJSON source when someone turns it on,
and that cost is the reason it's off by default in the spec.

## Launch Checklist

 - [x] Briefly describe the changes in this PR.
 - [x] Link to related issues: fixes #292.
- [x] Include before/after visuals or gifs if this PR includes visual
changes.
- [x] Write tests for all new functionality: `enable line metrics for a
GeoJSON source` in `modals.spec.ts` (with an `enableLineMetrics` action
in the modal driver). It fails on `main`.
 - [x] Add an entry to `CHANGELOG.md` under the `## main` section.

Tested locally: `npm run lint` clean, `npx tsc --noEmit` clean, `npx
vitest run` (57 passed) and the full `npx playwright test` suite (221
passed). The new test fails when I put back `main`'s
`ModalSourcesTypeEditor.tsx`.

Assisted-By: Claude Opus 5.5 (Claude Code). It helped write the change,
the test and this description. I've reviewed and tested the change and
can answer questions about it.
2026-12-04 20:08:50 +02:00

5.8 KiB

Maputnik is a MapLibre style editor written using React and TypeScript.

To get started, install all npm packages:

npm install

Verify code correctness by running ESLint:

npm run lint

Or try fixing lint issues with:

npm run lint -- --fix

The project type checked and built with:

npm run build

Install the Playwright browser (first time only):

npx playwright install --with-deps chromium

Then run the end-to-end tests (Playwright starts the dev server automatically):

npm run test

Run the unit tests with Vitest:

npm run test-unit

Accessibility

  • Every input, select and button needs an accessible name. InputString, InputNumber, InputSelect and InputButton accept an aria-label.
  • When a control repeats, as in the rows of an array or collection field, give each copy its own label that names the row and what it edits (for example … row 2 X offset), and pass it through t(). Controls that share one label can't be told apart with a screen reader.

Translations

  • Every new user-visible string goes through t() and needs a translation in each src/locales/*/translation.json, kept in alphabetical key order.
  • npm run i18n:extract adds missing keys with the value __STRING_NOT_TRANSLATED__; replace each one with a real translation. npx i18next-cli status lists keys that are still missing.

Pull Requests

  • Pull requests should update CHANGELOG.md with a short description of the change.

Testing

Prefer end-to-end tests

Most of this codebase is React components, and they are only reachable from an end-to-end test. E2E coverage is the primary signal.

Reach for a unit test only for pure logic that e2e cannot cheaply reach (parsers, sorting, watchers, stores). Before writing one, check whether e2e already covers the file — a unit test that duplicates existing e2e coverage adds test code and almost no coverage:

npx nyc report --reporter=text --include="src/libs/style.ts"

Do not merge the Vitest (v8) and e2e (istanbul) coverage reports locally. They produce conflicting statement maps for the same files and the combined percentage is meaningless. Codecov merges the two uploads server-side; that is the number to trust. Locally, read them separately:

  • e2e: npx playwright test then npx nyc report --reporter=text-summary (reads .nyc_output/)
  • unit: npx vitest run --coverage (writes coverage/)

E2E layering

Three layers, and the boundaries matter:

  • e2e/playwright-helper.ts — generic, app-agnostic browser actions. The only file allowed to import @playwright/test (besides e2e/utils/fixtures.ts).
  • e2e/maputnik-driver.ts — domain actions (layers, filters, functions, the style). Knows nothing about page or Playwright.
  • e2e/modal-driver.ts — actions scoped to a modal, exposed as when.modal.*.

Specs get a driver at describe scope and assert fluently:

describe("layer editor", () => {
  const { given, get, when, then } = new MaputnikDriver();
  ...
  await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({ layers: [{ id, type: "fill" }] });
});

New UI interactions belong in a driver, not inline in a spec.

Writing assertions

  • shouldDeepNestedInclude is a recursive partial match (toMatchObject): nested objects are matched as subsets, arrays and primitives must match exactly (including array length).
  • Assert against the whole style, not an extracted slice. Avoid get.styleFromLocalStorage().then(style => style.layers.find(...)) — it moves test logic into the test. Compare the real object instead.
  • Query.then() is lazy and returns a new Query, not a Promise. await get.styleFromLocalStorage() hangs forever. Use .get() to await it directly, or pass the Query to then(...).

One behaviour per test

If a test needs comments narrating "and now this…", it is several tests. Split it, and hoist the shared setup into a nested describe + beforeEach.

Test ids

Test ids use the data-wd-key attribute and are read via get.elementByTestId.

The Input* components already accept data-wd-key and render it on the real <input>; the Field* wrappers forward it through their {...props} spread. So passing data-wd-key to a Field* component is usually enough. Do not also add it to Block/Fieldset — the id then matches two elements and locators fail in strict mode.

Note InputNumber renders <key>-text and <key>-range when allowRange is set, and <key> otherwise.

Input commit semantics (common source of "the value didn't save")

  • InputString only fires its onChange on blur or Enter. Typing alone fires onInput. A driver that calls fill() must then call blur(), or the value never reaches the style.
  • InputNumber commits on every change; no blur needed.
  • The autocomplete inputs (layer source, add-layer source) are controlled downshift comboboxes. Keystroke typing is dropped/reordered — {selectall} then typing raster yields "exampleaster". Use fill(), which dispatches a single input event, then pick from the filtered menu.
  • CodeMirror auto-closes brackets and quotes, and types over its own closers, so inserting a well-formed JSON fragment stays well-formed. To break JSON on purpose, insert a bare word.

Fixtures

Style fixtures live in e2e/fixtures/. A new one must be registered in two places in maputnik-driver.ts: the list in given.setupMockBackedResponses and the styleFileByKey map in when.setStyle.

Verify a new test can fail

A test that passes for the wrong reason is worse than no test. After writing one, mutate the expected value and confirm it fails. This has caught real mistakes (e.g. a driver that never committed its input, so the assertion was matching a value written by the previous step).