jest-canvas-mock
Version:
Mock a canvas in your jest tests.
34 lines (33 loc) • 1.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _CanvasRenderingContext2D = _interopRequireDefault(require("./CanvasRenderingContext2D"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
// Path2D.prototype
const Path2DFunc = ['addPath'];
const borrowedFromCanvas = ['closePath', 'moveTo', 'lineTo', 'bezierCurveTo', 'quadraticCurveTo', 'arc', 'arcTo', 'ellipse', 'rect'];
class Path2D {
constructor() {
_defineProperty(this, "_path", []);
_defineProperty(this, "_events", []);
_defineProperty(this, "_stackIndex", 0);
_defineProperty(this, "_transformStack", [[1, 0, 0, 1, 0, 0]]);
borrowedFromCanvas.forEach(key => {
this[key] = jest.fn(_CanvasRenderingContext2D.default.prototype[key].bind(this));
});
Path2DFunc.forEach(key => {
this[key] = jest.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'.");
for (let i = 0; i < path._path.length; i++) this._path.push(path._path[i]);
}
}
exports.default = Path2D;