summaryrefslogtreecommitdiffhomepage
path: root/vitest-setup.ts
blob: a0ace279d3992558bb748adc3593f3182f575316 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import "@testing-library/jest-dom/vitest";
import "fake-indexeddb/auto";

// jsdom implements neither Element.scrollTo nor ResizeObserver; the smart-scroll
// controller uses both against the real transcript element when App mounts. Stub
// the outermost edges so component tests can render without throwing.
if (typeof Element !== "undefined" && typeof Element.prototype.scrollTo !== "function") {
  Element.prototype.scrollTo = () => {};
}
if (typeof globalThis.ResizeObserver === "undefined") {
  globalThis.ResizeObserver = class {
    observe(): void {}
    unobserve(): void {}
    disconnect(): void {}
  };
}
// jsdom implements no Element.animate (Web Animations API — svelte transitions
// and the grid's FLIP reflow use it); stub a no-op so components render in tests.
if (typeof Element !== "undefined" && typeof Element.prototype.animate !== "function") {
  Element.prototype.animate = () => {
    const noop = () => {};
    return {
      cancel: noop,
      finish: noop,
      pause: noop,
      play: noop,
      onfinish: null,
    } as unknown as Animation;
  };
}
// jsdom implements no PointerEvent constructor — testing-library's
// fireEvent.pointerDown falls back to a plain Event and drops the pointer
// properties (clientX/pointerType/…). Polyfill over jsdom's MouseEvent (the
// drag/press logic is pointer-driven; a real browser always has PointerEvent).
if (typeof globalThis.PointerEvent === "undefined") {
  class PointerEventPolyfill extends MouseEvent {
    readonly pointerType: string;
    constructor(type: string, init: PointerEventInit = {}) {
      super(type, init);
      this.pointerType = init.pointerType ?? "";
    }
  }
  globalThis.PointerEvent = PointerEventPolyfill as unknown as typeof PointerEvent;
}