-
-
Notifications
You must be signed in to change notification settings - Fork 215
Expand file tree
/
Copy pathsetup.ts
More file actions
112 lines (104 loc) · 3.24 KB
/
setup.ts
File metadata and controls
112 lines (104 loc) · 3.24 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { expect, afterEach } from "vitest";
import { cleanup } from "@testing-library/react";
import * as matchers from "@testing-library/jest-dom/matchers";
expect.extend(matchers);
afterEach(() => {
cleanup();
});
const originalGetComputedStyle = window.getComputedStyle;
window.getComputedStyle = (elt: Element, pseudoElt?: string | null) => {
if (pseudoElt) {
return {
width: '0px',
height: '0px',
getPropertyValue: () => '',
} as unknown as CSSStyleDeclaration;
}
return originalGetComputedStyle(elt);
};
Object.defineProperty(window, "matchMedia", {
writable: true,
value: (query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: () => { /* mock */ },
removeListener: () => { /* mock */ },
addEventListener: () => { /* mock */ },
removeEventListener: () => { /* mock */ },
dispatchEvent: () => { return false; },
}),
});
interface MockCanvasContext {
fillStyle: string;
fillRect: () => void;
clearRect: () => void;
getImageData: () => { data: number[] };
putImageData: () => void;
createImageData: () => { data: number[] };
setTransform: () => void;
drawImage: () => void;
save: () => void;
restore: () => void;
beginPath: () => void;
moveTo: () => void;
lineTo: () => void;
closePath: () => void;
stroke: () => void;
fill: () => void;
translate: () => void;
scale: () => void;
rotate: () => void;
arc: () => void;
measureText: () => { width: number };
transform: () => void;
rect: () => void;
clip: () => void;
canvas: HTMLCanvasElement;
}
// capture original method so we can delegate for non-2d calls
const originalGetContext = HTMLCanvasElement.prototype.getContext as (
this: HTMLCanvasElement,
contextId: string,
options?: unknown
) => RenderingContext | null;
HTMLCanvasElement.prototype.getContext = function (
this: HTMLCanvasElement,
contextId: string,
options?: unknown
) {
if (contextId === '2d') {
const mockContext: MockCanvasContext = {
fillStyle: '',
fillRect: () => { /* mock */ },
clearRect: () => { /* mock */ },
getImageData: () => ({ data: [] }),
putImageData: () => { /* mock */ },
createImageData: () => ({ data: [] }),
setTransform: () => { /* mock */ },
drawImage: () => { /* mock */ },
save: () => { /* mock */ },
restore: () => { /* mock */ },
beginPath: () => { /* mock */ },
moveTo: () => { /* mock */ },
lineTo: () => { /* mock */ },
closePath: () => { /* mock */ },
stroke: () => { /* mock */ },
fill: () => { /* mock */ },
translate: () => { /* mock */ },
scale: () => { /* mock */ },
rotate: () => { /* mock */ },
arc: () => { /* mock */ },
measureText: () => ({ width: 0 }),
transform: () => { /* mock */ },
rect: () => { /* mock */ },
clip: () => { /* mock */ },
canvas: this,
};
// cast through unknown since the mock only implements a subset of methods
return mockContext as unknown as CanvasRenderingContext2D;
}
// for other context types, delegate to original
// for other context types, delegate to original
return originalGetContext.call(this, contextId, options);
} as unknown as typeof HTMLCanvasElement.prototype.getContext;