-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCoordinateViewer.test.tsx
254 lines (215 loc) · 9.03 KB
/
CoordinateViewer.test.tsx
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)
// SPDX-License-Identifier: Apache-2.0
import { MapContainer } from "@open-pioneer/map";
import { createServiceOptions, setupMap, waitForMapMount } from "@open-pioneer/map-test-utils";
import { PackageContextProvider } from "@open-pioneer/test-utils/react";
import { act, render, renderHook, screen, waitFor } from "@testing-library/react";
import View from "ol/View";
import BaseEvent from "ol/events/Event";
import { expect, it } from "vitest";
import { CoordinateViewer, useCoordinatesString } from "./CoordinateViewer";
it("should successfully create a coordinate viewer component", async () => {
const { map, registry } = await setupMap();
const injectedServices = createServiceOptions({ registry });
render(
<PackageContextProvider services={injectedServices}>
<CoordinateViewer map={map} data-testid="coordinate-viewer" />
</PackageContextProvider>
);
// coordinate viewer is mounted
const { viewerDiv } = await waitForCoordinateViewer();
expect(viewerDiv).toMatchSnapshot();
// check coordinate viewer box is available
expect(viewerDiv.tagName).toBe("DIV");
});
it("should successfully create a coordinate viewer component with additional css classes", async () => {
const { map, registry } = await setupMap();
const injectedServices = createServiceOptions({ registry });
render(
<PackageContextProvider services={injectedServices}>
<CoordinateViewer map={map} className="test" data-testid="coordinate-viewer" />
</PackageContextProvider>
);
const { viewerDiv } = await waitForCoordinateViewer();
expect(viewerDiv.classList.contains("test")).toBe(true);
expect(viewerDiv.classList.contains("foo")).toBe(false);
});
it("tracks the user's mouse position", async () => {
const { map, registry } = await setupMap();
const injectedServices = createServiceOptions({ registry });
render(
<PackageContextProvider services={injectedServices}>
<MapContainer map={map} data-testid="map" />
<CoordinateViewer map={map} precision={1} data-testid="coordinate-viewer" />
</PackageContextProvider>
);
await waitForMapMount("map");
const { viewerText } = await waitForCoordinateViewer();
expect(viewerText.textContent).toMatchInlineSnapshot('""');
const simulateMove = (x: number, y: number) => {
const fakeMoveEvent = new BaseEvent("pointermove");
(fakeMoveEvent as any).coordinate = [x, y];
map.olMap.dispatchEvent(fakeMoveEvent);
};
// Simple move
act(() => {
simulateMove(123.4, 567.8);
});
expect(viewerText.textContent).toMatchInlineSnapshot('"123.4 567.8 EPSG:3857"');
// Another move + projection change
act(() => {
map.olMap.setView(
new View({
center: [0, 0],
zoom: 0,
projection: "EPSG:4326"
})
);
simulateMove(42, 1337);
});
expect(viewerText.textContent).toMatchInlineSnapshot('"42.0 1,337.0 EPSG:4326"');
});
it("should format coordinates to correct coordinate string for the corresponding locale and precision", async () => {
const coords = [3545.08081, 4543543.009];
const renderCoords = (locale: string, precision = 2) => {
return renderHook(() => useCoordinatesString(coords, precision, undefined), {
wrapper: (props) => <PackageContextProvider {...props} locale={locale} />
});
};
const hookEN = renderCoords("en");
const stringCoordinates = hookEN.result.current;
expect(stringCoordinates).equals("3,545.08 4,543,543.01");
const hookDE = renderCoords("de", 3);
expect(hookDE.result.current).equals("3.545,081 4.543.543,009");
const hookDE_precision0 = renderCoords("de", 0);
expect(hookDE_precision0.result.current).equals("3.545 4.543.543");
});
it("should format coordinates to correct coordinate string with default precision", async () => {
const coords = [3545.08081, 4543543.009];
const hookDeWithoutPrecision = renderHook(
() => useCoordinatesString(coords, undefined, undefined),
{
wrapper: (props) => <PackageContextProvider {...props} locale="de" />
}
);
expect(hookDeWithoutPrecision.result.current).equals("3.545,0808 4.543.543,0090");
});
it("should display transformed coordinates if output projection is provided", async () => {
const outputProjection = "EPSG:4326"; //WGS84
const { map, registry } = await setupMap();
const injectedServices = createServiceOptions({ registry });
render(
<PackageContextProvider services={injectedServices}>
<MapContainer map={map} data-testid="map" />
<CoordinateViewer
map={map}
precision={2}
displayProjectionCode={outputProjection}
data-testid="coordinate-viewer"
/>
</PackageContextProvider>
);
await waitForMapMount("map");
const { viewerText } = await waitForCoordinateViewer();
expect(viewerText.textContent).toMatchInlineSnapshot('""');
const simulateMove = (x: number, y: number) => {
const fakeMoveEvent = new BaseEvent("pointermove");
(fakeMoveEvent as any).coordinate = [x, y];
map.olMap.dispatchEvent(fakeMoveEvent);
};
// Simple move
act(() => {
simulateMove(851594.11, 6789283.95); //map projection is EPSG:3857 (Web Mercator)
});
//851594.11, 6789283.95 (EPSG:3857) == 7.65, 51.94 (EPSG:4326)
expect(viewerText.textContent).toMatchInlineSnapshot('"7.65 51.94 EPSG:4326"'); //should display WGS84
});
it("tracks the user's mouse position, when format is set to 'degree'", async () => {
const { map, registry } = await setupMap();
const injectedServices = createServiceOptions({ registry });
render(
<PackageContextProvider services={injectedServices}>
<MapContainer map={map} data-testid="map" />
<CoordinateViewer
map={map}
precision={2}
format="degree"
data-testid="coordinate-viewer"
/>
</PackageContextProvider>
);
await waitForMapMount("map");
const { viewerText } = await waitForCoordinateViewer();
expect(viewerText.textContent).toMatchInlineSnapshot('""');
const simulateMove = (x: number, y: number) => {
const fakeMoveEvent = new BaseEvent("pointermove");
(fakeMoveEvent as any).coordinate = [x, y];
map.olMap.dispatchEvent(fakeMoveEvent);
};
// Simple move
act(() => {
simulateMove(7.65, 51.94);
});
expect(viewerText.textContent).toMatchInlineSnapshot(
'"7°39\'0.00"(E) 51°56\'24.00"(N) EPSG:3857"'
);
// Another move + projection change
act(() => {
map.olMap.setView(
new View({
center: [0, 0],
zoom: 0,
projection: "EPSG:4326"
})
);
simulateMove(42, 1337);
});
expect(viewerText.textContent).toMatchInlineSnapshot(
'"42°0\'0.00"(E) 1337°0\'0.00"(N) EPSG:4326"'
);
});
it("should display transformed coordinates if output projection and format is set to 'degree' is provided", async () => {
const outputProjection = "EPSG:4326"; //WGS84
const format = "degree";
const { map, registry } = await setupMap();
const injectedServices = createServiceOptions({ registry });
render(
<PackageContextProvider services={injectedServices}>
<MapContainer map={map} data-testid="map" />
<CoordinateViewer
map={map}
precision={2}
displayProjectionCode={outputProjection}
format={format}
data-testid="coordinate-viewer"
/>
</PackageContextProvider>
);
await waitForMapMount("map");
const { viewerText } = await waitForCoordinateViewer();
expect(viewerText.textContent).toMatchInlineSnapshot('""');
const simulateMove = (x: number, y: number) => {
const fakeMoveEvent = new BaseEvent("pointermove");
(fakeMoveEvent as any).coordinate = [x, y];
map.olMap.dispatchEvent(fakeMoveEvent);
};
// Simple move
act(() => {
simulateMove(851594.11, 6789283.95); //map projection is EPSG:3857 (Web Mercator)
});
//851594.11, 6789283.95 (EPSG:3857) == 7°39'0.00"(E) 51°56'24.00"(N) (EPSG:4326)
expect(viewerText.textContent).toMatchInlineSnapshot(
'"7°39\'0.00"(E) 51°56\'24.00"(N) EPSG:4326"'
); //should display WGS84 in Degree
});
async function waitForCoordinateViewer() {
const { viewerDiv, viewerText } = await waitFor(async () => {
const viewerDiv = await screen.findByTestId("coordinate-viewer");
const viewerText = viewerDiv.querySelector(".coordinate-viewer-text");
if (!viewerText) {
throw new Error("coordinate viewer text not rendered");
}
return { viewerDiv, viewerText };
});
return { viewerDiv, viewerText };
}