tests/download.test.js
- import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
- import { downloadBlob } from "../src/view/download.js";
- import { STUBS } from "./api.js";
- describe("downloadBlob", () => {
- let anchor;
- beforeEach(() => {
- anchor = STUBS.anchor();
- STUBS.url();
- STUBS.documentDownload(anchor);
- });
- afterEach(() => vi.unstubAllGlobals());
-
- it("triggers an anchor download with stem return; accepts a direct Blob or decodes Base64 to audio/midi with a default UTC-timestamp filename", () => {
- expect(downloadBlob(new Blob(["x"]), "output.mid")).toBe("output");
- expect(anchor.href).toBe("blob:mock");
- expect(anchor.download).toBe("output.mid");
- expect(downloadBlob(btoa("hello"))).toMatch(
- /^\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}$/,
- );
- const blob = URL.createObjectURL.mock.calls[1][0];
- expect(blob).toBeInstanceOf(Blob);
- expect(blob.type).toBe("audio/midi");
- expect(anchor.click).toHaveBeenCalledTimes(2);
- expect(anchor.remove).toHaveBeenCalledTimes(2);
- });
- });