|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { describe, it } from "node:test"; |
| 3 | +import { normalizeColorbarState } from "../packages/plugins/src/plugins/maplibre-components.ts"; |
| 4 | + |
| 5 | +/** |
| 6 | + * The colorbar plugin persists a `stackOrientation` flag (vertical vs |
| 7 | + * horizontal stacking) in the saved project state. These tests guard the |
| 8 | + * normalization path that runs on both live control snapshots and deserialized |
| 9 | + * project JSON, so a user's horizontal choice round-trips on reopen and old |
| 10 | + * projects without the field fall back to the previous (vertical) behavior. |
| 11 | + */ |
| 12 | +describe("normalizeColorbarState stackOrientation", () => { |
| 13 | + it("keeps a horizontal stack orientation", () => { |
| 14 | + const normalized = normalizeColorbarState({ |
| 15 | + visible: true, |
| 16 | + colorbars: [], |
| 17 | + stackOrientation: "horizontal", |
| 18 | + }); |
| 19 | + assert.equal(normalized?.stackOrientation, "horizontal"); |
| 20 | + }); |
| 21 | + |
| 22 | + it("defaults missing stack orientation to vertical (backward compat)", () => { |
| 23 | + const normalized = normalizeColorbarState({ visible: true, colorbars: [] }); |
| 24 | + assert.equal(normalized?.stackOrientation, "vertical"); |
| 25 | + }); |
| 26 | + |
| 27 | + it("coerces an unknown stack orientation to vertical", () => { |
| 28 | + const normalized = normalizeColorbarState({ |
| 29 | + visible: true, |
| 30 | + colorbars: [], |
| 31 | + stackOrientation: "diagonal", |
| 32 | + }); |
| 33 | + assert.equal(normalized?.stackOrientation, "vertical"); |
| 34 | + }); |
| 35 | + |
| 36 | + it("round-trips a horizontal choice through a second normalization", () => { |
| 37 | + const once = normalizeColorbarState({ |
| 38 | + visible: true, |
| 39 | + colorbars: [ |
| 40 | + { |
| 41 | + mode: "named", |
| 42 | + colormap: "viridis", |
| 43 | + customColors: "", |
| 44 | + vmin: 0, |
| 45 | + vmax: 100, |
| 46 | + label: "Depth", |
| 47 | + units: "", |
| 48 | + orientation: "vertical", |
| 49 | + colorbarPosition: "bottom-right", |
| 50 | + }, |
| 51 | + ], |
| 52 | + stackOrientation: "horizontal", |
| 53 | + }); |
| 54 | + const twice = normalizeColorbarState(once); |
| 55 | + assert.equal(twice?.stackOrientation, "horizontal"); |
| 56 | + assert.deepEqual(twice, once); |
| 57 | + }); |
| 58 | +}); |
0 commit comments