tests/feedback.test.js
- import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
- import { MOCKS, STUBS } from "./api.js";
- const { downloadBlob } = await MOCKS.download;
- const { submitFeedback } = await import("../src/view/feedback.js");
- describe("submitFeedback", () => {
- let dialog;
- beforeEach(() => {
- downloadBlob.mockReturnValue("stem-fake");
- dialog = STUBS.dialog();
- STUBS.formData();
- STUBS.windowFeedback();
- });
- afterEach(() => vi.unstubAllGlobals());
-
- it("downloads MIDI, builds a mailto URL from form data and parameters and closes the dialog; falls back to '-' for missing form fields", async () => {
- STUBS.formData({
- occupation: "Musician",
- experience: "4",
- rating: "5",
- feedback: "Great!",
- human: "on",
- reuse: "on",
- });
- const parameters = { key: 0 };
- const tri = {
- aspects: { rhythm: "positive", harmony: "neutral" },
- };
- const chordProgression = [
- { label: "A", chords: ["I", "IV", "V", "I"] },
- { label: "C" },
- ];
- submitFeedback(dialog, "MIDI-B64", parameters, {
- ...tri,
- chordProgression,
- });
- expect(downloadBlob).toHaveBeenCalledWith("MIDI-B64");
- expect(dialog.close).toHaveBeenCalledOnce();
- const [url] = window.open.mock.calls[0];
- expect(url).toContain("mailto:medinger@b-tu.de");
- expect(url).toContain("subject=stem-fake");
- const decoded = decodeURIComponent(url);
- expect(decoded).toContain("Occupation: Musician");
- expect(decoded).toContain("Musical experience (1-5): 4");
- expect(decoded).toContain("Overall impression (1-5): 5");
- expect(decoded).toContain("Comments: Great!");
- expect(decoded).toContain(
- 'Aspect ratings: {"rhythm":"positive","harmony":"neutral"}',
- );
- expect(decoded).toContain(
- 'Further questions: {"human":"yes","reuse":"yes"}',
- );
- expect(decoded).toContain(JSON.stringify(parameters, null, 2));
- expect(decoded).toContain("Chord progression:");
- expect(decoded).toContain("A: I -> IV -> V -> I");
- expect(decoded).toContain("C: ");
- window.open.mockClear();
- STUBS.formData();
- submitFeedback(dialog, "MIDI-B64", {}, { aspects: {} });
- const [fallbackUrl] = window.open.mock.calls[0];
- const fallback = decodeURIComponent(fallbackUrl);
- expect(fallback).toContain("Occupation: -");
- expect(fallback).toContain("Musical experience (1-5): -");
- expect(fallback).toContain("Overall impression (1-5): -");
- expect(fallback).toContain("Comments: -");
- expect(fallback).not.toContain("Chord progression:");
- });
- });