Files
watermaps/apps/web/tests/api-features.test.ts
BuTzZ 593dbd5f85
Test and publish container images / test (push) Successful in 2m26s
Test and publish container images / publish (push) Failing after 3s
optimized events
2026-07-28 14:36:47 +02:00

107 lines
2.7 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import {
FeatureDataUnavailableError,
getMapFeatures
} from "../src/api";
afterEach(() => {
vi.unstubAllGlobals();
});
describe("getMapFeatures", () => {
it("rejects an unavailable feature source instead of treating it as an empty result", async () => {
vi.stubGlobal(
"fetch",
vi.fn(async () =>
new Response(
JSON.stringify({
type: "FeatureCollection",
features: [],
metadata: {
source: "unavailable",
warning: "Keine lokale Feature-Datenbank konfiguriert."
}
}),
{
status: 200,
headers: { "content-type": "application/json" }
}
)
)
);
await expect(
getMapFeatures({
bbox: [7, 53, 8, 54],
layers: ["harbours"]
})
).rejects.toEqual(
expect.objectContaining({
name: "FeatureDataUnavailableError",
message: "Keine lokale Feature-Datenbank konfiguriert."
})
);
await expect(
getMapFeatures({
bbox: [7, 53, 8, 54],
layers: ["locks"]
})
).rejects.toBeInstanceOf(FeatureDataUnavailableError);
});
it("keeps usable postgis and legacy feature collections intact", async () => {
const responses = [
{
type: "FeatureCollection",
features: [],
metadata: { source: "postgis" }
},
{
type: "FeatureCollection",
features: []
}
];
vi.stubGlobal(
"fetch",
vi.fn(async () =>
new Response(JSON.stringify(responses.shift()), {
status: 200,
headers: { "content-type": "application/json" }
})
)
);
await expect(
getMapFeatures({ bbox: [7, 53, 8, 54], layers: ["bridges"] })
).resolves.toMatchObject({ metadata: { source: "postgis" } });
await expect(
getMapFeatures({ bbox: [7, 53, 8, 54], layers: ["bridges"] })
).resolves.toMatchObject({ features: [] });
});
it("uses the API error message for an unavailable source", async () => {
vi.stubGlobal(
"fetch",
vi.fn(async () =>
new Response(
JSON.stringify({
error: "feature_source_unavailable",
message: "Keine lokale Karten- und Ereignisdatenbank konfiguriert."
}),
{
status: 503,
statusText: "Service Unavailable",
headers: { "content-type": "application/json" }
}
)
)
);
await expect(
getMapFeatures({ bbox: [7, 53, 8, 54], layers: ["locks"] })
).rejects.toThrow(
"Keine lokale Karten- und Ereignisdatenbank konfiguriert."
);
});
});