jest-canvas-mock 2.1.2 → 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.
@@ -3,22 +3,12 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports["default"] = void 0;
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; }
6
+ exports.default = void 0;
13
7
 
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
- var DOMMatrix =
17
- /*#__PURE__*/
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
- _createClass(DOMMatrix, [{
96
- key: "isIdentity",
97
- get: function get() {
98
- if (this._is2D) {
99
- 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;
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
- key: "is2D",
154
- get: function get() {
155
- return this._is2D;
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
- return DOMMatrix;
160
- }();
145
+ }
161
146
 
162
- exports["default"] = DOMMatrix;
147
+ exports.default = DOMMatrix;
@@ -3,22 +3,12 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports["default"] = void 0;
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; }
6
+ exports.default = void 0;
13
7
 
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
- var ImageBitmap =
17
- /*#__PURE__*/
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
- _createClass(ImageBitmap, [{
34
- key: "close",
35
- value: function close() {
36
- this.width = 0;
37
- this.height = 0;
38
- this._closed = true;
39
- }
40
- }]);
23
+ close() {
24
+ this.width = 0;
25
+ this.height = 0;
26
+ this._closed = true;
27
+ }
41
28
 
42
- return ImageBitmap;
43
- }();
29
+ }
44
30
 
45
- exports["default"] = ImageBitmap;
31
+ exports.default = ImageBitmap;
@@ -3,39 +3,24 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports["default"] = void 0;
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; }
6
+ exports.default = void 0;
13
7
 
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
- var ImageData =
17
- /*#__PURE__*/
18
- function () {
19
- _createClass(ImageData, [{
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
- function ImageData(arr, w, h) {
37
- _classCallCheck(this, ImageData);
15
+ get height() {
16
+ return this._height;
17
+ }
18
+
19
+ get data() {
20
+ return this._data;
21
+ }
38
22
 
23
+ constructor(arr, w, h) {
39
24
  _defineProperty(this, "_width", 0);
40
25
 
41
26
  _defineProperty(this, "_height", 0);
@@ -52,8 +37,8 @@ function () {
52
37
  this._height = arr.length / 4 / w;
53
38
  this._data = arr;
54
39
  } else {
55
- var width = arr;
56
- var height = w;
40
+ const width = arr;
41
+ const height = w;
57
42
  if (!Number.isFinite(height)) throw new RangeError('The height is zero or not a number.');
58
43
  if (height === 0) throw new RangeError('The height is zero or not a number.');
59
44
  if (!Number.isFinite(width)) throw new RangeError('The width is zero or not a number.');
@@ -79,7 +64,6 @@ function () {
79
64
  }
80
65
  }
81
66
 
82
- return ImageData;
83
- }();
67
+ }
84
68
 
85
- exports["default"] = ImageData;
69
+ exports.default = ImageData;
@@ -3,32 +3,20 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports["default"] = void 0;
6
+ exports.default = void 0;
7
7
 
8
8
  var _CanvasRenderingContext2D = _interopRequireDefault(require("./CanvasRenderingContext2D"));
9
9
 
10
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
11
-
12
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
13
-
14
- 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); } }
15
-
16
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
11
 
18
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; }
19
13
 
20
14
  // Path2D.prototype
