- Introduced `.prettierrc.json` to enforce consistent code formatting across the project. - Updated unit and e2e test files to address formatting issues, improve readability, and ensure alignment with the new Prettier configuration.
115 lines
3.2 KiB
JavaScript
115 lines
3.2 KiB
JavaScript
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
request: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("@/components/session/token/SessionUser.vue", () => ({
|
|
SessionUser: {
|
|
request: mocks.request,
|
|
},
|
|
}));
|
|
|
|
import {
|
|
__resetDepartmentOutsideGateCapabilitiesCacheForTests,
|
|
getDepartmentOutsideGateCapabilities,
|
|
} from "@/composables/departmentOutsideGateCapabilities.js";
|
|
|
|
describe("department outside gate capabilities", () => {
|
|
let consoleErrorSpy;
|
|
|
|
beforeEach(() => {
|
|
mocks.request.mockReset();
|
|
__resetDepartmentOutsideGateCapabilitiesCacheForTests();
|
|
consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
});
|
|
|
|
afterEach(() => {
|
|
consoleErrorSpy.mockRestore();
|
|
});
|
|
|
|
it("detects outside entrance and exit gates for the selected department", async () => {
|
|
mocks.request.mockResolvedValue({
|
|
data: {
|
|
data: [
|
|
{ id: 1, department: 12, is_entrance: true, is_exit: false },
|
|
{ id: 2, department: 12, is_entrance: false, is_exit: 1 },
|
|
{ id: 3, department: 99, is_entrance: true, is_exit: true },
|
|
],
|
|
meta: {
|
|
pagination: {
|
|
page: 1,
|
|
total_pages: 1,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
const capabilities = await getDepartmentOutsideGateCapabilities("12");
|
|
|
|
expect(capabilities).toEqual({
|
|
hasOutsideEntranceGate: true,
|
|
hasOutsideExitGate: true,
|
|
});
|
|
expect(mocks.request).toHaveBeenCalledWith("/department/gates", "GET", {
|
|
page: 1,
|
|
limit: 100,
|
|
order: "id:asc",
|
|
filters: "department:12",
|
|
});
|
|
});
|
|
|
|
it("normalizes truthy/falsy gate flags before computing capabilities", async () => {
|
|
mocks.request.mockResolvedValue({
|
|
data: {
|
|
data: [
|
|
{ id: 10, department: 7, is_entrance: "yes", is_exit: "off" },
|
|
{ id: 11, department: 7, is_entrance: "0", is_exit: "true" },
|
|
],
|
|
meta: {
|
|
pagination: {
|
|
page: 1,
|
|
total_pages: 1,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
const capabilities = await getDepartmentOutsideGateCapabilities(7);
|
|
|
|
expect(capabilities).toEqual({
|
|
hasOutsideEntranceGate: true,
|
|
hasOutsideExitGate: true,
|
|
});
|
|
});
|
|
|
|
it("returns disabled capabilities for invalid or missing department ids", async () => {
|
|
await expect(getDepartmentOutsideGateCapabilities(null)).resolves.toEqual({
|
|
hasOutsideEntranceGate: false,
|
|
hasOutsideExitGate: false,
|
|
});
|
|
await expect(getDepartmentOutsideGateCapabilities("abc")).resolves.toEqual({
|
|
hasOutsideEntranceGate: false,
|
|
hasOutsideExitGate: false,
|
|
});
|
|
await expect(getDepartmentOutsideGateCapabilities(0)).resolves.toEqual({
|
|
hasOutsideEntranceGate: false,
|
|
hasOutsideExitGate: false,
|
|
});
|
|
|
|
expect(mocks.request).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("falls back to hidden outside gates when loading department gates fails", async () => {
|
|
mocks.request.mockRejectedValue(new Error("department gates unavailable"));
|
|
|
|
const capabilities = await getDepartmentOutsideGateCapabilities(12);
|
|
|
|
expect(capabilities).toEqual({
|
|
hasOutsideEntranceGate: false,
|
|
hasOutsideExitGate: false,
|
|
});
|
|
expect(consoleErrorSpy).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|