summaryrefslogtreecommitdiff
path: root/frontend/src/test-setup.ts
blob: cdd60719ef9b12f1570f06d7f39eb223b5b70abc (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

// 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;
}