vitest-webgl-canvas-mock
Version:
Mock both 2D and WebGL contexts in Vitest.
42 lines (36 loc) • 1.03 kB
JavaScript
import { vi } from 'vitest';
import CanvasRenderingContext2D from './CanvasRenderingContext2D.js';
// Path2D.prototype
const Path2DFunc = [
'addPath',
];
const borrowedFromCanvas = [
'closePath',
'moveTo',
'lineTo',
'bezierCurveTo',
'quadraticCurveTo',
'arc',
'arcTo',
'ellipse',
'rect',
];
export default class Path2D {
_path = [];
_events = [];
_stackIndex = 0;
_transformStack = [[1, 0, 0, 1, 0, 0]];
constructor() {
borrowedFromCanvas.forEach((key) => {
this[key] = vi.fn(CanvasRenderingContext2D.prototype[key].bind(this));
});
Path2DFunc.forEach((key) => {
this[key] = vi.fn(this[key].bind(this));
});
}
addPath(path) {
if (arguments.length < 1) throw new TypeError('Failed to execute \'addPath\' on \'Path2D\': 1 argument required, but only 0 present.');
if (!(path instanceof Path2D)) throw new TypeError('Failed to execute \'addPath\' on \'Path2D\': parameter 1 is not of type \'Path2D\'.');
this._path = this._path.concat(path._path);
}
}