jest-canvas-mock 2.4.0 → 2.5.0
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/CHANGELOG.md +7 -0
- package/README.md +14 -0
- package/lib/classes/CanvasGradient.js +0 -7
- package/lib/classes/CanvasPattern.js +0 -4
- package/lib/classes/CanvasRenderingContext2D.js +21 -334
- package/lib/classes/DOMMatrix.js +9 -64
- package/lib/classes/ImageBitmap.js +3 -9
- package/lib/classes/ImageData.js +3 -11
- package/lib/classes/Path2D.js +3 -14
- package/lib/classes/TextMetrics.js +3 -17
- package/lib/index.js +7 -6
- package/lib/mock/createCanvasEvent.js +0 -1
- package/lib/mock/createImageBitmap.js +2 -12
- package/lib/mock/prototype.js +7 -23
- package/lib/window.js +4 -17
- package/package.json +1 -1
- package/types/index.d.ts +3 -1
package/lib/classes/DOMMatrix.js
CHANGED
|
@@ -4,28 +4,24 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
|
-
function
|
|
9
|
-
|
|
7
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
8
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
|
|
9
|
+
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
10
10
|
function sumMultipleOfMatricesCells(matrix1Array, matrix2Array, {
|
|
11
11
|
i,
|
|
12
12
|
j
|
|
13
13
|
}) {
|
|
14
14
|
let sum = 0;
|
|
15
|
-
|
|
16
15
|
for (let k = 0; k < 4; k++) {
|
|
17
16
|
const matrix1Index = j - 1 + k * 4;
|
|
18
17
|
const matrix2Index = (i - 1) * 4 + k;
|
|
19
18
|
sum += matrix1Array[matrix1Index] * matrix2Array[matrix2Index];
|
|
20
19
|
}
|
|
21
|
-
|
|
22
20
|
return sum;
|
|
23
21
|
}
|
|
24
|
-
|
|
25
22
|
function multiplyMatrices(leftMatrix, rightMatrix) {
|
|
26
23
|
const leftMatrixArray = leftMatrix.toFloat64Array();
|
|
27
24
|
const rightMatrixArray = rightMatrix.toFloat64Array();
|
|
28
|
-
|
|
29
25
|
for (let i = 1; i <= 4; i++) {
|
|
30
26
|
for (let j = 1; j <= 4; j++) {
|
|
31
27
|
leftMatrix[`m${i}${j}`] = sumMultipleOfMatricesCells(leftMatrixArray, rightMatrixArray, {
|
|
@@ -35,43 +31,25 @@ function multiplyMatrices(leftMatrix, rightMatrix) {
|
|
|
35
31
|
}
|
|
36
32
|
}
|
|
37
33
|
}
|
|
38
|
-
|
|
39
34
|
class DOMMatrix {
|
|
40
35
|
constructor(transform) {
|
|
41
36
|
_defineProperty(this, "_is2D", true);
|
|
42
|
-
|
|
43
37
|
_defineProperty(this, "m11", 1.0);
|
|
44
|
-
|
|
45
38
|
_defineProperty(this, "m12", 0.0);
|
|
46
|
-
|
|
47
39
|
_defineProperty(this, "m13", 0.0);
|
|
48
|
-
|
|
49
40
|
_defineProperty(this, "m14", 0.0);
|
|
50
|
-
|
|
51
41
|
_defineProperty(this, "m21", 0.0);
|
|
52
|
-
|
|
53
42
|
_defineProperty(this, "m22", 1.0);
|
|
54
|
-
|
|
55
43
|
_defineProperty(this, "m23", 0.0);
|
|
56
|
-
|
|
57
44
|
_defineProperty(this, "m24", 0.0);
|
|
58
|
-
|
|
59
45
|
_defineProperty(this, "m31", 0.0);
|
|
60
|
-
|
|
61
46
|
_defineProperty(this, "m32", 0.0);
|
|
62
|
-
|
|
63
47
|
_defineProperty(this, "m33", 1.0);
|
|
64
|
-
|
|
65
48
|
_defineProperty(this, "m34", 0.0);
|
|
66
|
-
|
|
67
49
|
_defineProperty(this, "m41", 0.0);
|
|
68
|
-
|
|
69
50
|
_defineProperty(this, "m42", 0.0);
|
|
70
|
-
|
|
71
51
|
_defineProperty(this, "m43", 0.0);
|
|
72
|
-
|
|
73
52
|
_defineProperty(this, "m44", 1.0);
|
|
74
|
-
|
|
75
53
|
if (transform && transform.length === 6) {
|
|
76
54
|
this.m11 = transform[0];
|
|
77
55
|
this.m12 = transform[1];
|
|
@@ -82,7 +60,6 @@ class DOMMatrix {
|
|
|
82
60
|
this._is2D = true;
|
|
83
61
|
return this;
|
|
84
62
|
}
|
|
85
|
-
|
|
86
63
|
if (transform && transform.length === 16) {
|
|
87
64
|
this.m11 = transform[0];
|
|
88
65
|
this.m12 = transform[1];
|
|
@@ -103,14 +80,11 @@ class DOMMatrix {
|
|
|
103
80
|
this._is2D = false;
|
|
104
81
|
return this;
|
|
105
82
|
}
|
|
106
|
-
|
|
107
83
|
if (transform) {
|
|
108
84
|
throw new TypeError("Failed to construct 'DOMMatrix': The sequence must contain 6 elements for a 2D matrix or 16 elements for a 3D matrix.");
|
|
109
85
|
}
|
|
110
|
-
|
|
111
86
|
this._is2D = false;
|
|
112
87
|
}
|
|
113
|
-
|
|
114
88
|
get isIdentity() {
|
|
115
89
|
if (this._is2D) {
|
|
116
90
|
return this.m11 == 1.0 && this.m12 == 0.0 && this.m21 == 0.0 && this.m22 == 1.0 && this.m41 == 0.0 && this.m42 == 0.0;
|
|
@@ -118,103 +92,81 @@ class DOMMatrix {
|
|
|
118
92
|
return this.m11 = 1.0 && this.m12 === 0.0 && this.m13 === 0.0 && this.m14 === 0.0 && this.m21 === 0.0 && this.m22 === 1.0 && this.m23 === 0.0 && this.m24 === 0.0 && this.m31 === 0.0 && this.m32 === 0.0 && this.m33 === 1.0 && this.m34 === 0.0 && this.m41 === 0.0 && this.m42 === 0.0 && this.m43 === 0.0 && this.m44 === 1.0;
|
|
119
93
|
}
|
|
120
94
|
}
|
|
121
|
-
|
|
122
95
|
get a() {
|
|
123
96
|
return this.m11;
|
|
124
97
|
}
|
|
125
|
-
|
|
126
98
|
set a(value) {
|
|
127
99
|
this.m11 = value;
|
|
128
100
|
}
|
|
129
|
-
|
|
130
101
|
get b() {
|
|
131
102
|
return this.m12;
|
|
132
103
|
}
|
|
133
|
-
|
|
134
104
|
set b(value) {
|
|
135
105
|
this.m12 = value;
|
|
136
106
|
}
|
|
137
|
-
|
|
138
107
|
get c() {
|
|
139
108
|
return this.m21;
|
|
140
109
|
}
|
|
141
|
-
|
|
142
110
|
set c(value) {
|
|
143
111
|
this.m21 = value;
|
|
144
112
|
}
|
|
145
|
-
|
|
146
113
|
get d() {
|
|
147
114
|
return this.m22;
|
|
148
115
|
}
|
|
149
|
-
|
|
150
116
|
set d(value) {
|
|
151
117
|
this.m22 = value;
|
|
152
118
|
}
|
|
153
|
-
|
|
154
119
|
get e() {
|
|
155
120
|
return this.m41;
|
|
156
121
|
}
|
|
157
|
-
|
|
158
122
|
set e(value) {
|
|
159
123
|
this.m41 = value;
|
|
160
124
|
}
|
|
161
|
-
|
|
162
125
|
get f() {
|
|
163
126
|
return this.m42;
|
|
164
127
|
}
|
|
165
|
-
|
|
166
128
|
set f(value) {
|
|
167
129
|
this.m42 = value;
|
|
168
130
|
}
|
|
169
|
-
|
|
170
131
|
get is2D() {
|
|
171
132
|
return this._is2D;
|
|
172
133
|
}
|
|
173
|
-
|
|
174
134
|
toFloat32Array() {
|
|
175
135
|
return new Float32Array([this.m11, this.m12, this.m13, this.m14, this.m21, this.m22, this.m23, this.m24, this.m31, this.m32, this.m33, this.m34, this.m41, this.m42, this.m43, this.m44]);
|
|
176
136
|
}
|
|
177
|
-
|
|
178
137
|
toFloat64Array() {
|
|
179
138
|
return new Float64Array([this.m11, this.m12, this.m13, this.m14, this.m21, this.m22, this.m23, this.m24, this.m31, this.m32, this.m33, this.m34, this.m41, this.m42, this.m43, this.m44]);
|
|
180
139
|
}
|
|
181
|
-
|
|
182
140
|
translateSelf(x, y, z) {
|
|
183
141
|
const tx = Number(x),
|
|
184
|
-
|
|
185
|
-
|
|
142
|
+
ty = Number(y),
|
|
143
|
+
tz = isNaN(Number(z)) ? 0 : Number(z);
|
|
186
144
|
const translationMatrix = new DOMMatrix();
|
|
187
145
|
translationMatrix.m41 = tx;
|
|
188
146
|
translationMatrix.m42 = ty;
|
|
189
147
|
translationMatrix.m43 = tz;
|
|
190
148
|
multiplyMatrices(this, translationMatrix);
|
|
191
|
-
|
|
192
149
|
if (tz) {
|
|
193
150
|
this._is2D = false;
|
|
194
151
|
}
|
|
195
|
-
|
|
196
152
|
return this;
|
|
197
153
|
}
|
|
198
|
-
|
|
199
154
|
translate(x, y, z) {
|
|
200
155
|
let translatedMatrix;
|
|
201
|
-
|
|
202
156
|
if (this.is2D) {
|
|
203
157
|
translatedMatrix = new DOMMatrix([this.a, this.b, this.c, this.d, this.e, this.f]);
|
|
204
158
|
} else {
|
|
205
159
|
translatedMatrix = new DOMMatrix(this.toFloat32Array());
|
|
206
160
|
}
|
|
207
|
-
|
|
208
161
|
return translatedMatrix.translateSelf(x, y, z);
|
|
209
162
|
}
|
|
210
|
-
|
|
211
163
|
scaleSelf(scaleX, scaleY, scaleZ, originX, originY, originZ) {
|
|
212
164
|
const sx = Number(scaleX),
|
|
213
|
-
|
|
214
|
-
|
|
165
|
+
sy = isNaN(Number(scaleY)) ? sx : Number(scaleY),
|
|
166
|
+
sz = isNaN(Number(scaleZ)) ? 1 : Number(scaleZ);
|
|
215
167
|
const ox = isNaN(Number(originX)) ? 0 : Number(originX),
|
|
216
|
-
|
|
217
|
-
|
|
168
|
+
oy = isNaN(Number(originY)) ? 0 : Number(originY),
|
|
169
|
+
oz = isNaN(Number(originZ)) ? 0 : Number(originZ);
|
|
218
170
|
this.translateSelf(ox, oy, oz);
|
|
219
171
|
const scaleMatrix = new DOMMatrix();
|
|
220
172
|
scaleMatrix.m11 = sx;
|
|
@@ -222,26 +174,19 @@ class DOMMatrix {
|
|
|
222
174
|
scaleMatrix.m33 = sz;
|
|
223
175
|
multiplyMatrices(this, scaleMatrix);
|
|
224
176
|
this.translateSelf(-ox, -oy, -oz);
|
|
225
|
-
|
|
226
177
|
if (Math.abs(sz) !== 1) {
|
|
227
178
|
this._is2D = false;
|
|
228
179
|
}
|
|
229
|
-
|
|
230
180
|
return this;
|
|
231
181
|
}
|
|
232
|
-
|
|
233
182
|
scale(scaleX, scaleY, scaleZ, originX, originY, originZ) {
|
|
234
183
|
let scaledMatrix;
|
|
235
|
-
|
|
236
184
|
if (this.is2D) {
|
|
237
185
|
scaledMatrix = new DOMMatrix([this.a, this.b, this.c, this.d, this.e, this.f]);
|
|
238
186
|
} else {
|
|
239
187
|
scaledMatrix = new DOMMatrix(this.toFloat32Array());
|
|
240
188
|
}
|
|
241
|
-
|
|
242
189
|
return scaledMatrix.scaleSelf(scaleX, scaleY, scaleZ, originX, originY, originZ);
|
|
243
190
|
}
|
|
244
|
-
|
|
245
191
|
}
|
|
246
|
-
|
|
247
192
|
exports.default = DOMMatrix;
|
|
@@ -4,28 +4,22 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
|
-
function
|
|
9
|
-
|
|
7
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
8
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
|
|
9
|
+
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
10
10
|
class ImageBitmap {
|
|
11
11
|
constructor(width, height) {
|
|
12
12
|
_defineProperty(this, "width", 0);
|
|
13
|
-
|
|
14
13
|
_defineProperty(this, "height", 0);
|
|
15
|
-
|
|
16
14
|
_defineProperty(this, "_closed", false);
|
|
17
|
-
|
|
18
15
|
this.width = width;
|
|
19
16
|
this.height = height;
|
|
20
17
|
this.close = jest.fn(this.close.bind(this));
|
|
21
18
|
}
|
|
22
|
-
|
|
23
19
|
close() {
|
|
24
20
|
this.width = 0;
|
|
25
21
|
this.height = 0;
|
|
26
22
|
this._closed = true;
|
|
27
23
|
}
|
|
28
|
-
|
|
29
24
|
}
|
|
30
|
-
|
|
31
25
|
exports.default = ImageBitmap;
|
package/lib/classes/ImageData.js
CHANGED
|
@@ -4,29 +4,23 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
|
-
function
|
|
9
|
-
|
|
7
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
8
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
|
|
9
|
+
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
10
10
|
class ImageData {
|
|
11
11
|
get width() {
|
|
12
12
|
return this._width;
|
|
13
13
|
}
|
|
14
|
-
|
|
15
14
|
get height() {
|
|
16
15
|
return this._height;
|
|
17
16
|
}
|
|
18
|
-
|
|
19
17
|
get data() {
|
|
20
18
|
return this._data;
|
|
21
19
|
}
|
|
22
|
-
|
|
23
20
|
constructor(arr, w, h) {
|
|
24
21
|
_defineProperty(this, "_width", 0);
|
|
25
|
-
|
|
26
22
|
_defineProperty(this, "_height", 0);
|
|
27
|
-
|
|
28
23
|
_defineProperty(this, "_data", null);
|
|
29
|
-
|
|
30
24
|
if (arguments.length === 2) {
|
|
31
25
|
if (arr instanceof Uint8ClampedArray) {
|
|
32
26
|
if (arr.length === 0) throw new RangeError('Source length must be a positive multiple of 4.');
|
|
@@ -63,7 +57,5 @@ class ImageData {
|
|
|
63
57
|
throw new TypeError('Wrong number of arguments provided.');
|
|
64
58
|
}
|
|
65
59
|
}
|
|
66
|
-
|
|
67
60
|
}
|
|
68
|
-
|
|
69
61
|
exports.default = ImageData;
|
package/lib/classes/Path2D.js
CHANGED
|
@@ -4,27 +4,20 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var _CanvasRenderingContext2D = _interopRequireDefault(require("./CanvasRenderingContext2D"));
|
|
9
|
-
|
|
10
8
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
-
|
|
12
|
-
function
|
|
13
|
-
|
|
9
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
10
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
|
|
11
|
+
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
14
12
|
// Path2D.prototype
|
|
15
13
|
const Path2DFunc = ['addPath'];
|
|
16
14
|
const borrowedFromCanvas = ['closePath', 'moveTo', 'lineTo', 'bezierCurveTo', 'quadraticCurveTo', 'arc', 'arcTo', 'ellipse', 'rect'];
|
|
17
|
-
|
|
18
15
|
class Path2D {
|
|
19
16
|
constructor() {
|
|
20
17
|
_defineProperty(this, "_path", []);
|
|
21
|
-
|
|
22
18
|
_defineProperty(this, "_events", []);
|
|
23
|
-
|
|
24
19
|
_defineProperty(this, "_stackIndex", 0);
|
|
25
|
-
|
|
26
20
|
_defineProperty(this, "_transformStack", [[1, 0, 0, 1, 0, 0]]);
|
|
27
|
-
|
|
28
21
|
borrowedFromCanvas.forEach(key => {
|
|
29
22
|
this[key] = jest.fn(_CanvasRenderingContext2D.default.prototype[key].bind(this));
|
|
30
23
|
});
|
|
@@ -32,14 +25,10 @@ class Path2D {
|
|
|
32
25
|
this[key] = jest.fn(this[key].bind(this));
|
|
33
26
|
});
|
|
34
27
|
}
|
|
35
|
-
|
|
36
28
|
addPath(path) {
|
|
37
29
|
if (arguments.length < 1) throw new TypeError("Failed to execute 'addPath' on 'Path2D': 1 argument required, but only 0 present.");
|
|
38
30
|
if (!(path instanceof Path2D)) throw new TypeError("Failed to execute 'addPath' on 'Path2D': parameter 1 is not of type 'Path2D'.");
|
|
39
|
-
|
|
40
31
|
for (let i = 0; i < path._path.length; i++) this._path.push(path._path[i]);
|
|
41
32
|
}
|
|
42
|
-
|
|
43
33
|
}
|
|
44
|
-
|
|
45
34
|
exports.default = Path2D;
|
|
@@ -4,38 +4,24 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
|
-
function
|
|
9
|
-
|
|
7
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
8
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
|
|
9
|
+
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
10
10
|
class TextMetrics {
|
|
11
11
|
constructor(text) {
|
|
12
12
|
_defineProperty(this, "width", 0);
|
|
13
|
-
|
|
14
13
|
_defineProperty(this, "actualBoundingBoxLeft", 0);
|
|
15
|
-
|
|
16
14
|
_defineProperty(this, "actualBoundingBoxRight", 0);
|
|
17
|
-
|
|
18
15
|
_defineProperty(this, "fontBoundingBoxAscent", 0);
|
|
19
|
-
|
|
20
16
|
_defineProperty(this, "fontBoundingBoxDescent", 0);
|
|
21
|
-
|
|
22
17
|
_defineProperty(this, "actualBoundingBoxAscent", 0);
|
|
23
|
-
|
|
24
18
|
_defineProperty(this, "actualBoundingBoxDescent", 0);
|
|
25
|
-
|
|
26
19
|
_defineProperty(this, "emHeightAscent", 0);
|
|
27
|
-
|
|
28
20
|
_defineProperty(this, "emHeightDescent", 0);
|
|
29
|
-
|
|
30
21
|
_defineProperty(this, "hangingBaseline", 0);
|
|
31
|
-
|
|
32
22
|
_defineProperty(this, "alphabeticBaseline", 0);
|
|
33
|
-
|
|
34
23
|
_defineProperty(this, "ideographicBaseline", 0);
|
|
35
|
-
|
|
36
24
|
this.width = text.length;
|
|
37
25
|
}
|
|
38
|
-
|
|
39
26
|
}
|
|
40
|
-
|
|
41
27
|
exports.default = TextMetrics;
|
package/lib/index.js
CHANGED
|
@@ -3,21 +3,22 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
exports.setupJestCanvasMock = setupJestCanvasMock;
|
|
6
7
|
exports.ver = void 0;
|
|
7
|
-
|
|
8
8
|
var _window = _interopRequireDefault(require("./window"));
|
|
9
|
-
|
|
10
9
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
-
|
|
12
10
|
/**
|
|
13
11
|
* Created by hustcc 17/12/25.
|
|
14
12
|
* Contract: i@hust.cc
|
|
15
13
|
*/
|
|
14
|
+
|
|
16
15
|
// mock global window
|
|
17
16
|
// TODO: Force coverage to ignore this branch
|
|
18
17
|
if (typeof window !== 'undefined') {
|
|
19
18
|
(0, _window.default)(global.window);
|
|
20
19
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
const ver = "2.5.0";
|
|
21
|
+
exports.ver = ver;
|
|
22
|
+
function setupJestCanvasMock(window) {
|
|
23
|
+
(0, _window.default)(window || global.window);
|
|
24
|
+
}
|
|
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = createCanvasEvent;
|
|
7
|
-
|
|
8
7
|
/**
|
|
9
8
|
* This function returns a CanvasRenderingContext2DEvent. Whenever an operation would modify the canvas
|
|
10
9
|
* context, this function should be used to generate an "event" that represents that sort of modification.
|
|
@@ -4,11 +4,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var _ImageBitmap = _interopRequireDefault(require("../classes/ImageBitmap"));
|
|
9
|
-
|
|
10
8
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
-
|
|
12
9
|
var _default = jest.fn(function createImageBitmap(img, sx, sy, sWidth, sHeight, options) {
|
|
13
10
|
var length = arguments.length;
|
|
14
11
|
return new Promise((resolve, reject) => {
|
|
@@ -17,28 +14,23 @@ var _default = jest.fn(function createImageBitmap(img, sx, sy, sWidth, sHeight,
|
|
|
17
14
|
let validImage = false;
|
|
18
15
|
if (img instanceof HTMLImageElement) validImage = true;
|
|
19
16
|
if (img instanceof HTMLVideoElement) validImage = true;
|
|
20
|
-
if (img instanceof HTMLCanvasElement) validImage = true;
|
|
21
|
-
|
|
17
|
+
if (img instanceof HTMLCanvasElement) validImage = true;
|
|
18
|
+
// checking constructor name is the only reliable way to verify the object's constructing class is "blob-like"
|
|
22
19
|
if (img instanceof Blob || img && img.constructor && img.constructor.name === 'Blob') validImage = true;
|
|
23
20
|
if (img instanceof _ImageBitmap.default) validImage = true;
|
|
24
21
|
if (img instanceof ImageData) validImage = true;
|
|
25
22
|
if (!validImage) return reject(new TypeError("Failed to execute 'createImageBitmap' on 'Window': The provided value is not of type '(HTMLImageElement or SVGImageElement or HTMLVideoElement or HTMLCanvasElement or Blob or ImageData or ImageBitmap or OffscreenCanvas)'"));
|
|
26
|
-
|
|
27
23
|
if (length >= 2) {
|
|
28
24
|
let index = 6;
|
|
29
|
-
|
|
30
25
|
if (length === 2) {
|
|
31
26
|
index = 2;
|
|
32
27
|
options = sx;
|
|
33
28
|
}
|
|
34
|
-
|
|
35
29
|
if (length === 5) options = null;
|
|
36
|
-
|
|
37
30
|
if (options !== null && options !== void 0) {
|
|
38
31
|
if (typeof options !== 'object') throw new TypeError("Failed to execute 'createImageBitmap' on 'Window': parameter " + index + " ('options') is not an object.");
|
|
39
32
|
}
|
|
40
33
|
}
|
|
41
|
-
|
|
42
34
|
if (length >= 5) {
|
|
43
35
|
sWidth = Number(sWidth);
|
|
44
36
|
sHeight = Number(sHeight);
|
|
@@ -50,9 +42,7 @@ var _default = jest.fn(function createImageBitmap(img, sx, sy, sWidth, sHeight,
|
|
|
50
42
|
sWidth = img.width || 1;
|
|
51
43
|
sHeight = img.height || 1;
|
|
52
44
|
}
|
|
53
|
-
|
|
54
45
|
return resolve(new _ImageBitmap.default(sWidth, sHeight));
|
|
55
46
|
});
|
|
56
47
|
});
|
|
57
|
-
|
|
58
48
|
exports.default = _default;
|
package/lib/mock/prototype.js
CHANGED
|
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = mockPrototype;
|
|
7
|
-
|
|
8
7
|
function mockPrototype() {
|
|
9
8
|
/**
|
|
10
9
|
* This weakmap is designed to contain all of the generated canvas contexts. It's keys are the
|
|
@@ -17,11 +16,10 @@ function mockPrototype() {
|
|
|
17
16
|
* value of getContext. It attempts to preserve the original getContext function by storing it on
|
|
18
17
|
* the callback as a property.
|
|
19
18
|
*/
|
|
20
|
-
|
|
21
19
|
const getContext2D = jest.fn(function getContext2d(type) {
|
|
22
20
|
if (type === '2d') {
|
|
23
21
|
/**
|
|
24
|
-
* Contexts must be
|
|
22
|
+
* Contexts must be idempotent. Once they are generated, they should be returned when
|
|
25
23
|
* getContext() is called on the same canvas object multiple times.
|
|
26
24
|
*/
|
|
27
25
|
if (generatedContexts.has(this)) return generatedContexts.get(this);
|
|
@@ -29,52 +27,45 @@ function mockPrototype() {
|
|
|
29
27
|
generatedContexts.set(this, ctx);
|
|
30
28
|
return ctx;
|
|
31
29
|
}
|
|
32
|
-
|
|
33
30
|
try {
|
|
34
31
|
if (!this.dataset.internalRequireTest) require('canvas');
|
|
35
32
|
} catch (_unused) {
|
|
36
33
|
return null;
|
|
37
34
|
}
|
|
38
|
-
|
|
39
35
|
return getContext2D.internal.call(this, type);
|
|
40
36
|
});
|
|
41
|
-
|
|
42
37
|
if (!jest.isMockFunction(HTMLCanvasElement.prototype.getContext)) {
|
|
43
38
|
getContext2D.internal = HTMLCanvasElement.prototype.getContext;
|
|
44
39
|
} else {
|
|
45
40
|
getContext2D.internal = HTMLCanvasElement.prototype.getContext.internal;
|
|
46
41
|
}
|
|
47
|
-
|
|
48
42
|
HTMLCanvasElement.prototype.getContext = getContext2D;
|
|
43
|
+
|
|
49
44
|
/**
|
|
50
45
|
* This function technically throws SecurityError at runtime, but it cannot be mocked, because
|
|
51
46
|
* we don't know if the canvas is tainted. These kinds of errors will be silent.
|
|
52
47
|
*/
|
|
53
|
-
|
|
54
48
|
const toBlobOverride = jest.fn(function toBlobOverride(callback, mimetype) {
|
|
55
49
|
if (arguments.length < 1) throw new TypeError("Failed to execute 'toBlob' on 'HTMLCanvasElement': 1 argument required, but only 0 present.");
|
|
56
50
|
if (typeof callback !== 'function') throw new TypeError("Failed to execute 'toBlob' on 'HTMLCanvasElement': The callback provided as parameter 1 is not a function.");
|
|
51
|
+
|
|
57
52
|
/**
|
|
58
53
|
* Mime type must be image/jpeg or image/webp exactly for the browser to accept it, otherwise
|
|
59
54
|
* it's image/png.
|
|
60
55
|
*/
|
|
61
|
-
|
|
62
56
|
switch (mimetype) {
|
|
63
57
|
case 'image/webp':
|
|
64
58
|
break;
|
|
65
|
-
|
|
66
59
|
case 'image/jpeg':
|
|
67
60
|
break;
|
|
68
|
-
|
|
69
61
|
default:
|
|
70
62
|
mimetype = 'image/png';
|
|
71
63
|
}
|
|
64
|
+
|
|
72
65
|
/**
|
|
73
66
|
* This section creates a blob of size width * height * 4. This is not actually valid, because
|
|
74
67
|
* jpeg size is variable, and so is png. TODO: Is there a better way to do this?
|
|
75
68
|
*/
|
|
76
|
-
|
|
77
|
-
|
|
78
69
|
const length = this.width * this.height * 4;
|
|
79
70
|
const data = new Uint8Array(length);
|
|
80
71
|
const blob = new window.Blob([data], {
|
|
@@ -82,43 +73,36 @@ function mockPrototype() {
|
|
|
82
73
|
});
|
|
83
74
|
setTimeout(() => callback(blob), 0);
|
|
84
75
|
});
|
|
85
|
-
|
|
86
76
|
if (!jest.isMockFunction(HTMLCanvasElement.prototype.toBlob)) {
|
|
87
77
|
toBlobOverride.internal = HTMLCanvasElement.prototype.toBlob;
|
|
88
78
|
} else {
|
|
89
79
|
toBlobOverride.internal = HTMLCanvasElement.prototype.toBlob.internal;
|
|
90
80
|
}
|
|
91
|
-
|
|
92
81
|
HTMLCanvasElement.prototype.toBlob = toBlobOverride;
|
|
82
|
+
|
|
93
83
|
/**
|
|
94
84
|
* This section creates a dataurl with a validated mime type. This is not actually valid, because
|
|
95
85
|
* jpeg size is variable, and so is png. TODO: Is there a better way to do this?
|
|
96
86
|
*/
|
|
97
|
-
|
|
98
87
|
const toDataURLOverride = jest.fn(function toDataURLOverride(type, encoderOptions) {
|
|
99
88
|
switch (type) {
|
|
100
89
|
case 'image/jpeg':
|
|
101
90
|
break;
|
|
102
|
-
|
|
103
91
|
case 'image/webp':
|
|
104
92
|
break;
|
|
105
|
-
|
|
106
93
|
default:
|
|
107
94
|
type = 'image/png';
|
|
108
95
|
}
|
|
96
|
+
|
|
109
97
|
/**
|
|
110
98
|
* This is the smallest valid data url I could generate.
|
|
111
99
|
*/
|
|
112
|
-
|
|
113
|
-
|
|
114
100
|
return 'data:' + type + ';base64,00';
|
|
115
101
|
});
|
|
116
|
-
|
|
117
102
|
if (!jest.isMockFunction(HTMLCanvasElement.prototype.toDataURL)) {
|
|
118
103
|
toDataURLOverride.internal = HTMLCanvasElement.prototype.toDataURL;
|
|
119
104
|
} else {
|
|
120
|
-
toDataURLOverride.internal = HTMLCanvasElement.prototype.
|
|
105
|
+
toDataURLOverride.internal = HTMLCanvasElement.prototype.toDataURL.internal;
|
|
121
106
|
}
|
|
122
|
-
|
|
123
107
|
HTMLCanvasElement.prototype.toDataURL = toDataURLOverride;
|
|
124
108
|
}
|
package/lib/window.js
CHANGED
|
@@ -4,38 +4,28 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var _Path2D = _interopRequireDefault(require("./classes/Path2D"));
|
|
9
|
-
|
|
10
8
|
var _CanvasGradient = _interopRequireDefault(require("./classes/CanvasGradient"));
|
|
11
|
-
|
|
12
9
|
var _CanvasPattern = _interopRequireDefault(require("./classes/CanvasPattern"));
|
|
13
|
-
|
|
14
10
|
var _CanvasRenderingContext2D = _interopRequireDefault(require("./classes/CanvasRenderingContext2D"));
|
|
15
|
-
|
|
16
11
|
var _DOMMatrix = _interopRequireDefault(require("./classes/DOMMatrix"));
|
|
17
|
-
|
|
18
12
|
var _ImageData = _interopRequireDefault(require("./classes/ImageData"));
|
|
19
|
-
|
|
20
13
|
var _TextMetrics = _interopRequireDefault(require("./classes/TextMetrics"));
|
|
21
|
-
|
|
22
14
|
var _ImageBitmap = _interopRequireDefault(require("./classes/ImageBitmap"));
|
|
23
|
-
|
|
24
15
|
var _prototype = _interopRequireDefault(require("./mock/prototype"));
|
|
25
|
-
|
|
26
16
|
var _createImageBitmap = _interopRequireDefault(require("./mock/createImageBitmap"));
|
|
27
|
-
|
|
28
17
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
29
|
-
|
|
30
18
|
/**
|
|
31
19
|
* Created by hustcc 17/12/25.
|
|
32
20
|
* Contract: i@hust.cc
|
|
33
21
|
*/
|
|
34
22
|
var _default = win => {
|
|
35
23
|
const d = win.document;
|
|
36
|
-
const f = win.document.createElement;
|
|
24
|
+
const f = win.document.createElement;
|
|
25
|
+
|
|
26
|
+
// jsdom@11.6.2 || jest@^22.0.0, console.error in Function getContext();
|
|
37
27
|
// https://github.com/jsdom/jsdom/blob/4c7698f760fc64f20b2a0ddff450eddbdd193176/lib/jsdom/living/nodes/HTMLCanvasElement-impl.js#L55-L58
|
|
38
|
-
//
|
|
28
|
+
// console.error will make ci error.
|
|
39
29
|
// try {
|
|
40
30
|
// // get the context 2d.
|
|
41
31
|
// const ctx = d.createElement('canvas').getContext('2d');
|
|
@@ -53,14 +43,12 @@ var _default = win => {
|
|
|
53
43
|
// }
|
|
54
44
|
// if ctx not exist, mock it.
|
|
55
45
|
// just mock canvas creator.
|
|
56
|
-
|
|
57
46
|
/*
|
|
58
47
|
win.document.createElement = param => param.toString().toLowerCase() === 'canvas'
|
|
59
48
|
? createCanvas('canvas')
|
|
60
49
|
: f.call(d, param);
|
|
61
50
|
*/
|
|
62
51
|
// if not exist, then mock it.
|
|
63
|
-
|
|
64
52
|
if (!win.Path2D) win.Path2D = _Path2D.default;
|
|
65
53
|
if (!win.CanvasGradient) win.CanvasGradient = _CanvasGradient.default;
|
|
66
54
|
if (!win.CanvasPattern) win.CanvasPattern = _CanvasPattern.default;
|
|
@@ -73,5 +61,4 @@ var _default = win => {
|
|
|
73
61
|
(0, _prototype.default)();
|
|
74
62
|
return win;
|
|
75
63
|
};
|
|
76
|
-
|
|
77
64
|
exports.default = _default;
|