Home Manual Reference Source Test

tests/download.test.js

  1. import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
  2. import { downloadBlob } from "../src/view/download.js";
  3. import { STUBS } from "./api.js";
  4. /** @test {downloadBlob} */
  5. describe("downloadBlob", () => {
  6. let anchor;
  7. beforeEach(() => {
  8. anchor = STUBS.anchor();
  9. STUBS.url();
  10. STUBS.documentDownload(anchor);
  11. });
  12. afterEach(() => vi.unstubAllGlobals());
  13. /** @test {downloadBlob} */
  14. it("triggers an anchor download with stem return; accepts a direct Blob or decodes Base64 to audio/midi with a default UTC-timestamp filename", () => {
  15. expect(downloadBlob(new Blob(["x"]), "output.mid")).toBe("output");
  16. expect(anchor.href).toBe("blob:mock");
  17. expect(anchor.download).toBe("output.mid");
  18. expect(downloadBlob(btoa("hello"))).toMatch(
  19. /^\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}$/,
  20. );
  21. const blob = URL.createObjectURL.mock.calls[1][0];
  22. expect(blob).toBeInstanceOf(Blob);
  23. expect(blob.type).toBe("audio/midi");
  24. expect(anchor.click).toHaveBeenCalledTimes(2);
  25. expect(anchor.remove).toHaveBeenCalledTimes(2);
  26. });
  27. });