shoc-frontend-new/src/test/domain/services/api/services-api.test.ts

81 lines
2.4 KiB
TypeScript
Raw Normal View History

import { beforeEach, describe, expect, it, vi } from "vitest";
import { API_PATHS } from "@/api/api-paths";
import type { ServiceInput } from "@/domain/services/types/service";
const apiGet = vi.fn();
const apiPost = vi.fn();
const apiPut = vi.fn();
vi.mock("@/api/api", () => ({
apiGet: (...args: unknown[]) => apiGet(...args),
apiPost: (...args: unknown[]) => apiPost(...args),
apiPut: (...args: unknown[]) => apiPut(...args),
}));
import { servicesApi } from "@/domain/services/api/services-api";
describe("servicesApi", () => {
beforeEach(() => {
apiGet.mockReset();
apiPost.mockReset();
apiPut.mockReset();
});
it("loads active services and maps the linked completion template", async () => {
apiGet.mockResolvedValue({
items: [
{
Id: 7,
Name: "HVAC inspection",
Trade: "HVAC",
IconKey: "droplets",
RequiresCompletionDocument: true,
CompletionDocTemplate: { Id: 4, Name: "HVAC checklist" },
IsActive: true,
SupportedWorkOrderTypes: [2, 6],
},
],
});
const services = await servicesApi.getAll(true);
expect(apiGet).toHaveBeenCalledWith(API_PATHS.services.list, {
searchParams: { isActive: true },
});
expect(services[0]).toMatchObject({
id: 7,
name: "HVAC inspection",
completionDocTemplate: { id: 4, name: "HVAC checklist" },
supportedWorkOrderTypes: ["PM", "Reactive"],
});
});
it("creates, updates, and deactivates through the registry routes", async () => {
const input: ServiceInput = {
name: "Boiler inspection",
trade: "HVAC",
iconKey: "droplets",
requiresCompletionDocument: false,
completionDocTemplateId: null,
supportedWorkOrderTypes: ["PM"],
};
apiPost.mockResolvedValue({ id: 8, ...input, isActive: true, supportedWorkOrderTypes: [2] });
apiPut.mockResolvedValue({ id: 8, ...input, isActive: true, supportedWorkOrderTypes: [2] });
await servicesApi.create(input);
await servicesApi.update(8, input);
await servicesApi.deactivate(8);
expect(apiPost).toHaveBeenNthCalledWith(1, API_PATHS.services.list, {
...input,
supportedWorkOrderTypes: [2],
});
expect(apiPost).toHaveBeenNthCalledWith(2, API_PATHS.services.deactivate(8));
expect(apiPut).toHaveBeenCalledWith(API_PATHS.services.byId(8), {
...input,
supportedWorkOrderTypes: [2],
});
});
});