-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseMapStyleControl.test.tsx
More file actions
224 lines (207 loc) · 7.68 KB
/
Copy pathBaseMapStyleControl.test.tsx
File metadata and controls
224 lines (207 loc) · 7.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* Libration
* Copyright (C) 2026 Ken McDonald
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
/** @vitest-environment happy-dom */
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, describe, expect, it } from "vitest";
import { useState } from "react";
import {
DEFAULT_BASE_MAP_PRESENTATION,
type BaseMapPresentationConfig,
} from "../../config/baseMapPresentation";
import {
DEFAULT_EQUIRECT_BASE_MAP_ID,
getEquirectBaseMapOptionForId,
} from "../../config/v2/sceneConfig";
import { BaseMapStyleControl } from "./BaseMapStyleControl";
function Harness({
baseMapId = DEFAULT_EQUIRECT_BASE_MAP_ID,
initial = DEFAULT_BASE_MAP_PRESENTATION,
productInstantMs,
mutable = true,
onSelectId,
}: {
baseMapId?: string;
initial?: BaseMapPresentationConfig;
productInstantMs?: number;
mutable?: boolean;
onSelectId?: (id: string) => void;
}) {
const [presentation, setPresentation] = useState(initial);
const [mapId, setMapId] = useState(baseMapId);
return (
<>
<BaseMapStyleControl
baseMapId={mapId}
presentation={presentation}
productInstantMs={productInstantMs}
mutable={mutable}
onSelectId={
onSelectId ??
((id) => {
setMapId(id);
})
}
onPresentationChange={setPresentation}
/>
<pre data-testid="presentation-state">{JSON.stringify(presentation)}</pre>
</>
);
}
function readPresentation(): BaseMapPresentationConfig {
return JSON.parse(screen.getByTestId("presentation-state").textContent ?? "{}");
}
describe("BaseMapStyleControl", () => {
afterEach(() => {
cleanup();
});
it("commits a valid typed brightness on blur and updates presentation", async () => {
const user = userEvent.setup();
render(<Harness />);
const num = screen.getByTestId("config-bm-pres-brightness-number");
await user.clear(num);
await user.type(num, "1.25");
fireEvent.blur(num);
const p = readPresentation();
expect(p.brightness).toBe(1.25);
expect(p.contrast).toBe(1);
expect(p.gamma).toBe(1);
expect(p.saturation).toBe(1);
});
it("clamps an out-of-range typed value on blur through normalize", async () => {
const user = userEvent.setup();
render(<Harness />);
const num = screen.getByTestId("config-bm-pres-brightness-number");
await user.clear(num);
await user.type(num, "10");
fireEvent.blur(num);
expect(readPresentation().brightness).toBe(2);
});
it("does not commit invalid or empty text on blur (leaves last good state)", async () => {
const user = userEvent.setup();
render(
<Harness
initial={{
...DEFAULT_BASE_MAP_PRESENTATION,
brightness: 0.8,
}}
/>,
);
const num = screen.getByTestId("config-bm-pres-brightness-number");
await user.click(num);
fireEvent.change(num, { target: { value: "nope" } });
fireEvent.blur(num);
expect(readPresentation().brightness).toBe(0.8);
expect(num).toHaveValue(0.8);
});
it("keeps last good state when blurring with an empty field", async () => {
const user = userEvent.setup();
render(
<Harness
initial={{
...DEFAULT_BASE_MAP_PRESENTATION,
contrast: 0.7,
}}
/>,
);
const num = screen.getByTestId("config-bm-pres-contrast-number");
await user.clear(num);
fireEvent.blur(num);
expect(readPresentation().contrast).toBe(0.7);
});
it("shows source and license block for legacy default with packaged attribution", () => {
render(<Harness />);
const block = screen.getByTestId("config-base-map-source-license");
expect(block).toHaveAttribute("aria-label", "Source and license");
expect(screen.getByText("Libration packaged reference map")).toBeInTheDocument();
expect(
screen.getByText(/Original shaded-relief basemap shipped with Libration/),
).toBeInTheDocument();
});
it("shows external source link when political family is selected", async () => {
const user = userEvent.setup();
render(<Harness />);
await user.selectOptions(screen.getByLabelText("Map style"), "equirect-world-political-v1");
const link = screen.getByRole("link", { name: "Natural Earth" });
expect(link).toHaveAttribute("href", "https://www.naturalearthdata.com/");
expect(link).toHaveAttribute("target", "_blank");
expect(link).toHaveAttribute("rel", "noopener noreferrer");
expect(screen.getByText("Natural Earth (public domain)")).toBeInTheDocument();
});
it("shows CC BY source link when Köppen–Geiger climate family is selected", async () => {
const user = userEvent.setup();
render(<Harness />);
await user.selectOptions(
screen.getByLabelText("Map style"),
"equirect-world-climate-koppen-beck-v1",
);
const link = screen.getByRole("link", { name: "Beck et al. (2018) dataset (Figshare)" });
expect(link).toHaveAttribute("href", "https://doi.org/10.6084/m9.figshare.6396959");
const block = screen.getByTestId("config-base-map-source-license");
expect(block).toHaveTextContent(/Beck et al\. \(2018\)/);
expect(block).toHaveTextContent(/CC BY 4\.0/i);
});
it("exposes licenseNote and sourceLinks on political catalog option", () => {
const o = getEquirectBaseMapOptionForId("equirect-world-political-v1");
expect(o.licenseNote).toMatch(/public domain/i);
expect(o.sourceLinks?.[0]?.href).toBe("https://www.naturalearthdata.com/");
});
it("exposes variantMode on catalog options", () => {
expect(getEquirectBaseMapOptionForId(DEFAULT_EQUIRECT_BASE_MAP_ID).variantMode).toBe("static");
expect(getEquirectBaseMapOptionForId("equirect-world-blue-marble-bm-v1").variantMode).toBe(
"monthOfYear",
);
});
it("does not show active UTC month for static families", () => {
const julyMs = Date.UTC(2024, 6, 15);
render(<Harness productInstantMs={julyMs} />);
expect(screen.queryByTestId("config-base-map-active-month")).not.toBeInTheDocument();
});
it("shows active UTC civil month for month-aware Blue Marble family", async () => {
const user = userEvent.setup();
const julyMs = Date.UTC(2024, 6, 15);
render(
<Harness
baseMapId="equirect-world-blue-marble-bm-v1"
productInstantMs={julyMs}
/>,
);
expect(screen.getByTestId("config-base-map-active-month")).toHaveTextContent(
"Displaying: July (UTC civil month 7)",
);
await user.selectOptions(screen.getByLabelText("Map style"), "equirect-world-political-v1");
expect(screen.queryByTestId("config-base-map-active-month")).not.toBeInTheDocument();
});
it("omits active month line when productInstantMs is not provided", () => {
render(<Harness baseMapId="equirect-world-blue-marble-t-v1" />);
expect(screen.queryByTestId("config-base-map-active-month")).not.toBeInTheDocument();
});
it("reset display restores defaults for all presentation fields", async () => {
const user = userEvent.setup();
render(
<Harness
initial={{
brightness: 0.7,
contrast: 0.6,
gamma: 0.6,
saturation: 0.5,
}}
/>,
);
await user.click(
screen.getByRole("button", { name: "Reset base map display to defaults" }),
);
const p = readPresentation();
expect(p).toEqual({ ...DEFAULT_BASE_MAP_PRESENTATION });
});
});