Files
pleno-vue/tests/unit/superuser-draft-count.spec.js
T

144 lines
3.4 KiB
JavaScript

import { beforeEach, describe, expect, it, vi } from "vitest";
const authenticatedRequestMock = vi.hoisted(() => vi.fn());
vi.mock("@/components/session/authenticatedRequest.vue", () => ({
authenticatedRequest: authenticatedRequestMock,
}));
import { fetchSuperUserDraftCount } from "@/components/models/navigation/items/superUserDraftCount.js";
describe("fetchSuperUserDraftCount", () => {
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
beforeEach(() => {
authenticatedRequestMock.mockReset();
consoleErrorSpy.mockClear();
});
it("returns pagination total when the fast draft-count request returns a row", async () => {
authenticatedRequestMock.mockResolvedValue({
data: {
data: [{ id: 1 }],
meta: {
pagination: {
total: 5,
},
},
},
});
await expect(
fetchSuperUserDraftCount({
customerNumber: 6001,
})
).resolves.toBe(5);
expect(authenticatedRequestMock).toHaveBeenCalledTimes(1);
expect(authenticatedRequestMock).toHaveBeenCalledWith("/orders", "GET", {
filters: "customer_id:6001",
page: 1,
limit: 1,
search: "",
order: "id:DESC",
});
});
it("falls back to a full page request when the fast request reports a stale total without rows", async () => {
authenticatedRequestMock
.mockResolvedValueOnce({
data: {
data: [],
meta: {
pagination: {
total: 5,
},
},
},
})
.mockResolvedValueOnce({
data: {
data: [{ id: 11 }, { id: 12 }],
meta: {
pagination: {
total: 2,
},
},
},
});
await expect(
fetchSuperUserDraftCount({
customerNumber: 6001,
})
).resolves.toBe(2);
expect(authenticatedRequestMock).toHaveBeenNthCalledWith(1, "/orders", "GET", {
filters: "customer_id:6001",
page: 1,
limit: 1,
search: "",
order: "id:DESC",
});
expect(authenticatedRequestMock).toHaveBeenNthCalledWith(2, "/orders", "GET", {
filters: "customer_id:6001",
page: 1,
limit: 100,
search: "",
order: "id:DESC",
});
});
it("returns 0 when the fallback request also has no rows", async () => {
authenticatedRequestMock
.mockResolvedValueOnce({
data: {
data: [],
meta: {
pagination: {
total: 1,
},
},
},
})
.mockResolvedValueOnce({
data: {
data: [],
meta: {
pagination: {
total: 0,
},
},
},
});
await expect(
fetchSuperUserDraftCount({
customerNumber: 6001,
})
).resolves.toBe(0);
});
it("returns 0 when the customer number is invalid", async () => {
await expect(
fetchSuperUserDraftCount({
customerNumber: 0,
})
).resolves.toBe(0);
expect(authenticatedRequestMock).not.toHaveBeenCalled();
});
it("returns 0 when the orders request fails", async () => {
authenticatedRequestMock.mockRejectedValue(new Error("Request failed"));
await expect(
fetchSuperUserDraftCount({
customerNumber: 6001,
})
).resolves.toBe(0);
expect(consoleErrorSpy).toHaveBeenCalled();
});
});