shoc-frontend-new/src/test/data/areas.test.ts
Alexandre Brandizzi ebc401dcb7
Some checks are pending
Frontend checks / Build and test (push) Waiting to run
Frontend checks / governance (push) Waiting to run
Frontend checks / Visual regression (push) Waiting to run
Deploy dev content / Deploy shoc-frontend-new-dev through Terraform (push) Waiting to run
SH-273: derive Site Area from State (#190)
* feat(locations): derive site areas from state

* chore(locations): keep ticket keys out of source comments

---------

Co-authored-by: Codex Review Integration <codex-review@local.invalid>
2026-09-15 16:38:18 -03:00

165 lines
4.8 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
AREA_ZONE_STATE_CODES,
AREA_ZONES,
STATE_AREA_ENTRIES,
US_STATE_CODES,
expandAreasToStateCodes,
formatStateOptionLabel,
getAreaForState,
normalizeStateCode,
} from "@/data/areas";
const EAST = [
"CT",
"DE",
"GA",
"MA",
"MD",
"ME",
"NC",
"NH",
"NJ",
"NY",
"PA",
"RI",
"SC",
"VA",
"VT",
"WV",
];
const CENTRAL = [
"AL",
"AR",
"FL",
"IA",
"IL",
"IN",
"KS",
"KY",
"LA",
"MI",
"MN",
"MO",
"MS",
"ND",
"NE",
"OH",
"OK",
"SD",
"TN",
"WI",
];
const WEST = ["AK", "AZ", "CO", "HI", "ID", "MT", "NM", "NV", "OR", "TX", "UT", "WA", "WY"];
describe("state area catalog", () => {
it("covers exactly the 50 states with unique codes and names", () => {
expect(STATE_AREA_ENTRIES).toHaveLength(50);
expect(US_STATE_CODES).toHaveLength(50);
expect(new Set(US_STATE_CODES).size).toBe(50);
expect(new Set(STATE_AREA_ENTRIES.map((entry) => entry.name)).size).toBe(50);
});
it("excludes DC and territories", () => {
expect(US_STATE_CODES).not.toContain("DC");
expect(US_STATE_CODES).not.toContain("PR");
expect(US_STATE_CODES).not.toContain("GU");
expect(US_STATE_CODES).not.toContain("VI");
expect(US_STATE_CODES).not.toContain("AS");
expect(US_STATE_CODES).not.toContain("MP");
});
it("maps every state to exactly one confirmed zone with counts", () => {
expect([...AREA_ZONE_STATE_CODES.East]).toEqual(EAST);
expect([...AREA_ZONE_STATE_CODES.Central]).toEqual(CENTRAL);
expect([...AREA_ZONE_STATE_CODES.West]).toEqual(WEST);
expect([...AREA_ZONE_STATE_CODES.California]).toEqual(["CA"]);
expect(EAST).toHaveLength(16);
expect(CENTRAL).toHaveLength(20);
expect(WEST).toHaveLength(13);
for (const entry of STATE_AREA_ENTRIES) {
expect(AREA_ZONES).toContain(entry.zone);
expect(entry.code).toMatch(/^[A-Z]{2}$/);
expect(entry.name.length).toBeGreaterThan(0);
}
});
it("keeps the canonical zone order", () => {
expect([...AREA_ZONES]).toEqual(["East", "Central", "West", "California"]);
expect(US_STATE_CODES[0]).toBe("CT");
expect(US_STATE_CODES[49]).toBe("CA");
});
});
describe("formatStateOptionLabel", () => {
it("renders Name (CODE)", () => {
expect(formatStateOptionLabel("TX")).toBe("Texas (TX)");
expect(formatStateOptionLabel("CA")).toBe("California (CA)");
});
});
describe("normalizeStateCode", () => {
it("accepts canonical codes in any case", () => {
expect(normalizeStateCode("TX")).toBe("TX");
expect(normalizeStateCode("tx")).toBe("TX");
expect(normalizeStateCode("Mo")).toBe("MO");
});
it("normalizes legacy full-name and lowercase values", () => {
expect(normalizeStateCode("Texas")).toBe("TX");
expect(normalizeStateCode("texas")).toBe("TX");
expect(normalizeStateCode(" Illinois ")).toBe("IL");
expect(normalizeStateCode("NORTH CAROLINA")).toBe("NC");
expect(normalizeStateCode("California")).toBe("CA");
});
it("returns null for empty, unknown, and non-state values", () => {
expect(normalizeStateCode("")).toBeNull();
expect(normalizeStateCode(" ")).toBeNull();
expect(normalizeStateCode(null)).toBeNull();
expect(normalizeStateCode(undefined)).toBeNull();
expect(normalizeStateCode("ZZ")).toBeNull();
expect(normalizeStateCode("DC")).toBeNull();
expect(normalizeStateCode("Puerto Rico")).toBeNull();
});
});
describe("getAreaForState", () => {
it("derives the confirmed zone per state", () => {
expect(getAreaForState("CT")).toBe("East");
expect(getAreaForState("IL")).toBe("Central");
expect(getAreaForState("TX")).toBe("West");
expect(getAreaForState("CA")).toBe("California");
});
it("derives the zone from legacy full-name values", () => {
expect(getAreaForState("illinois")).toBe("Central");
expect(getAreaForState("Texas")).toBe("West");
});
it("returns null for unknown states", () => {
expect(getAreaForState("XX")).toBeNull();
expect(getAreaForState("")).toBeNull();
expect(getAreaForState(null)).toBeNull();
});
});
describe("expandAreasToStateCodes", () => {
it("returns an empty list for an empty selection", () => {
expect(expandAreasToStateCodes([])).toEqual([]);
});
it("expands a single zone in canonical code order", () => {
expect(expandAreasToStateCodes(["East"])).toEqual(EAST);
expect(expandAreasToStateCodes(["California"])).toEqual(["CA"]);
expect(expandAreasToStateCodes(["West"])).toEqual(WEST);
});
it("expands multiple zones in canonical zone order and dedupes", () => {
expect(expandAreasToStateCodes(["West", "California"])).toEqual([...WEST, "CA"]);
expect(expandAreasToStateCodes(["California", "West", "West"])).toEqual([...WEST, "CA"]);
expect(expandAreasToStateCodes([...AREA_ZONES])).toEqual(US_STATE_CODES);
});
});