jest-canvas-mock
Version:
Mock a canvas in your jest tests.
61 lines (60 loc) • 3.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
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); }
class ImageData {
get width() {
return this._width;
}
get height() {
return this._height;
}
get data() {
return this._data;
}
constructor(arr, w, h) {
_defineProperty(this, "_width", 0);
_defineProperty(this, "_height", 0);
_defineProperty(this, "_data", null);
if (arguments.length === 2) {
if (arr instanceof Uint8ClampedArray) {
if (arr.length === 0) throw new RangeError('Source length must be a positive multiple of 4.');
if (arr.length % 4 !== 0) throw new RangeError('Source length must be a positive multiple of 4.');
if (!Number.isFinite(w)) throw new RangeError('The width is zero or not a number.');
if (w === 0) throw new RangeError('The width is zero or not a number.');
this._width = w;
this._height = arr.length / 4 / w;
this._data = arr;
} else {
const width = arr;
const height = w;
if (!Number.isFinite(height)) throw new RangeError('The height is zero or not a number.');
if (height === 0) throw new RangeError('The height is zero or not a number.');
if (!Number.isFinite(width)) throw new RangeError('The width is zero or not a number.');
if (width === 0) throw new RangeError('The width is zero or not a number.');
this._width = width;
this._height = height;
this._data = new Uint8ClampedArray(width * height * 4);
}
} else if (arguments.length === 3) {
if (!(arr instanceof Uint8ClampedArray)) throw new TypeError('First argument must be a Uint8ClampedArray when using 3 arguments.');
if (arr.length === 0) throw new RangeError('Source length must be a positive multiple of 4.');
if (arr.length % 4 !== 0) throw new RangeError('Source length must be a positive multiple of 4.');
if (!Number.isFinite(h)) throw new RangeError('The height is zero or not a number.');
if (h === 0) throw new RangeError('The height is zero or not a number.');
if (!Number.isFinite(w)) throw new RangeError('The width is zero or not a number.');
if (w === 0) throw new RangeError('The width is zero or not a number.');
if (arr.length !== w * h * 4) throw new RangeError("Source doesn'n contain the exact number of pixels needed.");
this._width = w;
this._height = h;
this._data = arr;
} else {
throw new TypeError('Wrong number of arguments provided.');
}
}
}
exports.default = ImageData;