21
- var Path2DFunc = ['addPath'];
22
- var borrowedFromCanvas = ['closePath', 'moveTo', 'lineTo', 'bezierCurveTo', 'quadraticCurveTo', 'arc', 'arcTo', 'ellipse', 'rect'];
23
-
24
- var Path2D =
25
- /*#__PURE__*/
26
- function () {
27
- function Path2D() {
28
- var _this = this;
29
-
30
- _classCallCheck(this, Path2D);
15
+ const Path2DFunc = ['addPath'];
16
+ const borrowedFromCanvas = ['closePath', 'moveTo', 'lineTo', 'bezierCurveTo', 'quadraticCurveTo', 'arc', 'arcTo', 'ellipse', 'rect'];
31
17
 
18
+ class Path2D {
19
+ constructor() {
32
20
  _defineProperty(this, "_path", []);
33
21
 
34
22
  _defineProperty(this, "_events", []);
@@ -37,24 +25,20 @@ function () {
37
25
 
38
26
  _defineProperty(this, "_transformStack", [[1, 0, 0, 1, 0, 0]]);
39
27
 
40
- borrowedFromCanvas.forEach(function (key) {
41
- _this[key] = jest.fn(_CanvasRenderingContext2D["default"].prototype[key].bind(_this));
28
+ borrowedFromCanvas.forEach(key => {
29
+ this[key] = jest.fn(_CanvasRenderingContext2D.default.prototype[key].bind(this));
42
30
  });
43
- Path2DFunc.forEach(function (key) {
44
- _this[key] = jest.fn(_this[key].bind(_this));
31
+ Path2DFunc.forEach(key => {
32
+ this[key] = jest.fn(this[key].bind(this));
45
33
  });
46
34
  }
47
35
 
48
- _createClass(Path2D, [{
49
- key: "addPath",
50
- value: function addPath(path) {
51
- if (arguments.length < 1) throw new TypeError('Failed to execute \'addPath\' on \'Path2D\': 1 argument required, but only 0 present.');
52
- if (!(path instanceof Path2D)) throw new TypeError('Failed to execute \'addPath\' on \'Path2D\': parameter 1 is not of type \'Path2D\'.');
53
- this._path = this._path.concat(path._path);
54
- }
55
- }]);
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
+ }
56
41
 
57
- return Path2D;
58
- }();
42
+ }
59
43
 
60
- exports["default"] = Path2D;
44
+ exports.default = Path2D;
@@ -3,40 +3,39 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports["default"] = void 0;
7
-
8
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6
+ exports.default = void 0;
9
7
 
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
- var TextMetrics = function TextMetrics(text) {
13
- _classCallCheck(this, TextMetrics);
10
+ class TextMetrics {
11
+ constructor(text) {
12
+ _defineProperty(this, "width", 0);
14
13
 
15
- _defineProperty(this, "width", 0);
14
+ _defineProperty(this, "actualBoundingBoxLeft", 0);
16
15
 
17
- _defineProperty(this, "actualBoundingBoxLeft", 0);
16
+ _defineProperty(this, "actualBoundingBoxRight", 0);
18
17
 
19
- _defineProperty(this, "actualBoundingBoxRight", 0);
18
+ _defineProperty(this, "fontBoundingBoxAscent", 0);
20
19
 
21
- _defineProperty(this, "fontBoundingBoxAscent", 0);
20
+ _defineProperty(this, "fontBoundingBoxDescent", 0);
22
21
 
23
- _defineProperty(this, "fontBoundingBoxDescent", 0);
22
+ _defineProperty(this, "actualBoundingBoxAscent", 0);
24
23
 
25
- _defineProperty(this, "actualBoundingBoxAscent", 0);
24
+ _defineProperty(this, "actualBoundingBoxDescent", 0);
26
25
 
27
- _defineProperty(this, "actualBoundingBoxDescent", 0);
26
+ _defineProperty(this, "emHeightAscent", 0);
28
27
 
29
- _defineProperty(this, "emHeightAscent", 0);
28
+ _defineProperty(this, "emHeightDescent", 0);
30
29
 
31
- _defineProperty(this, "emHeightDescent", 0);
30
+ _defineProperty(this, "hangingBaseline", 0);
32
31
 
33
- _defineProperty(this, "hangingBaseline", 0);
32
+ _defineProperty(this, "alphabeticBaseline", 0);
34
33
 
35
- _defineProperty(this, "alphabeticBaseline", 0);
34
+ _defineProperty(this, "ideographicBaseline", 0);
36
35
 
37
- _defineProperty(this, "ideographicBaseline", 0);
36
+ this.width = text.length;
37
+ }
38
38
 
39
- this.width = text.length;
40
- };
39
+ }
41
40
 
42
- exports["default"] = TextMetrics;
41
+ exports.default = TextMetrics;
package/lib/index.js CHANGED
@@ -7,17 +7,17 @@ exports.ver = void 0;
7
7
 
8
8
  var _window = _interopRequireDefault(require("./window"));
9
9
 
10
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
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
18
18
  if (typeof window !== 'undefined') {
19
- global.window = (0, _window["default"])(window);
19
+ global.window = (0, _window.default)(window);
20
20
  }
21
21
 
22
- var ver = "2.1.2";
22
+ const ver = "2.2.0";
23
23
  exports.ver = ver;
@@ -3,27 +3,27 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports["default"] = createCanvasEvent;
6
+ exports.default = createCanvasEvent;
7
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
- * }
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
22
  */
23
23
  function createCanvasEvent(type, transform, props) {
24
24
  return {
25
- type: type,
26
- transform: transform,
27
- props: props
25
+ type,
26
+ transform,
27
+ props
28
28
  };
29
29
  }
@@ -3,31 +3,29 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports["default"] = void 0;
6
+ exports.default = void 0;
7
7
 
8
8
  var _ImageBitmap = _interopRequireDefault(require("../classes/ImageBitmap"));
9
9
 
10
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
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); }
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
11
 
14
12
  var _default = jest.fn(function createImageBitmap(img, sx, sy, sWidth, sHeight, options) {
15
13
  var length = arguments.length;
16
- return new Promise(function (resolve, reject) {
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
- var validImage = false;
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"
23
21
 
24
22
  if (img instanceof Blob || img && img.constructor && img.constructor.name === "Blob") validImage = true;
25
- if (img instanceof _ImageBitmap["default"]) validImage = true;
23
+ if (img instanceof _ImageBitmap.default) validImage = true;
26
24
  if (img instanceof ImageData) validImage = true;
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
- var index = 6;
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 (_typeof(options) !== 'object') throw new TypeError('Failed to execute \'createImageBitmap\' on \'Window\': parameter ' + index + ' (\'options\') is not an object.');
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
 
@@ -53,8 +51,8 @@ var _default = jest.fn(function createImageBitmap(img, sx, sy, sWidth, sHeight,
53
51
  sHeight = img.height || 1;
54
52
  }
55
53
 
56
- return resolve(new _ImageBitmap["default"](sWidth, sHeight));
54
+ return resolve(new _ImageBitmap.default(sWidth, sHeight));
57
55
  });
58
56
  });
59
57
 
60
- exports["default"] = _default;
58
+ exports.default = _default;