forked from sbrin/lopaka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-polyfills.ts
123 lines (115 loc) · 3.66 KB
/
test-polyfills.ts
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
113
114
115
116
117
118
119
120
121
122
123
import {vi} from 'vitest';
vi.mock('./src/draw/fonts/index.ts', () => ({
getFont: (name) => {
return {
name,
getSize: () => {
return 1;
}
};
},
loadFont: (platformFont) => {
return Promise.resolve({
name: platformFont.name,
getSize: () => {
return 1;
}
});
}
}));
const getContextMock = (type: string) => {
if (type !== '2d') {
throw Error(`Canvas context ${type} disallowed for tests`);
}
return new Proxy<CanvasRenderingContext2D>({} as CanvasRenderingContext2D, {
get: (c: CanvasRenderingContext2D, command: any) => {
if (command == 'createPattern') {
return () => ({
setTransform: vi.fn()
});
} else if (command == 'measureText') {
return () => 10;
} else if (command == 'backingStorePixelRatio') {
return 1;
} else if (command == 'getImageData') {
return () => ({
data: new Uint8ClampedArray(100 * 100 * 4)
});
}
return vi.fn();
},
set: (c: CanvasRenderingContext2D, property: any, value: any) => {
return true;
}
});
};
function mock(w: any, d: any) {
const createElement = d.createElement;
d.createElement = (...args: any[]) => {
const el = createElement.apply(d, args);
if (args[0] == 'canvas') {
Object.assign(el, {
toDataURL: () => 'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=',
// mock canvas 2d context
getContext: getContextMock
});
}
return el;
};
vi.stubGlobal('OffscreenCanvas', function () {
this.getContext = getContextMock;
});
vi.stubGlobal('DOMMatrix', function () {
(this.scale = vi.fn(() => this)), (this.translate = vi.fn(() => this));
});
vi.stubGlobal('Path2D', function () {
(this.rect = vi.fn(
// emulate actual rect implementation
(...props) =>
(this._path = [
{
props: {
x: props[0],
y: props[1],
width: props[2],
height: props[3]
}
}
])
)),
(this.toJSON = () => {
let rect = this._path[0].props;
return `M${rect.x} ${rect.y} h ${rect.width} v ${rect.height} h -${rect.width} Z`;
});
});
vi.stubGlobal('fetch', (...args: any[]) => {
throw Error(`Fetch is disallowed for tests ${JSON.stringify(args)}`);
});
w.requestAnimationFrame = (fn: Function) => w.setTimeout(fn, 0);
vi.stubGlobal('requestAnimationFrame', (fn: Function) => w.setTimeout(fn, 0));
vi.stubGlobal('requestIdleCallback', w.requestAnimationFrame.bind(w));
vi.stubGlobal('devicePixelRatio', 1);
vi.stubGlobal(
'ImageData',
class {
data = new Uint8ClampedArray(this.width * this.height * 4);
constructor(
private width: number,
private height: number
) {}
}
);
// resize observer mock
vi.stubGlobal(
'ResizeObserver',
class {
observe = vi.fn();
unobserve = vi.fn();
disconnect = vi.fn();
}
);
w.matchMedia = function () {
return {addListener: () => {}};
};
}
mock(window, window.document);