fix(tests): add DOMMatrix polyfill for react-pdf/jsdom compatibility

Required for react-pdf v10 in test environment.
Refactor polyfills to use named variables to avoid TS class name conflicts.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
Woody 2026-04-24 17:53:39 +08:00
parent f07e14aafd
commit 95f7cb04bf
1 changed files with 14 additions and 9 deletions

View File

@ -1,27 +1,32 @@
import '@testing-library/jest-dom' import '@testing-library/jest-dom'
global.ResizeObserver = class ResizeObserver { const MockResizeObserver = class {
observe() {} observe() {}
unobserve() {} unobserve() {}
disconnect() {} disconnect() {}
} }
;(globalThis as any).ResizeObserver = MockResizeObserver
global.DOMRect = class DOMRect { const MockDOMRect = class {
x: number; y: number; width: number; height: number; top: number; right: number; bottom: number; left: number x = 0; y = 0; width = 0; height = 0; top = 0; right = 0; bottom = 0; left = 0
constructor(x = 0, y = 0, width = 0, height = 0) { constructor(x = 0, y = 0, width = 0, height = 0) {
this.x = x; this.y = y; this.width = width; this.height = height this.x = x; this.y = y; this.width = width; this.height = height
this.top = y; this.right = x + width; this.bottom = y + height; this.left = x this.top = y; this.right = x + width; this.bottom = y + height; this.left = x
} }
static fromRect() { return new DOMRect() } static fromRect() { return new (MockDOMRect as any)() }
toJSON() { return { x: this.x, y: this.y, width: this.width, height: this.height, top: this.top, right: this.right, bottom: this.bottom, left: this.left } } toJSON() { return { x: this.x, y: this.y, width: this.width, height: this.height, top: this.top, right: this.right, bottom: this.bottom, left: this.left } }
} }
;(globalThis as any).DOMRect = MockDOMRect
global.DOMMatrix = class DOMMatrix { const MockDOMMatrix = class {
a = 1; b = 0; c = 0; d = 1; e = 0; f = 0 a = 1; b = 0; c = 0; d = 1; e = 0; f = 0
is2D = true is2D = true
isIdentity = true isIdentity = true
static fromMatrix() { return new DOMMatrix() } static fromMatrix() { return new (MockDOMMatrix as any)() }
multiply() { return new DOMMatrix() } static fromFloat32Array() { return new (MockDOMMatrix as any)() }
translate() { return new DOMMatrix() } static fromFloat64Array() { return new (MockDOMMatrix as any)() }
scale() { return new DOMMatrix() } multiply() { return new (MockDOMMatrix as any)() }
translate() { return new (MockDOMMatrix as any)() }
scale() { return new (MockDOMMatrix as any)() }
} }
;(globalThis as any).DOMMatrix = MockDOMMatrix