jest-canvas-mock 2.5.0 → 2.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/lib/classes/CanvasGradient.js +1 -1
- package/lib/classes/CanvasRenderingContext2D.js +18 -17
- package/lib/index.js +1 -1
- package/lib/mock/prototype.js +13 -13
- package/lib/window.js +1 -1
- package/package.json +1 -1
- package/types/index.d.ts +1 -1
package/README.md
CHANGED
|
@@ -195,6 +195,8 @@ canvas.toDataURL.mockReturnValueOnce(
|
|
|
195
195
|
- [@lekha](https://github.com/lekha)
|
|
196
196
|
- [@yonatankra](https://github.com/yonatankra)
|
|
197
197
|
- [@LitoMore](https://github.com/LitoMore)
|
|
198
|
+
- [@hrd543](https://github.com/hrd543)
|
|
199
|
+
- [@danielrentz](https://github.com/danielrentz)
|
|
198
200
|
|
|
199
201
|
## License
|
|
200
202
|
|
|
@@ -12,7 +12,7 @@ class CanvasGradient {
|
|
|
12
12
|
addColorStop(offset, color) {
|
|
13
13
|
const numoffset = Number(offset);
|
|
14
14
|
if (!Number.isFinite(numoffset) || numoffset < 0 || numoffset > 1) {
|
|
15
|
-
throw new DOMException(
|
|
15
|
+
throw new DOMException("Failed to execute 'addColorStop' on 'CanvasGradient': The provided value ('" + numoffset + "') is outside the range (0.0, 1.0)", 'IndexSizeError');
|
|
16
16
|
}
|
|
17
17
|
try {
|
|
18
18
|
new _mooColor.MooColor(color);
|
|
@@ -28,11 +28,6 @@ function serializeColor(value) {
|
|
|
28
28
|
return value.getAlpha() === 1 ? value.toHex() : value.toRgb();
|
|
29
29
|
}
|
|
30
30
|
class CanvasRenderingContext2D {
|
|
31
|
-
/**
|
|
32
|
-
* Every time a function call would result in a drawing operation, it should be added to this array.
|
|
33
|
-
* This goes for only draw call functions.
|
|
34
|
-
*/
|
|
35
|
-
|
|
36
31
|
__getDrawCalls() {
|
|
37
32
|
return this._drawCalls.slice();
|
|
38
33
|
}
|
|
@@ -86,6 +81,10 @@ class CanvasRenderingContext2D {
|
|
|
86
81
|
return this._clipStack[this._stackIndex];
|
|
87
82
|
}
|
|
88
83
|
constructor(canvas) {
|
|
84
|
+
/**
|
|
85
|
+
* Every time a function call would result in a drawing operation, it should be added to this array.
|
|
86
|
+
* This goes for only draw call functions.
|
|
87
|
+
*/
|
|
89
88
|
_defineProperty(this, "_drawCalls", []);
|
|
90
89
|
_defineProperty(this, "_events", []);
|
|
91
90
|
_defineProperty(this, "_path", [(0, _createCanvasEvent.default)('beginPath', [1, 0, 0, 1, 0, 0], {})]);
|
|
@@ -134,7 +133,7 @@ class CanvasRenderingContext2D {
|
|
|
134
133
|
label,
|
|
135
134
|
role
|
|
136
135
|
} = options;
|
|
137
|
-
if (!path && !id) throw new DOMException(
|
|
136
|
+
if (!path && !id) throw new DOMException("Failed to execute 'addHitRegion' on '" + this.constructor.name + "': Both id and control are null.", 'ConstraintError');
|
|
138
137
|
if (fillRule && fillRule !== 'evenodd' && fillRule !== 'nonzero') throw new TypeError("Failed to execute 'addHitRegion' on '" + this.constructor.name + "': The provided value '" + fillRule + "' is not a valid enum value of type CanvasFillRule.");
|
|
139
138
|
this._events.push((0, _createCanvasEvent.default)('addHitRegion', getTransformSlice(this), {
|
|
140
139
|
path,
|
|
@@ -158,7 +157,7 @@ class CanvasRenderingContext2D {
|
|
|
158
157
|
|
|
159
158
|
// quick is finite check
|
|
160
159
|
if (!Number.isFinite(xResult + yResult + radiusResult + startAngleResult + endAngleResult)) return;
|
|
161
|
-
if (Number(radius) < 0) throw new DOMException(
|
|
160
|
+
if (Number(radius) < 0) throw new DOMException("Failed to execute 'arc' on '" + this.constructor.name + "': The radius provided (" + radius + ') is negative.', 'IndexSizeError');
|
|
162
161
|
const event = (0, _createCanvasEvent.default)('arc', getTransformSlice(this), {
|
|
163
162
|
x: xResult,
|
|
164
163
|
y: yResult,
|
|
@@ -171,14 +170,14 @@ class CanvasRenderingContext2D {
|
|
|
171
170
|
this._events.push(event);
|
|
172
171
|
}
|
|
173
172
|
arcTo(cpx1, cpy1, cpx2, cpy2, radius) {
|
|
174
|
-
if (arguments.length < 5) throw new
|
|
173
|
+
if (arguments.length < 5) throw new TypeError("Failed to execute 'arcTo' on '" + this.constructor.name + "': 5 arguments required, but only " + arguments.length + ' present.');
|
|
175
174
|
const cpx1Result = Number(cpx1);
|
|
176
175
|
const cpy1Result = Number(cpy1);
|
|
177
176
|
const cpx2Result = Number(cpx2);
|
|
178
177
|
const cpy2Result = Number(cpy2);
|
|
179
178
|
const radiusResult = Number(radius);
|
|
180
179
|
if (!Number.isFinite(cpx1Result + cpx2Result + cpy1Result + cpy2Result + radiusResult)) return;
|
|
181
|
-
if (radiusResult < 0) throw new
|
|
180
|
+
if (radiusResult < 0) throw new DOMException("Failed to execute 'arcTo' on '" + this.constructor.name + "': The radius provided (" + radius + ') is negative.', 'IndexSizeError');
|
|
182
181
|
const event = (0, _createCanvasEvent.default)('arcTo', getTransformSlice(this), {
|
|
183
182
|
cpx1: cpx1Result,
|
|
184
183
|
cpy1: cpy1Result,
|
|
@@ -323,7 +322,7 @@ class CanvasRenderingContext2D {
|
|
|
323
322
|
type
|
|
324
323
|
});
|
|
325
324
|
if (image instanceof ImageBitmap) {
|
|
326
|
-
if (image._closed) throw new DOMException(
|
|
325
|
+
if (image._closed) throw new DOMException("Failed to execute 'createPattern' on 'CanvasRenderingContext2D': The image source is detached.", 'InvalidStateError');
|
|
327
326
|
this._events.push(event);
|
|
328
327
|
return new _CanvasPattern.default();
|
|
329
328
|
}
|
|
@@ -353,8 +352,8 @@ class CanvasRenderingContext2D {
|
|
|
353
352
|
const y1Result = Number(y1);
|
|
354
353
|
const r1Result = Number(r1);
|
|
355
354
|
if (!Number.isFinite(x0Result + y0Result + r0Result + x1Result + y1Result + r1Result)) throw new TypeError("Failed to execute 'createRadialGradient' on '" + this.constructor.name + "': The provided double value is non-finite.");
|
|
356
|
-
if (r0Result < 0) throw new DOMException(
|
|
357
|
-
if (r1Result < 0) throw new DOMException(
|
|
355
|
+
if (r0Result < 0) throw new DOMException("Failed to execute 'createRadialGradient' on '" + this.constructor.name + "': The r0 provided is less than 0.", 'IndexSizeError');
|
|
356
|
+
if (r1Result < 0) throw new DOMException("Failed to execute 'createRadialGradient' on '" + this.constructor.name + "': The r1 provided is less than 0.", 'IndexSizeError');
|
|
358
357
|
const event = (0, _createCanvasEvent.default)('createRadialGradient', getTransformSlice(this), {
|
|
359
358
|
x0: x0Result,
|
|
360
359
|
y0: y0Result,
|
|
@@ -420,7 +419,7 @@ class CanvasRenderingContext2D {
|
|
|
420
419
|
let valid = false;
|
|
421
420
|
if (img instanceof HTMLImageElement) valid = true;
|
|
422
421
|
if (img instanceof ImageBitmap) {
|
|
423
|
-
if (img._closed) throw new DOMException(
|
|
422
|
+
if (img._closed) throw new DOMException("DOMException: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The image source is detached.", 'InvalidStateError');
|
|
424
423
|
valid = true;
|
|
425
424
|
}
|
|
426
425
|
if (img instanceof HTMLVideoElement) valid = true;
|
|
@@ -490,8 +489,8 @@ class CanvasRenderingContext2D {
|
|
|
490
489
|
const endAngleResult = Number(endAngle);
|
|
491
490
|
const anticlockwiseResult = Boolean(anticlockwise);
|
|
492
491
|
if (!Number.isFinite(xResult + yResult + radiusXResult + radiusYResult + rotationResult + startAngleResult + endAngleResult)) return;
|
|
493
|
-
if (Number(radiusX) < 0) throw new DOMException(
|
|
494
|
-
if (Number(radiusY) < 0) throw new DOMException(
|
|
492
|
+
if (Number(radiusX) < 0) throw new DOMException("Failed to execute 'ellipse' on '" + this.constructor.name + "': The major-axis radius provided (" + radiusX + ') is negative.', 'IndexSizeError');
|
|
493
|
+
if (Number(radiusY) < 0) throw new DOMException("Failed to execute 'ellipse' on '" + this.constructor.name + "': The minor-axis radius provided (" + radiusY + ') is negative.', 'IndexSizeError');
|
|
495
494
|
const event = (0, _createCanvasEvent.default)('ellipse', getTransformSlice(this), {
|
|
496
495
|
x: xResult,
|
|
497
496
|
y: yResult,
|
|
@@ -610,8 +609,10 @@ class CanvasRenderingContext2D {
|
|
|
610
609
|
get font() {
|
|
611
610
|
return this._fontStack[this._stackIndex];
|
|
612
611
|
}
|
|
613
|
-
getImageData() {
|
|
614
|
-
|
|
612
|
+
getImageData(x, y, w, h) {
|
|
613
|
+
if (arguments.length < 4) throw new TypeError("Failed to execute 'getImageData' on '" + this.constructor.name + "': 4 arguments required, but only " + arguments.length + ' present.');
|
|
614
|
+
if (w == 0 || h == 0) throw new DOMException("Failed to execute 'getImageData' on '" + this.constructor.name + "': The source " + (w == 0 ? 'width' : 'height') + ' is 0.', 'IndexSizeError');
|
|
615
|
+
return new ImageData(Math.abs(w), Math.abs(h));
|
|
615
616
|
}
|
|
616
617
|
getLineDash() {
|
|
617
618
|
return this._lineDashStack[this._stackIndex];
|
package/lib/index.js
CHANGED
|
@@ -17,7 +17,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
17
17
|
if (typeof window !== 'undefined') {
|
|
18
18
|
(0, _window.default)(global.window);
|
|
19
19
|
}
|
|
20
|
-
const ver = "2.5.
|
|
20
|
+
const ver = "2.5.1";
|
|
21
21
|
exports.ver = ver;
|
|
22
22
|
function setupJestCanvasMock(window) {
|
|
23
23
|
(0, _window.default)(window || global.window);
|
package/lib/mock/prototype.js
CHANGED
|
@@ -4,7 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = mockPrototype;
|
|
7
|
-
function mockPrototype() {
|
|
7
|
+
function mockPrototype(win) {
|
|
8
8
|
/**
|
|
9
9
|
* This weakmap is designed to contain all of the generated canvas contexts. It's keys are the
|
|
10
10
|
* jsdom canvases obtained by using the `this` keyword inside the `#getContext('2d')` function
|
|
@@ -34,12 +34,12 @@ function mockPrototype() {
|
|
|
34
34
|
}
|
|
35
35
|
return getContext2D.internal.call(this, type);
|
|
36
36
|
});
|
|
37
|
-
if (!jest.isMockFunction(HTMLCanvasElement.prototype.getContext)) {
|
|
38
|
-
getContext2D.internal = HTMLCanvasElement.prototype.getContext;
|
|
37
|
+
if (!jest.isMockFunction(win.HTMLCanvasElement.prototype.getContext)) {
|
|
38
|
+
getContext2D.internal = win.HTMLCanvasElement.prototype.getContext;
|
|
39
39
|
} else {
|
|
40
|
-
getContext2D.internal = HTMLCanvasElement.prototype.getContext.internal;
|
|
40
|
+
getContext2D.internal = win.HTMLCanvasElement.prototype.getContext.internal;
|
|
41
41
|
}
|
|
42
|
-
HTMLCanvasElement.prototype.getContext = getContext2D;
|
|
42
|
+
win.HTMLCanvasElement.prototype.getContext = getContext2D;
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
45
|
* This function technically throws SecurityError at runtime, but it cannot be mocked, because
|
|
@@ -73,12 +73,12 @@ function mockPrototype() {
|
|
|
73
73
|
});
|
|
74
74
|
setTimeout(() => callback(blob), 0);
|
|
75
75
|
});
|
|
76
|
-
if (!jest.isMockFunction(HTMLCanvasElement.prototype.toBlob)) {
|
|
77
|
-
toBlobOverride.internal = HTMLCanvasElement.prototype.toBlob;
|
|
76
|
+
if (!jest.isMockFunction(win.HTMLCanvasElement.prototype.toBlob)) {
|
|
77
|
+
toBlobOverride.internal = win.HTMLCanvasElement.prototype.toBlob;
|
|
78
78
|
} else {
|
|
79
|
-
toBlobOverride.internal = HTMLCanvasElement.prototype.toBlob.internal;
|
|
79
|
+
toBlobOverride.internal = win.HTMLCanvasElement.prototype.toBlob.internal;
|
|
80
80
|
}
|
|
81
|
-
HTMLCanvasElement.prototype.toBlob = toBlobOverride;
|
|
81
|
+
win.HTMLCanvasElement.prototype.toBlob = toBlobOverride;
|
|
82
82
|
|
|
83
83
|
/**
|
|
84
84
|
* This section creates a dataurl with a validated mime type. This is not actually valid, because
|
|
@@ -99,10 +99,10 @@ function mockPrototype() {
|
|
|
99
99
|
*/
|
|
100
100
|
return 'data:' + type + ';base64,00';
|
|
101
101
|
});
|
|
102
|
-
if (!jest.isMockFunction(HTMLCanvasElement.prototype.toDataURL)) {
|
|
103
|
-
toDataURLOverride.internal = HTMLCanvasElement.prototype.toDataURL;
|
|
102
|
+
if (!jest.isMockFunction(win.HTMLCanvasElement.prototype.toDataURL)) {
|
|
103
|
+
toDataURLOverride.internal = win.HTMLCanvasElement.prototype.toDataURL;
|
|
104
104
|
} else {
|
|
105
|
-
toDataURLOverride.internal = HTMLCanvasElement.prototype.toDataURL.internal;
|
|
105
|
+
toDataURLOverride.internal = win.HTMLCanvasElement.prototype.toDataURL.internal;
|
|
106
106
|
}
|
|
107
|
-
HTMLCanvasElement.prototype.toDataURL = toDataURLOverride;
|
|
107
|
+
win.HTMLCanvasElement.prototype.toDataURL = toDataURLOverride;
|
|
108
108
|
}
|
package/lib/window.js
CHANGED
|
@@ -58,7 +58,7 @@ var _default = win => {
|
|
|
58
58
|
if (!win.TextMetrics) win.TextMetrics = _TextMetrics.default;
|
|
59
59
|
if (!win.ImageBitmap) win.ImageBitmap = _ImageBitmap.default;
|
|
60
60
|
if (!win.createImageBitmap) win.createImageBitmap = _createImageBitmap.default;
|
|
61
|
-
(0, _prototype.default)();
|
|
61
|
+
(0, _prototype.default)(win);
|
|
62
62
|
return win;
|
|
63
63
|
};
|
|
64
64
|
exports.default = _default;
|
package/package.json
CHANGED
package/types/index.d.ts
CHANGED