180 lines
3.9 KiB
JavaScript
180 lines
3.9 KiB
JavaScript
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const authenticatedRequestMock = vi.hoisted(() => vi.fn());
|
|
|
|
vi.mock("@/components/session/authenticatedRequest.vue", () => ({
|
|
authenticatedRequest: authenticatedRequestMock,
|
|
}));
|
|
|
|
import {
|
|
fetchDepartmentOrderBookingCount,
|
|
fetchDepartmentOrderBookingCounts,
|
|
} from "@/components/models/navigation/items/adminBookingCount.js";
|
|
|
|
describe("fetchDepartmentOrderBookingCounts", () => {
|
|
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
|
|
beforeEach(() => {
|
|
authenticatedRequestMock.mockReset();
|
|
consoleErrorSpy.mockClear();
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it("returns past, current, and future pending booking totals for the department", async () => {
|
|
authenticatedRequestMock.mockResolvedValue({
|
|
data: {
|
|
data: {
|
|
past: 3,
|
|
current: 4,
|
|
future: 2,
|
|
},
|
|
},
|
|
});
|
|
|
|
await expect(
|
|
fetchDepartmentOrderBookingCounts({
|
|
departmentId: 12,
|
|
})
|
|
).resolves.toEqual({
|
|
past: 3,
|
|
current: 4,
|
|
future: 2,
|
|
});
|
|
|
|
expect(authenticatedRequestMock).toHaveBeenCalledWith("/order-bookings/counts", "GET", {
|
|
department: 12,
|
|
});
|
|
});
|
|
|
|
it("returns 0 counts when the department is invalid", async () => {
|
|
await expect(
|
|
fetchDepartmentOrderBookingCounts({
|
|
departmentId: null,
|
|
})
|
|
).resolves.toEqual({
|
|
past: 0,
|
|
current: 0,
|
|
future: 0,
|
|
});
|
|
|
|
await expect(
|
|
fetchDepartmentOrderBookingCount({
|
|
departmentId: null,
|
|
})
|
|
).resolves.toBe(0);
|
|
|
|
expect(authenticatedRequestMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("normalizes the cached endpoint response into booking counts", async () => {
|
|
authenticatedRequestMock.mockResolvedValue({
|
|
data: {
|
|
data: {
|
|
past: "41",
|
|
current: 37,
|
|
future: "26",
|
|
},
|
|
},
|
|
});
|
|
|
|
await expect(
|
|
fetchDepartmentOrderBookingCounts({
|
|
departmentId: 12,
|
|
})
|
|
).resolves.toEqual({
|
|
past: 41,
|
|
current: 37,
|
|
future: 26,
|
|
});
|
|
});
|
|
|
|
it("treats missing cached count fields as zero", async () => {
|
|
authenticatedRequestMock.mockResolvedValue({
|
|
data: {
|
|
data: {
|
|
current: 5,
|
|
},
|
|
},
|
|
});
|
|
|
|
await expect(
|
|
fetchDepartmentOrderBookingCounts({
|
|
departmentId: 12,
|
|
})
|
|
).resolves.toEqual({
|
|
past: 0,
|
|
current: 5,
|
|
future: 0,
|
|
});
|
|
});
|
|
|
|
it("returns 0 counts when the order-bookings request fails", async () => {
|
|
authenticatedRequestMock.mockRejectedValue(new Error("Request failed"));
|
|
|
|
await expect(
|
|
fetchDepartmentOrderBookingCounts({
|
|
departmentId: 12,
|
|
})
|
|
).resolves.toEqual({
|
|
past: 0,
|
|
current: 0,
|
|
future: 0,
|
|
});
|
|
|
|
await expect(
|
|
fetchDepartmentOrderBookingCount({
|
|
departmentId: 12,
|
|
})
|
|
).resolves.toBe(0);
|
|
|
|
expect(consoleErrorSpy).toHaveBeenCalled();
|
|
});
|
|
|
|
it("quietly returns 0 counts when the order-bookings request is denied", async () => {
|
|
authenticatedRequestMock.mockRejectedValue({
|
|
response: {
|
|
status: 403,
|
|
data: {
|
|
data: {
|
|
message: "Missing permission(s)",
|
|
permissions: ["list_own_bookings", "list_bookings"],
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
await expect(
|
|
fetchDepartmentOrderBookingCounts({
|
|
departmentId: 12,
|
|
})
|
|
).resolves.toEqual({
|
|
past: 0,
|
|
current: 0,
|
|
future: 0,
|
|
});
|
|
|
|
expect(consoleErrorSpy).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("returns the current booking count from the grouped counts helper", async () => {
|
|
authenticatedRequestMock.mockResolvedValue({
|
|
data: {
|
|
data: {
|
|
past: 1,
|
|
current: 5,
|
|
future: 1,
|
|
},
|
|
},
|
|
});
|
|
|
|
await expect(
|
|
fetchDepartmentOrderBookingCount({
|
|
departmentId: 12,
|
|
})
|
|
).resolves.toBe(5);
|
|
});
|
|
});
|