🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
fix(geoStore): handle empty region input in getRegionName function
Bill Yangf96317e25385
diff --git a/client/src/lib/geoStore.test.ts b/client/src/lib/geoStore.test.tsnew file mode 100644index 000000000..205b3cc1c--- /dev/null+++ b/client/src/lib/geoStore.test.ts@@ -0,0 +1,53 @@+import { afterEach, describe, expect, it } from "vitest";+import { useGeoStore } from "./geoStore";+import type { Subdivisions } from "./geoStore";++const subdivisions: Subdivisions = {+ type: "FeatureCollection",+ features: [+ {+ type: "Feature",+ properties: {+ name: "Azerbaijan",+ iso_3166_2: "",+ admin: "Azerbaijan",+ border: 4,+ },+ geometry: { type: "Polygon", coordinates: [] },+ },+ {+ type: "Feature",+ properties: {+ name: "Henan Sheng",+ iso_3166_2: "CN-HA",+ admin: "China",+ border: 0,+ },+ geometry: { type: "Polygon", coordinates: [] },+ },+ ],+};++afterEach(() => {+ useGeoStore.setState({ subdivisions: null });+});++describe("getRegionName", () => {+ it("does not match malformed subdivision data when the region is missing", () => {+ useGeoStore.setState({ subdivisions });++ expect(useGeoStore.getState().getRegionName("")).toBe("");+ });++ it("returns a matching region name", () => {+ useGeoStore.setState({ subdivisions });++ expect(useGeoStore.getState().getRegionName("CN-HA")).toBe("Henan Sheng");+ });++ it("returns an empty string for an unknown region", () => {+ useGeoStore.setState({ subdivisions });++ expect(useGeoStore.getState().getRegionName("XX-NOTREAL")).toBe("");+ });+});diff --git a/client/src/lib/geoStore.ts b/client/src/lib/geoStore.tsindex 52f21d23c..7e7c9fb25 100644--- a/client/src/lib/geoStore.ts+++ b/client/src/lib/geoStore.ts@@ -88,6 +88,8 @@ export const useGeoStore = create<GeoStore>((set, get) => ({},getRegionName: (region: string) => {+ if (!region) return "";+const { subdivisions } = get();return subdivisions?.features.find(feature => feature.properties.iso_3166_2 === region)?.properties.name ?? "";},