jest-canvas-mock 2.0.0 → 2.2.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/.github/FUNDING.yml +12 -0
- package/CHANGELOG.md +463 -427
- package/README.md +169 -101
- package/lib/classes/CanvasGradient.js +13 -27
- package/lib/classes/CanvasPattern.js +6 -20
- package/lib/classes/CanvasRenderingContext2D.js +1325 -694
- package/lib/classes/DOMMatrix.js +61 -76
- package/lib/classes/ImageBitmap.js +9 -23
- package/lib/classes/ImageData.js +48 -36
- package/lib/classes/Path2D.js +20 -25
- package/lib/classes/TextMetrics.js +17 -18
- package/lib/index.js +4 -4
- package/lib/mock/createCanvasEvent.js +29 -0
- package/lib/mock/createImageBitmap.js +4 -6
- package/lib/mock/prototype.js +42 -28
- package/lib/window.js +10 -10
- package/package.json +70 -67
- package/types/index.d.ts +86 -0
- package/lib/canvas.js +0 -28
- package/lib/classes/context2d.js +0 -107
package/lib/classes/DOMMatrix.js
CHANGED
|
@@ -5,20 +5,10 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
|
|
8
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
9
|
-
|
|
10
|
-
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
11
|
-
|
|
12
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
13
|
-
|
|
14
8
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
15
9
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
function () {
|
|
19
|
-
function DOMMatrix(transform) {
|
|
20
|
-
_classCallCheck(this, DOMMatrix);
|
|
21
|
-
|
|
10
|
+
class DOMMatrix {
|
|
11
|
+
constructor(transform) {
|
|
22
12
|
_defineProperty(this, "_is2D", true);
|
|
23
13
|
|
|
24
14
|
_defineProperty(this, "m11", 1.0);
|
|
@@ -92,71 +82,66 @@ function () {
|
|
|
92
82
|
this._is2D = false;
|
|
93
83
|
}
|
|
94
84
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
} else {
|
|
101
|
-
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;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}, {
|
|
105
|
-
key: "a",
|
|
106
|
-
get: function get() {
|
|
107
|
-
return this.m11;
|
|
108
|
-
},
|
|
109
|
-
set: function set(value) {
|
|
110
|
-
this.m11 = value;
|
|
111
|
-
}
|
|
112
|
-
}, {
|
|
113
|
-
key: "b",
|
|
114
|
-
get: function get() {
|
|
115
|
-
return this.m12;
|
|
116
|
-
},
|
|
117
|
-
set: function set(value) {
|
|
118
|
-
this.m12 = value;
|
|
119
|
-
}
|
|
120
|
-
}, {
|
|
121
|
-
key: "c",
|
|
122
|
-
get: function get() {
|
|
123
|
-
return this.m21;
|
|
124
|
-
},
|
|
125
|
-
set: function set(value) {
|
|
126
|
-
this.m21 = value;
|
|
127
|
-
}
|
|
128
|
-
}, {
|
|
129
|
-
key: "d",
|
|
130
|
-
get: function get() {
|
|
131
|
-
return this.m22;
|
|
132
|
-
},
|
|
133
|
-
set: function set(value) {
|
|
134
|
-
this.m22 = value;
|
|
135
|
-
}
|
|
136
|
-
}, {
|
|
137
|
-
key: "e",
|
|
138
|
-
get: function get() {
|
|
139
|
-
return this.m41;
|
|
140
|
-
},
|
|
141
|
-
set: function set(value) {
|
|
142
|
-
this.m41 = value;
|
|
143
|
-
}
|
|
144
|
-
}, {
|
|
145
|
-
key: "f",
|
|
146
|
-
get: function get() {
|
|
147
|
-
return this.m42;
|
|
148
|
-
},
|
|
149
|
-
set: function set(value) {
|
|
150
|
-
this.m42 = value;
|
|
85
|
+
get isIdentity() {
|
|
86
|
+
if (this._is2D) {
|
|
87
|
+
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;
|
|
88
|
+
} else {
|
|
89
|
+
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;
|
|
151
90
|
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
get a() {
|
|
94
|
+
return this.m11;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
set a(value) {
|
|
98
|
+
this.m11 = value;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
get b() {
|
|
102
|
+
return this.m12;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
set b(value) {
|
|
106
|
+
this.m12 = value;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
get c() {
|
|
110
|
+
return this.m21;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
set c(value) {
|
|
114
|
+
this.m21 = value;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
get d() {
|
|
118
|
+
return this.m22;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
set d(value) {
|
|
122
|
+
this.m22 = value;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
get e() {
|
|
126
|
+
return this.m41;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
set e(value) {
|
|
130
|
+
this.m41 = value;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
get f() {
|
|
134
|
+
return this.m42;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
set f(value) {
|
|
138
|
+
this.m42 = value;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
get is2D() {
|
|
142
|
+
return this._is2D;
|
|
143
|
+
}
|
|
158
144
|
|
|
159
|
-
|
|
160
|
-
}();
|
|
145
|
+
}
|
|
161
146
|
|
|
162
147
|
exports.default = DOMMatrix;
|
|
@@ -5,20 +5,10 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
|
|
8
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
9
|
-
|
|
10
|
-
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
11
|
-
|
|
12
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
13
|
-
|
|
14
8
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
15
9
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
function () {
|
|
19
|
-
function ImageBitmap(width, height) {
|
|
20
|
-
_classCallCheck(this, ImageBitmap);
|
|
21
|
-
|
|
10
|
+
class ImageBitmap {
|
|
11
|
+
constructor(width, height) {
|
|
22
12
|
_defineProperty(this, "width", 0);
|
|
23
13
|
|
|
24
14
|
_defineProperty(this, "height", 0);
|
|
@@ -30,16 +20,12 @@ function () {
|
|
|
30
20
|
this.close = jest.fn(this.close.bind(this));
|
|
31
21
|
}
|
|
32
22
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}]);
|
|
41
|
-
|
|
42
|
-
return ImageBitmap;
|
|
43
|
-
}();
|
|
23
|
+
close() {
|
|
24
|
+
this.width = 0;
|
|
25
|
+
this.height = 0;
|
|
26
|
+
this._closed = true;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
}
|
|
44
30
|
|
|
45
31
|
exports.default = ImageBitmap;
|
package/lib/classes/ImageData.js
CHANGED
|
@@ -5,53 +5,65 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
|
|
8
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
9
|
-
|
|
10
|
-
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
11
|
-
|
|
12
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
13
|
-
|
|
14
8
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
15
9
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
key: "width",
|
|
21
|
-
get: function get() {
|
|
22
|
-
return this._width;
|
|
23
|
-
}
|
|
24
|
-
}, {
|
|
25
|
-
key: "height",
|
|
26
|
-
get: function get() {
|
|
27
|
-
return this._height;
|
|
28
|
-
}
|
|
29
|
-
}, {
|
|
30
|
-
key: "data",
|
|
31
|
-
get: function get() {
|
|
32
|
-
return this._data;
|
|
33
|
-
}
|
|
34
|
-
}]);
|
|
10
|
+
class ImageData {
|
|
11
|
+
get width() {
|
|
12
|
+
return this._width;
|
|
13
|
+
}
|
|
35
14
|
|
|
36
|
-
|
|
37
|
-
|
|
15
|
+
get height() {
|
|
16
|
+
return this._height;
|
|
17
|
+
}
|
|
38
18
|
|
|
19
|
+
get data() {
|
|
20
|
+
return this._data;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
constructor(arr, w, h) {
|
|
39
24
|
_defineProperty(this, "_width", 0);
|
|
40
25
|
|
|
41
26
|
_defineProperty(this, "_height", 0);
|
|
42
27
|
|
|
43
28
|
_defineProperty(this, "_data", null);
|
|
44
29
|
|
|
45
|
-
if (
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
30
|
+
if (arguments.length === 2) {
|
|
31
|
+
if (arr instanceof Uint8ClampedArray) {
|
|
32
|
+
if (arr.length === 0) throw new RangeError('Source length must be a positive multiple of 4.');
|
|
33
|
+
if (arr.length % 4 !== 0) throw new RangeError('Source length must be a positive multiple of 4.');
|
|
34
|
+
if (!Number.isFinite(w)) throw new RangeError('The width is zero or not a number.');
|
|
35
|
+
if (w === 0) throw new RangeError('The width is zero or not a number.');
|
|
36
|
+
this._width = w;
|
|
37
|
+
this._height = arr.length / 4 / w;
|
|
38
|
+
this._data = arr;
|
|
39
|
+
} else {
|
|
40
|
+
const width = arr;
|
|
41
|
+
const height = w;
|
|
42
|
+
if (!Number.isFinite(height)) throw new RangeError('The height is zero or not a number.');
|
|
43
|
+
if (height === 0) throw new RangeError('The height is zero or not a number.');
|
|
44
|
+
if (!Number.isFinite(width)) throw new RangeError('The width is zero or not a number.');
|
|
45
|
+
if (width === 0) throw new RangeError('The width is zero or not a number.');
|
|
46
|
+
this._width = width;
|
|
47
|
+
this._height = height;
|
|
48
|
+
this._data = new Uint8ClampedArray(width * height * 4);
|
|
49
|
+
}
|
|
50
|
+
} else if (arguments.length === 3) {
|
|
51
|
+
if (!(arr instanceof Uint8ClampedArray)) throw new TypeError('First argument must be a Uint8ClampedArray when using 3 arguments.');
|
|
52
|
+
if (arr.length === 0) throw new RangeError('Source length must be a positive multiple of 4.');
|
|
53
|
+
if (arr.length % 4 !== 0) throw new RangeError('Source length must be a positive multiple of 4.');
|
|
54
|
+
if (!Number.isFinite(h)) throw new RangeError('The height is zero or not a number.');
|
|
55
|
+
if (h === 0) throw new RangeError('The height is zero or not a number.');
|
|
56
|
+
if (!Number.isFinite(w)) throw new RangeError('The width is zero or not a number.');
|
|
57
|
+
if (w === 0) throw new RangeError('The width is zero or not a number.');
|
|
58
|
+
if (arr.length !== w * h * 4) throw new RangeError('Source doesn\'n contain the exact number of pixels needed.');
|
|
59
|
+
this._width = w;
|
|
60
|
+
this._height = h;
|
|
61
|
+
this._data = arr;
|
|
62
|
+
} else {
|
|
63
|
+
throw new TypeError('Wrong number of arguments provided.');
|
|
64
|
+
}
|
|
52
65
|
}
|
|
53
66
|
|
|
54
|
-
|
|
55
|
-
}();
|
|
67
|
+
}
|
|
56
68
|
|
|
57
69
|
exports.default = ImageData;
|
package/lib/classes/Path2D.js
CHANGED
|
@@ -9,41 +9,36 @@ var _CanvasRenderingContext2D = _interopRequireDefault(require("./CanvasRenderin
|
|
|
9
9
|
|
|
10
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
11
|
|
|
12
|
-
function
|
|
12
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
// Path2D.prototype
|
|
15
|
+
const Path2DFunc = ['addPath'];
|
|
16
|
+
const borrowedFromCanvas = ['closePath', 'moveTo', 'lineTo', 'bezierCurveTo', 'quadraticCurveTo', 'arc', 'arcTo', 'ellipse', 'rect'];
|
|
15
17
|
|
|
16
|
-
|
|
18
|
+
class Path2D {
|
|
19
|
+
constructor() {
|
|
20
|
+
_defineProperty(this, "_path", []);
|
|
17
21
|
|
|
18
|
-
|
|
19
|
-
var Path2DFunc = ['addPath'];
|
|
20
|
-
var borrowedFromCanvas = ['closePath', 'moveTo', 'lineTo', 'bezierCurveTo', 'quadraticCurveTo', 'arc', 'arcTo', 'ellipse', 'rect'];
|
|
22
|
+
_defineProperty(this, "_events", []);
|
|
21
23
|
|
|
22
|
-
|
|
23
|
-
/*#__PURE__*/
|
|
24
|
-
function () {
|
|
25
|
-
function Path2D() {
|
|
26
|
-
var _this = this;
|
|
24
|
+
_defineProperty(this, "_stackIndex", 0);
|
|
27
25
|
|
|
28
|
-
|
|
26
|
+
_defineProperty(this, "_transformStack", [[1, 0, 0, 1, 0, 0]]);
|
|
29
27
|
|
|
30
|
-
borrowedFromCanvas.forEach(
|
|
31
|
-
|
|
28
|
+
borrowedFromCanvas.forEach(key => {
|
|
29
|
+
this[key] = jest.fn(_CanvasRenderingContext2D.default.prototype[key].bind(this));
|
|
32
30
|
});
|
|
33
|
-
Path2DFunc.forEach(
|
|
34
|
-
|
|
31
|
+
Path2DFunc.forEach(key => {
|
|
32
|
+
this[key] = jest.fn(this[key].bind(this));
|
|
35
33
|
});
|
|
36
34
|
}
|
|
37
35
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
}]);
|
|
36
|
+
addPath(path) {
|
|
37
|
+
if (arguments.length < 1) throw new TypeError('Failed to execute \'addPath\' on \'Path2D\': 1 argument required, but only 0 present.');
|
|
38
|
+
if (!(path instanceof Path2D)) throw new TypeError('Failed to execute \'addPath\' on \'Path2D\': parameter 1 is not of type \'Path2D\'.');
|
|
39
|
+
this._path = this._path.concat(path._path);
|
|
40
|
+
}
|
|
45
41
|
|
|
46
|
-
|
|
47
|
-
}();
|
|
42
|
+
}
|
|
48
43
|
|
|
49
44
|
exports.default = Path2D;
|
|
@@ -5,38 +5,37 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
|
|
8
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
9
|
-
|
|
10
8
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
11
9
|
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
class TextMetrics {
|
|
11
|
+
constructor(text) {
|
|
12
|
+
_defineProperty(this, "width", 0);
|
|
14
13
|
|
|
15
|
-
|
|
14
|
+
_defineProperty(this, "actualBoundingBoxLeft", 0);
|
|
16
15
|
|
|
17
|
-
|
|
16
|
+
_defineProperty(this, "actualBoundingBoxRight", 0);
|
|
18
17
|
|
|
19
|
-
|
|
18
|
+
_defineProperty(this, "fontBoundingBoxAscent", 0);
|
|
20
19
|
|
|
21
|
-
|
|
20
|
+
_defineProperty(this, "fontBoundingBoxDescent", 0);
|
|
22
21
|
|
|
23
|
-
|
|
22
|
+
_defineProperty(this, "actualBoundingBoxAscent", 0);
|
|
24
23
|
|
|
25
|
-
|
|
24
|
+
_defineProperty(this, "actualBoundingBoxDescent", 0);
|
|
26
25
|
|
|
27
|
-
|
|
26
|
+
_defineProperty(this, "emHeightAscent", 0);
|
|
28
27
|
|
|
29
|
-
|
|
28
|
+
_defineProperty(this, "emHeightDescent", 0);
|
|
30
29
|
|
|
31
|
-
|
|
30
|
+
_defineProperty(this, "hangingBaseline", 0);
|
|
32
31
|
|
|
33
|
-
|
|
32
|
+
_defineProperty(this, "alphabeticBaseline", 0);
|
|
34
33
|
|
|
35
|
-
|
|
34
|
+
_defineProperty(this, "ideographicBaseline", 0);
|
|
36
35
|
|
|
37
|
-
|
|
36
|
+
this.width = text.length;
|
|
37
|
+
}
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
};
|
|
39
|
+
}
|
|
41
40
|
|
|
42
41
|
exports.default = TextMetrics;
|
package/lib/index.js
CHANGED
|
@@ -9,9 +9,9 @@ var _window = _interopRequireDefault(require("./window"));
|
|
|
9
9
|
|
|
10
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
11
|
|
|
12
|
-
/**
|
|
13
|
-
* Created by hustcc 17/12/25.
|
|
14
|
-
* Contract: i@hust.cc
|
|
12
|
+
/**
|
|
13
|
+
* Created by hustcc 17/12/25.
|
|
14
|
+
* Contract: i@hust.cc
|
|
15
15
|
*/
|
|
16
16
|
// mock global window
|
|
17
17
|
// TODO: Force coverage to ignore this branch
|
|
@@ -19,5 +19,5 @@ if (typeof window !== 'undefined') {
|
|
|
19
19
|
global.window = (0, _window.default)(window);
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
const ver = "2.2.0";
|
|
23
23
|
exports.ver = ver;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = createCanvasEvent;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* This function returns a CanvasRenderingContext2DEvent. Whenever an operation would modify the canvas
|
|
10
|
+
* context, this function should be used to generate an "event" that represents that sort of modification.
|
|
11
|
+
* This will be used to mock for snapshots.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* interface CanvasRenderingContext2DEvent {
|
|
15
|
+
* type: string;
|
|
16
|
+
* transform: [number, number, number, number, number, number]; // the resulting current transform
|
|
17
|
+
* props: {
|
|
18
|
+
* // if the event is a property was set, `event.props.value` would be set
|
|
19
|
+
* [propName: string]: any;
|
|
20
|
+
* };
|
|
21
|
+
* }
|
|
22
|
+
*/
|
|
23
|
+
function createCanvasEvent(type, transform, props) {
|
|
24
|
+
return {
|
|
25
|
+
type,
|
|
26
|
+
transform,
|
|
27
|
+
props
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -9,14 +9,12 @@ var _ImageBitmap = _interopRequireDefault(require("../classes/ImageBitmap"));
|
|
|
9
9
|
|
|
10
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
11
|
|
|
12
|
-
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
|
13
|
-
|
|
14
12
|
var _default = jest.fn(function createImageBitmap(img, sx, sy, sWidth, sHeight, options) {
|
|
15
13
|
var length = arguments.length;
|
|
16
|
-
return new Promise(
|
|
14
|
+
return new Promise((resolve, reject) => {
|
|
17
15
|
if (length === 0) return reject(new TypeError('Failed to execute \'createImageBitmap\' on \'Window\': 1 argument required, but only 0 present.'));
|
|
18
16
|
if (length === 3 || length === 4) return reject(new TypeError('Failed to execute \'createImageBitmap\' on \'Window\': Valid arities are: [1, 2, 5, 6], but ' + length + ' arguments provided.'));
|
|
19
|
-
|
|
17
|
+
let validImage = false;
|
|
20
18
|
if (img instanceof HTMLImageElement) validImage = true;
|
|
21
19
|
if (img instanceof HTMLVideoElement) validImage = true;
|
|
22
20
|
if (img instanceof HTMLCanvasElement) validImage = true; // checking constructor name is the only reliable way to verify the object's constructing class is "blob-like"
|
|
@@ -27,7 +25,7 @@ var _default = jest.fn(function createImageBitmap(img, sx, sy, sWidth, sHeight,
|
|
|
27
25
|
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)\''));
|
|
28
26
|
|
|
29
27
|
if (length >= 2) {
|
|
30
|
-
|
|
28
|
+
let index = 6;
|
|
31
29
|
|
|
32
30
|
if (length === 2) {
|
|
33
31
|
index = 2;
|
|
@@ -37,7 +35,7 @@ var _default = jest.fn(function createImageBitmap(img, sx, sy, sWidth, sHeight,
|
|
|
37
35
|
if (length === 5) options = null;
|
|
38
36
|
|
|
39
37
|
if (options !== null && options !== void 0) {
|
|
40
|
-
if (
|
|
38
|
+
if (typeof options !== 'object') throw new TypeError('Failed to execute \'createImageBitmap\' on \'Window\': parameter ' + index + ' (\'options\') is not an object.');
|
|
41
39
|
}
|
|
42
40
|
}
|
|
43
41
|
|
package/lib/mock/prototype.js
CHANGED
|
@@ -6,13 +6,29 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.default = mockPrototype;
|
|
7
7
|
|
|
8
8
|
function mockPrototype() {
|
|
9
|
-
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* the
|
|
9
|
+
/**
|
|
10
|
+
* This weakmap is designed to contain all of the generated canvas contexts. It's keys are the
|
|
11
|
+
* jsdom canvases obtained by using the `this` keyword inside the `#getContext('2d')` function
|
|
12
|
+
* call. It's values are the generated `CanvasRenderingContext2D` objects.
|
|
13
13
|
*/
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
const generatedContexts = new WeakMap();
|
|
15
|
+
/**
|
|
16
|
+
* Overrides getContext. Every test run will create a new function that overrides the current
|
|
17
|
+
* value of getContext. It attempts to preserve the original getContext function by storing it on
|
|
18
|
+
* the callback as a property.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
const getContext2D = jest.fn(function getContext2d(type) {
|
|
22
|
+
if (type === '2d') {
|
|
23
|
+
/**
|
|
24
|
+
* Contexts must be indempotent. Once they are generated, they should be returned when
|
|
25
|
+
* getContext() is called on the same canvas object multiple times.
|
|
26
|
+
*/
|
|
27
|
+
if (generatedContexts.has(this)) return generatedContexts.get(this);
|
|
28
|
+
const ctx = new CanvasRenderingContext2D(this);
|
|
29
|
+
generatedContexts.set(this, ctx);
|
|
30
|
+
return ctx;
|
|
31
|
+
}
|
|
16
32
|
|
|
17
33
|
try {
|
|
18
34
|
if (!this.dataset.internalRequireTest) require('canvas');
|
|
@@ -30,17 +46,17 @@ function mockPrototype() {
|
|
|
30
46
|
}
|
|
31
47
|
|
|
32
48
|
HTMLCanvasElement.prototype.getContext = getContext2D;
|
|
33
|
-
/**
|
|
34
|
-
* This function technically throws SecurityError at runtime, but it cannot be mocked, because
|
|
35
|
-
* we don't know if the canvas is tainted. These kinds of errors will be silent.
|
|
49
|
+
/**
|
|
50
|
+
* This function technically throws SecurityError at runtime, but it cannot be mocked, because
|
|
51
|
+
* we don't know if the canvas is tainted. These kinds of errors will be silent.
|
|
36
52
|
*/
|
|
37
53
|
|
|
38
|
-
|
|
54
|
+
const toBlobOverride = jest.fn(function toBlobOverride(callback, mimetype) {
|
|
39
55
|
if (arguments.length < 1) throw new TypeError('Failed to execute \'toBlob\' on \'HTMLCanvasElement\': 1 argument required, but only 0 present.');
|
|
40
56
|
if (typeof callback !== 'function') throw new TypeError('Failed to execute \'toBlob\' on \'HTMLCanvasElement\': The callback provided as parameter 1 is not a function.');
|
|
41
|
-
/**
|
|
42
|
-
* Mime type must be image/jpeg or image/webp exactly for the browser to accept it, otherwise
|
|
43
|
-
* it's image/png.
|
|
57
|
+
/**
|
|
58
|
+
* Mime type must be image/jpeg or image/webp exactly for the browser to accept it, otherwise
|
|
59
|
+
* it's image/png.
|
|
44
60
|
*/
|
|
45
61
|
|
|
46
62
|
switch (mimetype) {
|
|
@@ -53,20 +69,18 @@ function mockPrototype() {
|
|
|
53
69
|
default:
|
|
54
70
|
mimetype = 'image/png';
|
|
55
71
|
}
|
|
56
|
-
/**
|
|
57
|
-
* This section creates a blob of size width * height * 4. This is not actually valid, because
|
|
58
|
-
* jpeg size is variable, and so is png. TODO: Is there a better way to do this?
|
|
72
|
+
/**
|
|
73
|
+
* This section creates a blob of size width * height * 4. This is not actually valid, because
|
|
74
|
+
* jpeg size is variable, and so is png. TODO: Is there a better way to do this?
|
|
59
75
|
*/
|
|
60
76
|
|
|
61
77
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
78
|
+
const length = this.width * this.height * 4;
|
|
79
|
+
const data = new Uint8Array(length);
|
|
80
|
+
const blob = new window.Blob([data], {
|
|
65
81
|
type: mimetype
|
|
66
82
|
});
|
|
67
|
-
setTimeout(
|
|
68
|
-
return callback(blob);
|
|
69
|
-
}, 0);
|
|
83
|
+
setTimeout(() => callback(blob), 0);
|
|
70
84
|
});
|
|
71
85
|
|
|
72
86
|
if (!jest.isMockFunction(HTMLCanvasElement.prototype.toBlob)) {
|
|
@@ -76,12 +90,12 @@ function mockPrototype() {
|
|
|
76
90
|
}
|
|
77
91
|
|
|
78
92
|
HTMLCanvasElement.prototype.toBlob = toBlobOverride;
|
|
79
|
-
/**
|
|
80
|
-
* This section creates a dataurl with a validated mime type. This is not actually valid, because
|
|
81
|
-
* jpeg size is variable, and so is png. TODO: Is there a better way to do this?
|
|
93
|
+
/**
|
|
94
|
+
* This section creates a dataurl with a validated mime type. This is not actually valid, because
|
|
95
|
+
* jpeg size is variable, and so is png. TODO: Is there a better way to do this?
|
|
82
96
|
*/
|
|
83
97
|
|
|
84
|
-
|
|
98
|
+
const toDataURLOverride = jest.fn(function toDataURLOverride(type, encoderOptions) {
|
|
85
99
|
switch (type) {
|
|
86
100
|
case 'image/jpeg':
|
|
87
101
|
break;
|
|
@@ -92,8 +106,8 @@ function mockPrototype() {
|
|
|
92
106
|
default:
|
|
93
107
|
type = 'image/png';
|
|
94
108
|
}
|
|
95
|
-
/**
|
|
96
|
-
* This is the smallest valid data url I could generate.
|
|
109
|
+
/**
|
|
110
|
+
* This is the smallest valid data url I could generate.
|
|
97
111
|
*/
|
|
98
112
|
|
|
99
113
|
|
package/lib/window.js
CHANGED
|
@@ -27,13 +27,13 @@ var _createImageBitmap = _interopRequireDefault(require("./mock/createImageBitma
|
|
|
27
27
|
|
|
28
28
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
29
29
|
|
|
30
|
-
/**
|
|
31
|
-
* Created by hustcc 17/12/25.
|
|
32
|
-
* Contract: i@hust.cc
|
|
30
|
+
/**
|
|
31
|
+
* Created by hustcc 17/12/25.
|
|
32
|
+
* Contract: i@hust.cc
|
|
33
33
|
*/
|
|
34
|
-
var _default =
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
var _default = win => {
|
|
35
|
+
const d = win.document;
|
|
36
|
+
const f = win.document.createElement; // jsdom@11.6.2 || jest@^22.0.0, console.error in Function getContext();
|
|
37
37
|
// https://github.com/jsdom/jsdom/blob/4c7698f760fc64f20b2a0ddff450eddbdd193176/lib/jsdom/living/nodes/HTMLCanvasElement-impl.js#L55-L58
|
|
38
38
|
// cosole.error will make ci error.
|
|
39
39
|
// try {
|
|
@@ -54,10 +54,10 @@ var _default = function _default(win) {
|
|
|
54
54
|
// if ctx not exist, mock it.
|
|
55
55
|
// just mock canvas creator.
|
|
56
56
|
|
|
57
|
-
/*
|
|
58
|
-
win.document.createElement = param => param.toString().toLowerCase() === 'canvas'
|
|
59
|
-
? createCanvas('canvas')
|
|
60
|
-
: f.call(d, param);
|
|
57
|
+
/*
|
|
58
|
+
win.document.createElement = param => param.toString().toLowerCase() === 'canvas'
|
|
59
|
+
? createCanvas('canvas')
|
|
60
|
+
: f.call(d, param);
|
|
61
61
|
*/
|
|
62
62
|
// if not exist, then mock it.
|
|
63
63
|
|