shoc-frontend-new/src/test/domain/work-orders/utils/run-sequential-uploads.test.ts
Arthur Bassi d72be8c505 fix(work-orders): use local calendar day for slide-over completedDate
[recover] remove malicious eslint payload (was 6a278c01)
2026-08-10 17:12:13 -03:00

65 lines
2 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { runSequentialUploads } from "@/domain/work-orders/utils/run-sequential-uploads";
describe("runSequentialUploads", () => {
it("keeps pending true across the full batch; second upload starts only after the first settles", async () => {
const pendingLog: boolean[] = [];
const setPending = (pending: boolean) => {
pendingLog.push(pending);
};
let resolveFirst: (() => void) | undefined;
let resolveSecond: (() => void) | undefined;
const first = new Promise<void>((resolve) => {
resolveFirst = resolve;
});
const second = new Promise<void>((resolve) => {
resolveSecond = resolve;
});
const upload = vi
.fn()
.mockImplementationOnce(() => first)
.mockImplementationOnce(() => second);
const done = runSequentialUploads(["large.bin", "small.bin"], upload, setPending);
expect(pendingLog).toEqual([true]);
expect(upload).toHaveBeenCalledTimes(1);
expect(upload).toHaveBeenCalledWith("large.bin");
// First file still in flight — pending must stay true.
await Promise.resolve();
expect(pendingLog).toEqual([true]);
expect(upload).toHaveBeenCalledTimes(1);
resolveFirst?.();
await Promise.resolve();
await Promise.resolve();
expect(upload).toHaveBeenCalledTimes(2);
expect(upload).toHaveBeenNthCalledWith(2, "small.bin");
expect(pendingLog).toEqual([true]);
resolveSecond?.();
await done;
expect(pendingLog).toEqual([true, false]);
});
it("clears pending when a middle upload rejects", async () => {
const setPending = vi.fn();
const upload = vi
.fn()
.mockResolvedValueOnce(undefined)
.mockRejectedValueOnce(new Error("upload failed"));
await expect(runSequentialUploads(["a", "b", "c"], upload, setPending)).rejects.toThrow(
"upload failed",
);
expect(setPending.mock.calls.map((call) => call[0])).toEqual([true, false]);
expect(upload).toHaveBeenCalledTimes(2);
});
});