Home Manual Reference Source Test

tests/feedback.test.js

  1. import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
  2. import { MOCKS, STUBS } from "./api.js";
  3. const { downloadBlob } = await MOCKS.download;
  4. const { submitFeedback } = await import("../src/view/feedback.js");
  5. /** @test {submitFeedback} */
  6. describe("submitFeedback", () => {
  7. let dialog;
  8. beforeEach(() => {
  9. downloadBlob.mockReturnValue("stem-fake");
  10. dialog = STUBS.dialog();
  11. STUBS.formData();
  12. STUBS.windowFeedback();
  13. });
  14. afterEach(() => vi.unstubAllGlobals());
  15. /** @test {submitFeedback} */
  16. it("downloads MIDI, builds a mailto URL from form data and parameters and closes the dialog; falls back to '-' for missing form fields", async () => {
  17. STUBS.formData({
  18. occupation: "Musician",
  19. experience: "4",
  20. rating: "5",
  21. feedback: "Great!",
  22. human: "on",
  23. reuse: "on",
  24. });
  25. const parameters = { key: 0 };
  26. const tri = {
  27. aspects: { rhythm: "positive", harmony: "neutral" },
  28. };
  29. const chordProgression = [
  30. { label: "A", chords: ["I", "IV", "V", "I"] },
  31. { label: "C" },
  32. ];
  33. submitFeedback(dialog, "MIDI-B64", parameters, {
  34. ...tri,
  35. chordProgression,
  36. });
  37. expect(downloadBlob).toHaveBeenCalledWith("MIDI-B64");
  38. expect(dialog.close).toHaveBeenCalledOnce();
  39. const [url] = window.open.mock.calls[0];
  40. expect(url).toContain("mailto:medinger@b-tu.de");
  41. expect(url).toContain("subject=stem-fake");
  42. const decoded = decodeURIComponent(url);
  43. expect(decoded).toContain("Occupation: Musician");
  44. expect(decoded).toContain("Musical experience (1-5): 4");
  45. expect(decoded).toContain("Overall impression (1-5): 5");
  46. expect(decoded).toContain("Comments: Great!");
  47. expect(decoded).toContain(
  48. 'Aspect ratings: {"rhythm":"positive","harmony":"neutral"}',
  49. );
  50. expect(decoded).toContain(
  51. 'Further questions: {"human":"yes","reuse":"yes"}',
  52. );
  53. expect(decoded).toContain(JSON.stringify(parameters, null, 2));
  54. expect(decoded).toContain("Chord progression:");
  55. expect(decoded).toContain("A: I -> IV -> V -> I");
  56. expect(decoded).toContain("C: ");
  57. window.open.mockClear();
  58. STUBS.formData();
  59. submitFeedback(dialog, "MIDI-B64", {}, { aspects: {} });
  60. const [fallbackUrl] = window.open.mock.calls[0];
  61. const fallback = decodeURIComponent(fallbackUrl);
  62. expect(fallback).toContain("Occupation: -");
  63. expect(fallback).toContain("Musical experience (1-5): -");
  64. expect(fallback).toContain("Overall impression (1-5): -");
  65. expect(fallback).toContain("Comments: -");
  66. expect(fallback).not.toContain("Chord progression:");
  67. });
  68. });