Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface AutoCleanupFormProps {

export function AutoCleanupForm({ settings, onSuccess }: AutoCleanupFormProps) {
const t = useTranslations("settings.config.form");
const tCommon = useTranslations("settings.common");
const [isSubmitting, setIsSubmitting] = useState(false);

const {
Expand Down Expand Up @@ -190,7 +191,7 @@ export function AutoCleanupForm({ settings, onSuccess }: AutoCleanupFormProps) {
{isSubmitting ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
{t("common.saving")}
{tCommon("saving")}
</>
) : (
t("saveConfig")
Expand Down
6 changes: 6 additions & 0 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defaultLocale } from "@/i18n/config";
import { redirect } from "@/i18n/routing";

export default function RootPage() {
redirect({ href: "/dashboard", locale: defaultLocale });
}
157 changes: 155 additions & 2 deletions src/components/ui/__tests__/map.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,13 @@ const maplibreMocks = vi.hoisted(() => {
draggable = false;
popup: FakePopup | null = null;
element = globalThis.document?.createElement("div") ?? ({} as HTMLElement);
options: Record<string, unknown>;
events = new globalThis.Map<string, Set<() => void>>();

constructor(options: Record<string, unknown>) {
this.options = options;
this.draggable = Boolean(options.draggable);
markers.push(this);
}

setLngLat([lng, lat]: [number, number]) {
Expand Down Expand Up @@ -166,16 +170,31 @@ const maplibreMocks = vi.hoisted(() => {
getPitchAlignment() {
return "auto";
}

on(event: string, handler: () => void) {
if (!this.events.has(event)) {
this.events.set(event, new Set());
}
this.events.get(event)?.add(handler);
return this;
}

off(event: string, handler: () => void) {
this.events.get(event)?.delete(handler);
return this;
}
}

const maps: FakeMap[] = [];
const markers: FakeMarker[] = [];

class FakeMap {
container: HTMLElement;
center: [number, number];
zoom: number;
bearing: number;
pitch: number;
projection: unknown;
style: unknown;
sources = new globalThis.Map<string, FakeGeoJSONSource>();
layers = new globalThis.Map<string, Record<string, unknown>>();
Expand Down Expand Up @@ -204,15 +223,19 @@ const maplibreMocks = vi.hoisted(() => {
this.pitch = next.pitch ?? this.pitch;
}
);
setProjection = vi.fn();
setProjection = vi.fn((nextProjection: unknown) => {
this.projection = nextProjection;
});
setPaintProperty = vi.fn();
resize = vi.fn();

constructor(options: Record<string, unknown>) {
this.container = options.container as HTMLElement;
this.center = (options.center as [number, number] | undefined) ?? [0, 0];
this.zoom = (options.zoom as number | undefined) ?? 0;
this.bearing = (options.bearing as number | undefined) ?? 0;
this.pitch = (options.pitch as number | undefined) ?? 0;
this.projection = options.projection;
this.style = options.style;
this.container.requestFullscreen = vi.fn(async () => undefined);
maps.push(this);
Expand Down Expand Up @@ -337,6 +360,7 @@ const maplibreMocks = vi.hoisted(() => {
FakeMarker,
FakePopup,
maps,
markers,
};
});

Expand All @@ -349,7 +373,15 @@ vi.mock("maplibre-gl", () => ({
},
}));

import { Map, MapClusterLayer, MapControls, MapPopup, MapRoute } from "@/components/ui/map";
import {
Map,
MapClusterLayer,
MapControls,
MapMarker,
MapPopup,
MapRoute,
MarkerContent,
} from "@/components/ui/map";

function render(node: ReactNode) {
const container = document.createElement("div");
Expand Down Expand Up @@ -394,6 +426,7 @@ function click(element: Element) {
describe("Map UI", () => {
beforeEach(() => {
maplibreMocks.maps.length = 0;
maplibreMocks.markers.length = 0;
document.body.innerHTML = "";
Object.defineProperty(window, "matchMedia", {
configurable: true,
Expand Down Expand Up @@ -538,6 +571,54 @@ describe("Map UI", () => {
unmount();
});

test("projection updates sync without recreating the map", async () => {
const { rerender, unmount } = render(
<div className="h-60 w-60">
<Map viewport={{ center: [1, 2], zoom: 3 }} projection={{ type: "mercator" }} />
</div>
);

await flushMicrotasks();
const map = maplibreMocks.maps.at(-1);
expect(maplibreMocks.maps.length).toBe(1);
expect(map?.projection).toEqual({ type: "mercator" });
expect(map?.setProjection).toHaveBeenLastCalledWith({ type: "mercator" });
const initialProjectionCalls = map?.setProjection.mock.calls.length ?? 0;

rerender(
<div className="h-60 w-60">
<Map viewport={{ center: [1, 2], zoom: 3 }} projection={{ type: "mercator" }} />
</div>
);

await flushMicrotasks();

expect(map?.setProjection).toHaveBeenCalledTimes(initialProjectionCalls);

rerender(
<div className="h-60 w-60">
<Map viewport={{ center: [1, 2], zoom: 3 }} projection={{ type: "globe" }} />
</div>
);

await flushMicrotasks();

expect(maplibreMocks.maps.length).toBe(1);
expect(map?.setProjection).toHaveBeenLastCalledWith({ type: "globe" });

rerender(
<div className="h-60 w-60">
<Map viewport={{ center: [1, 2], zoom: 3 }} />
</div>
);

await flushMicrotasks();

expect(map?.setProjection).toHaveBeenLastCalledWith({ type: "mercator" });

unmount();
});

test("MapRoute clears rendered geometry when coordinates shrink below two points", async () => {
const { rerender, unmount } = render(
<div className="h-60 w-60">
Expand Down Expand Up @@ -615,4 +696,76 @@ describe("Map UI", () => {

unmount();
});

test("MapClusterLayer re-adds recreated sources with the latest data", async () => {
const { rerender, unmount } = render(
<div className="h-60 w-60">
<Map viewport={{ center: [1, 2], zoom: 3 }}>
<MapClusterLayer data="https://example.com/a.geojson" clusterRadius={50} />
</Map>
</div>
);

await flushMicrotasks();

rerender(
<div className="h-60 w-60">
<Map viewport={{ center: [1, 2], zoom: 3 }}>
<MapClusterLayer data="https://example.com/b.geojson" clusterRadius={50} />
</Map>
</div>
);
await flushMicrotasks();

rerender(
<div className="h-60 w-60">
<Map viewport={{ center: [1, 2], zoom: 3 }}>
<MapClusterLayer data="https://example.com/b.geojson" clusterRadius={60} />
</Map>
</div>
);
await flushMicrotasks();

const map = maplibreMocks.maps.at(-1);
const source = Array.from(map?.sources.values() ?? [])[0];
expect(source?.data).toBe("https://example.com/b.geojson");

unmount();
});

test("MapMarker recreates construction-only options outside render", async () => {
const { rerender, unmount } = render(
<div className="h-60 w-60">
<Map viewport={{ center: [1, 2], zoom: 3 }}>
<MapMarker longitude={1} latitude={2} anchor="bottom" color="#111111">
<MarkerContent>marker</MarkerContent>
</MapMarker>
</Map>
</div>
);

await flushMicrotasks();

expect(maplibreMocks.markers).toHaveLength(1);
expect(maplibreMocks.markers[0]?.options.anchor).toBe("bottom");
expect(maplibreMocks.markers[0]?.options.color).toBe("#111111");

rerender(
<div className="h-60 w-60">
<Map viewport={{ center: [1, 2], zoom: 3 }}>
<MapMarker longitude={1} latitude={2} anchor="top" color="#222222">
<MarkerContent>marker</MarkerContent>
</MapMarker>
</Map>
</div>
);
await flushMicrotasks();

expect(maplibreMocks.maps.length).toBe(1);
expect(maplibreMocks.markers).toHaveLength(2);
expect(maplibreMocks.markers[1]?.options.anchor).toBe("top");
expect(maplibreMocks.markers[1]?.options.color).toBe("#222222");

unmount();
});
});
Loading
Loading