summaryrefslogtreecommitdiff
path: root/frontend/src/test-setup.ts
blob: 10f95680aa6a131f285c6ca90d698784151fea69 (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
45
46
47
48
49
50
51
52

// layerchart reads window.matchMedia (settings context, dark-mode aware
// styling) in jsdom where no backend exists. Narrow shim for media queries
// used by chart settings only.
if (typeof window !== 'undefined' && !window.matchMedia) {
  window.matchMedia = (query: string) => ({
    matches: false,
    media: query,
    onchange: null,
    addEventListener: () => {},
    removeEventListener: () => {},
    addListener: () => {},
    removeListener: () => {},
    dispatchEvent: () => false
  });
}

// jsdom does not implement Range.getClientRects, which CodeMirror's
// measurement layer calls for drawing decorations (diagnostics markers).
// Without it the suite emits an unhandled
// `textRange(...).getClientRects is not a function` TypeError even though
// assertions pass. Narrow geometry shim ONLY for the missing API; it returns
// one empty rect, from which CodeMirror derives null geometry — no other
// errors are suppressed.
if (typeof Range !== 'undefined' && !Range.prototype.getClientRects) {
  const EMPTY_RECT = { left: 0, right: 0, top: 0, bottom: 0, width: 0, height: 0, x: 0, y: 0, toJSON() { return {}; } };
  Range.prototype.getClientRects = function getClientRects() {
    return [EMPTY_RECT] as unknown as DOMRectList;
  };
}

// jsdom ships no canvas backend: HTMLCanvasElement.prototype.getContext exists
// but throws "Not implemented". Charts render with ECharts' SVG renderer; the
// only canvas user in tests is zrender's text-metrics helper (measureText
// paints into a hidden canvas). Replace getContext ONLY when the native
// implementation is the throwing stub, and provide just the measurement
// approximation — SVG chart rendering itself (the part regressions inspect)
// is untouched and fully real.
if (typeof HTMLCanvasElement !== 'undefined') {
    HTMLCanvasElement.prototype.getContext = function getContext(this: HTMLCanvasElement, contextId: string) {
      if (contextId === '2d') {
        return {
          canvas: this,
          font: '',
          measureText(text: string) {
            return { width: String(text).length * 7, actualBoundingBoxAscent: 7, actualBoundingBoxDescent: 1 };
          }
        } as unknown as CanvasRenderingContext2D;
      }
      return null;
    } as unknown as typeof HTMLCanvasElement.prototype.getContext;
}