mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-03 03:23:20 +00:00
161 lines
5.1 KiB
TypeScript
161 lines
5.1 KiB
TypeScript
|
|
import { describe, expect, it } from "vitest";
|
||
|
|
import { ASSIGNEE_FILTER_UNASSIGNED } from "@/domain/work-orders/types/work-order-list";
|
||
|
|
import { EMPTY_ADVANCED_FILTERS } from "@/domain/work-orders/types/work-order-advanced-filters";
|
||
|
|
import { WORK_ORDER_TYPE_API } from "@/domain/work-orders/types/work-order-board";
|
||
|
|
import {
|
||
|
|
BOARD_SEARCH_MAX_PAGE_SIZE,
|
||
|
|
buildBoardSearchParamsRecord,
|
||
|
|
OVERDUE_TYPE_API,
|
||
|
|
toBoardQueryParams,
|
||
|
|
toBoardSearchQueryParams,
|
||
|
|
} from "@/domain/work-orders/utils/board-query-params";
|
||
|
|
|
||
|
|
describe("toBoardQueryParams", () => {
|
||
|
|
const baseBar = {
|
||
|
|
weekMonday: "2026-07-13",
|
||
|
|
dispatcherIds: new Set<string>(),
|
||
|
|
type: "All" as const,
|
||
|
|
};
|
||
|
|
|
||
|
|
it("omits search when shorter than 2 characters", () => {
|
||
|
|
const params = toBoardQueryParams(baseBar, "B");
|
||
|
|
expect(params.search).toBeUndefined();
|
||
|
|
expect(params.weekStart).toBe("2026-07-13");
|
||
|
|
expect(params.weekEnd).toBe("2026-07-17");
|
||
|
|
});
|
||
|
|
|
||
|
|
it("includes trimmed search when length >= 2", () => {
|
||
|
|
const params = toBoardQueryParams(baseBar, " BK5 ");
|
||
|
|
expect(params.search).toBe("BK5");
|
||
|
|
});
|
||
|
|
|
||
|
|
it("maps unassigned dispatcher to __unassigned__", () => {
|
||
|
|
const params = toBoardQueryParams(
|
||
|
|
{
|
||
|
|
...baseBar,
|
||
|
|
dispatcherIds: new Set([ASSIGNEE_FILTER_UNASSIGNED, "user-guid"]),
|
||
|
|
},
|
||
|
|
"",
|
||
|
|
);
|
||
|
|
expect(params.dispatchers).toEqual(["__unassigned__", "user-guid"]);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("maps PM type filter to API types", () => {
|
||
|
|
const params = toBoardQueryParams({ ...baseBar, type: "PM" }, "");
|
||
|
|
expect(params.types).toEqual([WORK_ORDER_TYPE_API.PM]);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("maps Overdue bar filter to types=99", () => {
|
||
|
|
const params = toBoardQueryParams({ ...baseBar, type: "Overdue" }, "");
|
||
|
|
expect(params.types).toEqual([OVERDUE_TYPE_API]);
|
||
|
|
expect(OVERDUE_TYPE_API).toBe(99);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("omits types when filter is All", () => {
|
||
|
|
const params = toBoardQueryParams(baseBar, "");
|
||
|
|
expect(params.types).toBeUndefined();
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe("toBoardSearchQueryParams", () => {
|
||
|
|
it("defaults page to 0 (0-based pagination)", () => {
|
||
|
|
const params = toBoardSearchQueryParams({
|
||
|
|
filters: { ...EMPTY_ADVANCED_FILTERS },
|
||
|
|
});
|
||
|
|
expect(params.page).toBe(0);
|
||
|
|
expect(params.pageSize).toBe(BOARD_SEARCH_MAX_PAGE_SIZE);
|
||
|
|
expect(params.datePreset).toBe("ThisWeek");
|
||
|
|
});
|
||
|
|
|
||
|
|
it("maps date presets to PascalCase API values", () => {
|
||
|
|
const params = toBoardSearchQueryParams({
|
||
|
|
filters: { ...EMPTY_ADVANCED_FILTERS, dateRange: "last-3-months" },
|
||
|
|
});
|
||
|
|
expect(params.datePreset).toBe("Last3Months");
|
||
|
|
});
|
||
|
|
|
||
|
|
it("includes dateFrom/dateTo only for Custom preset", () => {
|
||
|
|
const custom = toBoardSearchQueryParams({
|
||
|
|
filters: {
|
||
|
|
...EMPTY_ADVANCED_FILTERS,
|
||
|
|
dateRange: "custom",
|
||
|
|
customFrom: "2026-06-01",
|
||
|
|
customTo: "2026-06-30",
|
||
|
|
},
|
||
|
|
});
|
||
|
|
expect(custom.datePreset).toBe("Custom");
|
||
|
|
expect(custom.dateFrom).toBe("2026-06-01");
|
||
|
|
expect(custom.dateTo).toBe("2026-06-30");
|
||
|
|
|
||
|
|
const thisWeek = toBoardSearchQueryParams({
|
||
|
|
filters: {
|
||
|
|
...EMPTY_ADVANCED_FILTERS,
|
||
|
|
dateRange: "this-week",
|
||
|
|
customFrom: "2026-06-01",
|
||
|
|
customTo: "2026-06-30",
|
||
|
|
},
|
||
|
|
});
|
||
|
|
expect(thisWeek.dateFrom).toBeUndefined();
|
||
|
|
expect(thisWeek.dateTo).toBeUndefined();
|
||
|
|
});
|
||
|
|
|
||
|
|
it("maps Overdue in advanced types to 99", () => {
|
||
|
|
const params = toBoardSearchQueryParams({
|
||
|
|
filters: {
|
||
|
|
...EMPTY_ADVANCED_FILTERS,
|
||
|
|
types: new Set(["PM", "Overdue"]),
|
||
|
|
},
|
||
|
|
});
|
||
|
|
expect(params.types).toEqual([WORK_ORDER_TYPE_API.PM, OVERDUE_TYPE_API]);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("resolves vendor company names to vendorIds", () => {
|
||
|
|
const params = toBoardSearchQueryParams({
|
||
|
|
filters: {
|
||
|
|
...EMPTY_ADVANCED_FILTERS,
|
||
|
|
vendorTechs: new Set(["Acme HVAC", "Unknown"]),
|
||
|
|
},
|
||
|
|
vendorIdsByCompanyName: new Map([["Acme HVAC", 12]]),
|
||
|
|
});
|
||
|
|
expect(params.vendorIds).toEqual([12]);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("omits empty filter arrays", () => {
|
||
|
|
const params = toBoardSearchQueryParams({
|
||
|
|
filters: { ...EMPTY_ADVANCED_FILTERS },
|
||
|
|
search: "x",
|
||
|
|
});
|
||
|
|
expect(params.search).toBeUndefined();
|
||
|
|
expect(params.sites).toBeUndefined();
|
||
|
|
expect(params.types).toBeUndefined();
|
||
|
|
expect(params.dispatchers).toBeUndefined();
|
||
|
|
expect(params.statuses).toBeUndefined();
|
||
|
|
expect(params.pmTypes).toBeUndefined();
|
||
|
|
expect(params.vendorIds).toBeUndefined();
|
||
|
|
expect(params.docStatuses).toBeUndefined();
|
||
|
|
});
|
||
|
|
|
||
|
|
it("caps pageSize at BOARD_SEARCH_MAX_PAGE_SIZE", () => {
|
||
|
|
const params = toBoardSearchQueryParams({
|
||
|
|
filters: { ...EMPTY_ADVANCED_FILTERS },
|
||
|
|
pageSize: 500,
|
||
|
|
});
|
||
|
|
expect(params.pageSize).toBe(100);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe("buildBoardSearchParamsRecord", () => {
|
||
|
|
it("keeps arrays as repeated-key friendly values (not types[])", () => {
|
||
|
|
const record = buildBoardSearchParamsRecord({
|
||
|
|
weekStart: "2026-07-13",
|
||
|
|
types: [2, 6],
|
||
|
|
dispatchers: ["guid-a", "__unassigned__"],
|
||
|
|
search: "",
|
||
|
|
});
|
||
|
|
expect(record.types).toEqual([2, 6]);
|
||
|
|
expect(record.dispatchers).toEqual(["guid-a", "__unassigned__"]);
|
||
|
|
expect(record.search).toBeUndefined();
|
||
|
|
expect(Object.keys(record)).not.toContain("types[]");
|
||
|
|
});
|
||
|
|
});
|