jest-canvas-mock 2.3.1 → 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 +16 -0
- package/README.md +17 -1
- 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 +78 -41
- package/lib/classes/ImageBitmap.js +3 -9
- package/lib/classes/ImageData.js +3 -11
- package/lib/classes/Path2D.js +4 -14
- package/lib/classes/TextMetrics.js +3 -17
- package/lib/index.js +8 -7
- 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 +3 -2
- package/types/index.d.ts +3 -1
package/lib/classes/DOMMatrix.js
CHANGED
|
@@ -4,45 +4,52 @@ 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
|
+
function sumMultipleOfMatricesCells(matrix1Array, matrix2Array, {
|
|
11
|
+
i,
|
|
12
|
+
j
|
|
13
|
+
}) {
|
|
14
|
+
let sum = 0;
|
|
15
|
+
for (let k = 0; k < 4; k++) {
|
|
16
|
+
const matrix1Index = j - 1 + k * 4;
|
|
17
|
+
const matrix2Index = (i - 1) * 4 + k;
|
|
18
|
+
sum += matrix1Array[matrix1Index] * matrix2Array[matrix2Index];
|
|
19
|
+
}
|
|
20
|
+
return sum;
|
|
21
|
+
}
|
|
22
|
+
function multiplyMatrices(leftMatrix, rightMatrix) {
|
|
23
|
+
const leftMatrixArray = leftMatrix.toFloat64Array();
|
|
24
|
+
const rightMatrixArray = rightMatrix.toFloat64Array();
|
|
25
|
+
for (let i = 1; i <= 4; i++) {
|
|
26
|
+
for (let j = 1; j <= 4; j++) {
|
|
27
|
+
leftMatrix[`m${i}${j}`] = sumMultipleOfMatricesCells(leftMatrixArray, rightMatrixArray, {
|
|
28
|
+
i,
|
|
29
|
+
j
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
10
34
|
class DOMMatrix {
|
|
11
35
|
constructor(transform) {
|
|
12
36
|
_defineProperty(this, "_is2D", true);
|
|
13
|
-
|
|
14
37
|
_defineProperty(this, "m11", 1.0);
|
|
15
|
-
|
|
16
38
|
_defineProperty(this, "m12", 0.0);
|
|
17
|
-
|
|
18
39
|
_defineProperty(this, "m13", 0.0);
|
|
19
|
-
|
|
20
40
|
_defineProperty(this, "m14", 0.0);
|
|
21
|
-
|
|
22
41
|
_defineProperty(this, "m21", 0.0);
|
|
23
|
-
|
|
24
42
|
_defineProperty(this, "m22", 1.0);
|
|
25
|
-
|
|
26
43
|
_defineProperty(this, "m23", 0.0);
|
|
27
|
-
|
|
28
44
|
_defineProperty(this, "m24", 0.0);
|
|
29
|
-
|
|
30
45
|
_defineProperty(this, "m31", 0.0);
|
|
31
|
-
|
|
32
46
|
_defineProperty(this, "m32", 0.0);
|
|
33
|
-
|
|
34
47
|
_defineProperty(this, "m33", 1.0);
|
|
35
|
-
|
|
36
48
|
_defineProperty(this, "m34", 0.0);
|
|
37
|
-
|
|
38
49
|
_defineProperty(this, "m41", 0.0);
|
|
39
|
-
|
|
40
50
|
_defineProperty(this, "m42", 0.0);
|
|
41
|
-
|
|
42
51
|
_defineProperty(this, "m43", 0.0);
|
|
43
|
-
|
|
44
52
|
_defineProperty(this, "m44", 1.0);
|
|
45
|
-
|
|
46
53
|
if (transform && transform.length === 6) {
|
|
47
54
|
this.m11 = transform[0];
|
|
48
55
|
this.m12 = transform[1];
|
|
@@ -53,7 +60,6 @@ class DOMMatrix {
|
|
|
53
60
|
this._is2D = true;
|
|
54
61
|
return this;
|
|
55
62
|
}
|
|
56
|
-
|
|
57
63
|
if (transform && transform.length === 16) {
|
|
58
64
|
this.m11 = transform[0];
|
|
59
65
|
this.m12 = transform[1];
|
|
@@ -74,14 +80,11 @@ class DOMMatrix {
|
|
|
74
80
|
this._is2D = false;
|
|
75
81
|
return this;
|
|
76
82
|
}
|
|
77
|
-
|
|
78
83
|
if (transform) {
|
|
79
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.");
|
|
80
85
|
}
|
|
81
|
-
|
|
82
86
|
this._is2D = false;
|
|
83
87
|
}
|
|
84
|
-
|
|
85
88
|
get isIdentity() {
|
|
86
89
|
if (this._is2D) {
|
|
87
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;
|
|
@@ -89,67 +92,101 @@ class DOMMatrix {
|
|
|
89
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;
|
|
90
93
|
}
|
|
91
94
|
}
|
|
92
|
-
|
|
93
95
|
get a() {
|
|
94
96
|
return this.m11;
|
|
95
97
|
}
|
|
96
|
-
|
|
97
98
|
set a(value) {
|
|
98
99
|
this.m11 = value;
|
|
99
100
|
}
|
|
100
|
-
|
|
101
101
|
get b() {
|
|
102
102
|
return this.m12;
|
|
103
103
|
}
|
|
104
|
-
|
|
105
104
|
set b(value) {
|
|
106
105
|
this.m12 = value;
|
|
107
106
|
}
|
|
108
|
-
|
|
109
107
|
get c() {
|
|
110
108
|
return this.m21;
|
|
111
109
|
}
|
|
112
|
-
|
|
113
110
|
set c(value) {
|
|
114
111
|
this.m21 = value;
|
|
115
112
|
}
|
|
116
|
-
|
|
117
113
|
get d() {
|
|
118
114
|
return this.m22;
|
|
119
115
|
}
|
|
120
|
-
|
|
121
116
|
set d(value) {
|
|
122
117
|
this.m22 = value;
|
|
123
118
|
}
|
|
124
|
-
|
|
125
119
|
get e() {
|
|
126
120
|
return this.m41;
|
|
127
121
|
}
|
|
128
|
-
|
|
129
122
|
set e(value) {
|
|
130
123
|
this.m41 = value;
|
|
131
124
|
}
|
|
132
|
-
|
|
133
125
|
get f() {
|
|
134
126
|
return this.m42;
|
|
135
127
|
}
|
|
136
|
-
|
|
137
128
|
set f(value) {
|
|
138
129
|
this.m42 = value;
|
|
139
130
|
}
|
|
140
|
-
|
|
141
131
|
get is2D() {
|
|
142
132
|
return this._is2D;
|
|
143
133
|
}
|
|
144
|
-
|
|
145
134
|
toFloat32Array() {
|
|
146
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]);
|
|
147
136
|
}
|
|
148
|
-
|
|
149
137
|
toFloat64Array() {
|
|
150
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]);
|
|
151
139
|
}
|
|
152
|
-
|
|
140
|
+
translateSelf(x, y, z) {
|
|
141
|
+
const tx = Number(x),
|
|
142
|
+
ty = Number(y),
|
|
143
|
+
tz = isNaN(Number(z)) ? 0 : Number(z);
|
|
144
|
+
const translationMatrix = new DOMMatrix();
|
|
145
|
+
translationMatrix.m41 = tx;
|
|
146
|
+
translationMatrix.m42 = ty;
|
|
147
|
+
translationMatrix.m43 = tz;
|
|
148
|
+
multiplyMatrices(this, translationMatrix);
|
|
149
|
+
if (tz) {
|
|
150
|
+
this._is2D = false;
|
|
151
|
+
}
|
|
152
|
+
return this;
|
|
153
|
+
}
|
|
154
|
+
translate(x, y, z) {
|
|
155
|
+
let translatedMatrix;
|
|
156
|
+
if (this.is2D) {
|
|
157
|
+
translatedMatrix = new DOMMatrix([this.a, this.b, this.c, this.d, this.e, this.f]);
|
|
158
|
+
} else {
|
|
159
|
+
translatedMatrix = new DOMMatrix(this.toFloat32Array());
|
|
160
|
+
}
|
|
161
|
+
return translatedMatrix.translateSelf(x, y, z);
|
|
162
|
+
}
|
|
163
|
+
scaleSelf(scaleX, scaleY, scaleZ, originX, originY, originZ) {
|
|
164
|
+
const sx = Number(scaleX),
|
|
165
|
+
sy = isNaN(Number(scaleY)) ? sx : Number(scaleY),
|
|
166
|
+
sz = isNaN(Number(scaleZ)) ? 1 : Number(scaleZ);
|
|
167
|
+
const ox = isNaN(Number(originX)) ? 0 : Number(originX),
|
|
168
|
+
oy = isNaN(Number(originY)) ? 0 : Number(originY),
|
|
169
|
+
oz = isNaN(Number(originZ)) ? 0 : Number(originZ);
|
|
170
|
+
this.translateSelf(ox, oy, oz);
|
|
171
|
+
const scaleMatrix = new DOMMatrix();
|
|
172
|
+
scaleMatrix.m11 = sx;
|
|
173
|
+
scaleMatrix.m22 = sy;
|
|
174
|
+
scaleMatrix.m33 = sz;
|
|
175
|
+
multiplyMatrices(this, scaleMatrix);
|
|
176
|
+
this.translateSelf(-ox, -oy, -oz);
|
|
177
|
+
if (Math.abs(sz) !== 1) {
|
|
178
|
+
this._is2D = false;
|
|
179
|
+
}
|
|
180
|
+
return this;
|
|
181
|
+
}
|
|
182
|
+
scale(scaleX, scaleY, scaleZ, originX, originY, originZ) {
|
|
183
|
+
let scaledMatrix;
|
|
184
|
+
if (this.is2D) {
|
|
185
|
+
scaledMatrix = new DOMMatrix([this.a, this.b, this.c, this.d, this.e, this.f]);
|
|
186
|
+
} else {
|
|
187
|
+
scaledMatrix = new DOMMatrix(this.toFloat32Array());
|
|
188
|
+
}
|
|
189
|
+
return scaledMatrix.scaleSelf(scaleX, scaleY, scaleZ, originX, originY, originZ);
|
|
190
|
+
}
|
|
153
191
|
}
|
|
154
|
-
|
|
155
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,13 +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
|
-
|
|
31
|
+
for (let i = 0; i < path._path.length; i++) this._path.push(path._path[i]);
|
|
40
32
|
}
|
|
41
|
-
|
|
42
33
|
}
|
|
43
|
-
|
|
44
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;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jest-canvas-mock",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.0",
|
|
4
4
|
"description": "Mock a canvas in your jest tests.",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "types/index.d.ts",
|
|
@@ -48,7 +48,8 @@
|
|
|
48
48
|
"src/mock/**/*.js"
|
|
49
49
|
],
|
|
50
50
|
"setupFiles": [
|
|
51
|
-
"./src/index.js"
|
|
51
|
+
"./src/index.js",
|
|
52
|
+
"./__mocks__/worker.js"
|
|
52
53
|
]
|
|
53
54
|
},
|
|
54
55
|
"repository": {
|