124 lines
3.9 KiB
JavaScript
124 lines
3.9 KiB
JavaScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
vi.mock("sweetalert2", () => ({
|
|
default: {
|
|
fire: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
import Swal from "sweetalert2";
|
|
import {
|
|
buildMultiMonthInvoiceContext,
|
|
MULTI_MONTH_INVOICE_ACTION,
|
|
promptMultiMonthInvoiceWarning,
|
|
shouldWarnAboutMultiMonthInvoice,
|
|
} from "@/services/invoiceMonthSplitWarning.js";
|
|
|
|
describe("invoice month split warning", () => {
|
|
beforeEach(() => {
|
|
Swal.fire.mockReset();
|
|
});
|
|
|
|
it("builds a scoped multi-month context without timezone shifting date strings", () => {
|
|
const context = buildMultiMonthInvoiceContext([
|
|
{ id: 1, created_at: "2026-03-31 23:30:00", invoice_collection_id: 501 },
|
|
{ id: 2, created_at: "2026-04-01T00:30:00.000Z", invoice_collection_id: "501" },
|
|
{ id: 3, created_at: "2026-04-03 09:00:00", invoice_collection_id: 502 },
|
|
{ id: 4, created_at: "invalid", invoice_collection_id: null },
|
|
]);
|
|
|
|
expect(context).toEqual({
|
|
months: ["2026-03", "2026-04"],
|
|
dateFrom: "2026-03-31",
|
|
dateTo: "2026-04-03",
|
|
invoiceCollectionIds: [501, 502],
|
|
});
|
|
expect(shouldWarnAboutMultiMonthInvoice(context)).toBe(true);
|
|
});
|
|
|
|
it("continues without a modal for single-month selections", async () => {
|
|
const action = await promptMultiMonthInvoiceWarning({
|
|
context: buildMultiMonthInvoiceContext([
|
|
{ created_at: "2026-04-01 10:00:00", invoice_collection_id: 501 },
|
|
{ created_at: "2026-04-02 10:00:00", invoice_collection_id: 501 },
|
|
]),
|
|
splitByMonth: vi.fn(),
|
|
});
|
|
|
|
expect(action).toBe(MULTI_MONTH_INVOICE_ACTION.CONTINUE);
|
|
expect(Swal.fire).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("splits selected invoice collections when the warning is confirmed", async () => {
|
|
const splitByMonth = vi.fn().mockResolvedValue({
|
|
data: {
|
|
data: {
|
|
processed_count: 2,
|
|
changed_count: 1,
|
|
skipped_count: 1,
|
|
},
|
|
},
|
|
});
|
|
Swal.fire.mockResolvedValueOnce({ isConfirmed: true }).mockResolvedValueOnce({ isConfirmed: true });
|
|
|
|
const action = await promptMultiMonthInvoiceWarning({
|
|
context: buildMultiMonthInvoiceContext([
|
|
{ created_at: "2026-03-20 10:00:00", invoice_collection_id: 7001 },
|
|
{ created_at: "2026-04-02 10:00:00", invoice_collection_id: 7001 },
|
|
]),
|
|
splitByMonth,
|
|
});
|
|
|
|
expect(action).toBe(MULTI_MONTH_INVOICE_ACTION.SPLIT);
|
|
expect(splitByMonth).toHaveBeenCalledWith("2026-03-20", "2026-04-02", {
|
|
invoiceCollectionIds: [7001],
|
|
preview: false,
|
|
});
|
|
expect(Swal.fire).toHaveBeenNthCalledWith(
|
|
1,
|
|
expect.objectContaining({
|
|
icon: "warning",
|
|
showDenyButton: true,
|
|
})
|
|
);
|
|
expect(Swal.fire).toHaveBeenNthCalledWith(
|
|
2,
|
|
expect.objectContaining({
|
|
icon: "success",
|
|
})
|
|
);
|
|
});
|
|
|
|
it("continues invoicing together when the warning deny button is selected", async () => {
|
|
const splitByMonth = vi.fn();
|
|
Swal.fire.mockResolvedValueOnce({ isDenied: true });
|
|
|
|
const action = await promptMultiMonthInvoiceWarning({
|
|
context: buildMultiMonthInvoiceContext([
|
|
{ created_at: "2026-03-20 10:00:00", invoice_collection_id: 7001 },
|
|
{ created_at: "2026-04-02 10:00:00", invoice_collection_id: 7001 },
|
|
]),
|
|
splitByMonth,
|
|
});
|
|
|
|
expect(action).toBe(MULTI_MONTH_INVOICE_ACTION.CONTINUE);
|
|
expect(splitByMonth).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("cancels invoicing when the warning is dismissed", async () => {
|
|
const splitByMonth = vi.fn();
|
|
Swal.fire.mockResolvedValueOnce({ isDismissed: true });
|
|
|
|
const action = await promptMultiMonthInvoiceWarning({
|
|
context: buildMultiMonthInvoiceContext([
|
|
{ created_at: "2026-03-20 10:00:00", invoice_collection_id: 7001 },
|
|
{ created_at: "2026-04-02 10:00:00", invoice_collection_id: 7001 },
|
|
]),
|
|
splitByMonth,
|
|
});
|
|
|
|
expect(action).toBe(MULTI_MONTH_INVOICE_ACTION.CANCEL);
|
|
expect(splitByMonth).not.toHaveBeenCalled();
|
|
});
|
|
});
|