Skip to content

Commit f717d5e

Browse files
committed
test(colorbar): guard stackOrientation round-trip in project state
Export normalizeColorbarState and add unit tests that a horizontal stack choice persists, missing/unknown values fall back to vertical, and the state round-trips cleanly. Addresses review feedback on PR #901.
1 parent e04dbf7 commit f717d5e

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

packages/plugins/src/plugins/maplibre-components.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,7 @@ function normalizeComponentsProjectState(
14321432
};
14331433
}
14341434

1435-
function normalizeColorbarState(
1435+
export function normalizeColorbarState(
14361436
state: unknown
14371437
): ComponentColorbarGuiState | undefined {
14381438
if (!state || typeof state !== "object") return undefined;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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

Comments
 (0)