shoc-frontend-new/src/test/domain/locations/api/locations-list-api.test.ts

61 lines
2.1 KiB
TypeScript
Raw Normal View History

import { beforeEach, describe, expect, it, vi } from "vitest";
import { API_PATHS } from "@/api/api-paths";
const apiGet = vi.fn();
vi.mock("@/api/api", () => ({
apiGet: (...args: unknown[]) => apiGet(...args),
apiPost: vi.fn(),
apiPut: vi.fn(),
apiDelete: vi.fn(),
}));
import { expandAreasToStateCodes } from "@/data/areas";
import { locationsApi } from "@/domain/locations/api/locations-api";
describe("locationsApi.getList states query param", () => {
beforeEach(() => {
apiGet.mockReset();
apiGet.mockResolvedValue([]);
});
it("joins state codes into a comma-separated param", async () => {
await locationsApi.getList({ states: ["TX", "MO"] });
expect(apiGet).toHaveBeenCalledWith(API_PATHS.location.getList, {
searchParams: { search: "", page: 1, pageSize: 12, states: "TX,MO" },
});
});
it("omits the states param when no states are provided", async () => {
await locationsApi.getList({});
const [, config] = apiGet.mock.calls[0];
expect(config.searchParams).toEqual({ search: "", page: 1, pageSize: 12 });
expect("states" in config.searchParams).toBe(false);
});
it("omits the states param when the states list is empty", async () => {
await locationsApi.getList({ states: [] });
const [, config] = apiGet.mock.calls[0];
expect(config.searchParams).toEqual({ search: "", page: 1, pageSize: 12 });
expect("states" in config.searchParams).toBe(false);
});
it("preserves the canonical area expansion order in the joined value", async () => {
await locationsApi.getList({ states: expandAreasToStateCodes(["West", "California"]) });
const [, config] = apiGet.mock.calls[0];
expect(config.searchParams.states).toBe("AK,AZ,CO,HI,ID,MT,NM,NV,OR,TX,UT,WA,WY,CA");
});
it("keeps existing search and pagination params", async () => {
await locationsApi.getList({ search: "dallas", page: 2, pageSize: 24, states: ["TX"] });
expect(apiGet).toHaveBeenCalledWith(API_PATHS.location.getList, {
searchParams: { search: "dallas", page: 2, pageSize: 24, states: "TX" },
});
});
});