129 lines
3.1 KiB
JavaScript
129 lines
3.1 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 {
|
|
fetchSuperUserBookingCount,
|
|
fetchSuperUserBookingCounts,
|
|
} from "@/components/models/navigation/items/superUserBookingCount.js";
|
|
|
|
describe("fetchSuperUserBookingCounts", () => {
|
|
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
|
|
beforeEach(() => {
|
|
authenticatedRequestMock.mockReset();
|
|
consoleErrorSpy.mockClear();
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it("returns past, current, and future pending booking totals across departments", async () => {
|
|
authenticatedRequestMock.mockResolvedValue({
|
|
data: {
|
|
data: {
|
|
past: 2,
|
|
current: 2,
|
|
future: 2,
|
|
},
|
|
},
|
|
});
|
|
|
|
await expect(fetchSuperUserBookingCounts()).resolves.toEqual({
|
|
past: 2,
|
|
current: 2,
|
|
future: 2,
|
|
});
|
|
|
|
expect(authenticatedRequestMock).toHaveBeenCalledWith("/order-bookings/counts", "GET", {});
|
|
});
|
|
|
|
it("normalizes the cached endpoint response into grouped counts", async () => {
|
|
authenticatedRequestMock.mockResolvedValue({
|
|
data: {
|
|
data: {
|
|
past: "31",
|
|
current: 51,
|
|
future: "21",
|
|
},
|
|
},
|
|
});
|
|
|
|
await expect(fetchSuperUserBookingCounts()).resolves.toEqual({
|
|
past: 31,
|
|
current: 51,
|
|
future: 21,
|
|
});
|
|
});
|
|
|
|
it("treats missing cached count fields as zero", async () => {
|
|
authenticatedRequestMock.mockResolvedValue({
|
|
data: {
|
|
data: {
|
|
current: 1,
|
|
},
|
|
},
|
|
});
|
|
|
|
await expect(fetchSuperUserBookingCounts()).resolves.toEqual({
|
|
past: 0,
|
|
current: 1,
|
|
future: 0,
|
|
});
|
|
});
|
|
|
|
it("returns 0 counts when the order-bookings request fails", async () => {
|
|
authenticatedRequestMock.mockRejectedValue(new Error("Request failed"));
|
|
|
|
await expect(fetchSuperUserBookingCounts()).resolves.toEqual({
|
|
past: 0,
|
|
current: 0,
|
|
future: 0,
|
|
});
|
|
await expect(fetchSuperUserBookingCount()).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(fetchSuperUserBookingCounts()).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: 3,
|
|
future: 1,
|
|
},
|
|
},
|
|
});
|
|
|
|
await expect(fetchSuperUserBookingCount()).resolves.toBe(3);
|
|
});
|
|
});
|