jest-canvas-mock 2.5.1 → 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/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.1";
20
+ const ver = "2.5.2";
21
21
  exports.ver = ver;
22
22
  function setupJestCanvasMock(window) {
23
23
  (0, _window.default)(window || global.window);
@@ -5,6 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = mockPrototype;
7
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(win) {
34
35
  }
35
36
  return getContext2D.internal.call(this, type);
36
37
  });
37
- if (!jest.isMockFunction(win.HTMLCanvasElement.prototype.getContext)) {
38
- getContext2D.internal = win.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 = win.HTMLCanvasElement.prototype.getContext.internal;
46
+ getContext2D.internal = htmlCanvasElementPrototype.getContext.internal;
41
47
  }
42
- win.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(win) {
73
79
  });
74
80
  setTimeout(() => callback(blob), 0);
75
81
  });
76
- if (!jest.isMockFunction(win.HTMLCanvasElement.prototype.toBlob)) {
77
- toBlobOverride.internal = win.HTMLCanvasElement.prototype.toBlob;
82
+ if (!jest.isMockFunction(htmlCanvasElementPrototype.toBlob)) {
83
+ toBlobOverride.internal = htmlCanvasElementPrototype.toBlob;
78
84
  } else {
79
- toBlobOverride.internal = win.HTMLCanvasElement.prototype.toBlob.internal;
85
+ toBlobOverride.internal = htmlCanvasElementPrototype.toBlob.internal;
80
86
  }
81
- win.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(win) {
99
105
  */
100
106
  return 'data:' + type + ';base64,00';
101
107
  });
102
- if (!jest.isMockFunction(win.HTMLCanvasElement.prototype.toDataURL)) {
103
- toDataURLOverride.internal = win.HTMLCanvasElement.prototype.toDataURL;
108
+ if (!jest.isMockFunction(htmlCanvasElementPrototype.toDataURL)) {
109
+ toDataURLOverride.internal = htmlCanvasElementPrototype.toDataURL;
104
110
  } else {
105
- toDataURLOverride.internal = win.HTMLCanvasElement.prototype.toDataURL.internal;
111
+ toDataURLOverride.internal = htmlCanvasElementPrototype.toDataURL.internal;
106
112
  }
107
- win.HTMLCanvasElement.prototype.toDataURL = toDataURLOverride;
113
+ htmlCanvasElementPrototype.toDataURL = toDataURLOverride;
108
114
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jest-canvas-mock",
3
- "version": "2.5.1",
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",