jest-webgl-canvas-mock
Advanced tools
Comparing version 0.2.2 to 0.2.3
@@ -20,4 +20,2 @@ "use strict"; | ||
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; } | ||
var HTMLCanvasElement = | ||
@@ -29,9 +27,4 @@ /*#__PURE__*/ | ||
_defineProperty(this, "generatedContexts", new WeakMap()); | ||
this.width = width || 100; | ||
this.height = height || 100; | ||
this.getContext = jest.fn(this.getContext.bind(this)); | ||
this.toBlob = jest.fn(this.toBlob.bind(this)); | ||
this.toDataURL = jest.fn(this.toDataURL.bind(this)); | ||
} | ||
@@ -41,92 +34,12 @@ | ||
key: "getContext", | ||
value: function getContext(type) { | ||
/** | ||
* Contexts must be indempotent. Once they are generated, they should be returned when | ||
* getContext() is called on the same canvas object multiple times. | ||
*/ | ||
if (type === '2d') { | ||
if (this.generatedContexts.has(this)) return this.generatedContexts.get(this); | ||
var ctx = new _CanvasRenderingContext2D["default"](this); | ||
this.generatedContexts.set(this, ctx); | ||
return ctx; | ||
} else if (type === 'webgl' || type === 'experimental-webgl') { | ||
if (this.generatedContexts.has(this)) return this.generatedContexts.get(this); | ||
value: function getContext(arg) { | ||
switch (arg) { | ||
case "2d": | ||
return new _CanvasRenderingContext2D["default"](this); | ||
var _ctx = new _WebGLRenderingContext["default"](this); | ||
this.generatedContexts.set(this, _ctx); | ||
return _ctx; | ||
case "webgl": | ||
case "experimental-webgl": | ||
return new _WebGLRenderingContext["default"](this); | ||
} | ||
try { | ||
if (!this.dataset.internalRequireTest) require('canvas'); | ||
} catch (_unused) { | ||
return null; | ||
} | ||
} | ||
/** | ||
* This function technically throws SecurityError at runtime, but it cannot be mocked, because | ||
* we don't know if the canvas is tainted. These kinds of errors will be silent. | ||
*/ | ||
}, { | ||
key: "toBlob", | ||
value: function toBlob(callback, mimetype) { | ||
if (arguments.length < 1) throw new TypeError('Failed to execute \'toBlob\' on \'HTMLCanvasElement\': 1 argument required, but only 0 present.'); | ||
if (typeof callback !== 'function') throw new TypeError('Failed to execute \'toBlob\' on \'HTMLCanvasElement\': The callback provided as parameter 1 is not a function.'); | ||
/** | ||
* Mime type must be image/jpeg or image/webp exactly for the browser to accept it, otherwise | ||
* it's image/png. | ||
*/ | ||
switch (mimetype) { | ||
case 'image/webp': | ||
break; | ||
case 'image/jpeg': | ||
break; | ||
default: | ||
mimetype = 'image/png'; | ||
} | ||
/** | ||
* This section creates a blob of size width * height * 4. This is not actually valid, because | ||
* jpeg size is variable, and so is png. TODO: Is there a better way to do this? | ||
*/ | ||
var length = this.width * this.height * 4; | ||
var data = new Uint8Array(length); | ||
var blob = new window.Blob([data], { | ||
type: mimetype | ||
}); | ||
setTimeout(function () { | ||
return callback(blob); | ||
}, 0); | ||
} | ||
/** | ||
* This section creates a dataurl with a validated mime type. This is not actually valid, because | ||
* jpeg size is variable, and so is png. TODO: Is there a better way to do this? | ||
*/ | ||
}, { | ||
key: "toDataURL", | ||
value: function toDataURL(type, encoderOptions) { | ||
switch (type) { | ||
case 'image/jpeg': | ||
break; | ||
case 'image/webp': | ||
break; | ||
default: | ||
type = 'image/png'; | ||
} | ||
/** | ||
* This is the smallest valid data url I could generate. | ||
*/ | ||
return 'data:' + type + ';base64,00'; | ||
} | ||
}]); | ||
@@ -133,0 +46,0 @@ |
@@ -363,3 +363,3 @@ "use strict"; | ||
WebGLRenderingContext.prototype[key] = function () { | ||
return {}; | ||
return key === 'getSupportedExtensions' ? [] : {}; | ||
}; | ||
@@ -366,0 +366,0 @@ }); |
@@ -22,3 +22,3 @@ "use strict"; | ||
var ver = "0.2.2"; | ||
var ver = "0.2.3"; | ||
exports.ver = ver; |
{ | ||
"name": "jest-webgl-canvas-mock", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"description": "Mock both 2D and WebGL contexts in Jest.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
443417
22
10148
1