jest-canvas-mock 2.5.0 → 2.5.2

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