jest-webgl-canvas-mock
Advanced tools
Comparing version 0.2.1 to 0.2.2
@@ -93,3 +93,3 @@ "use strict"; | ||
function CanvasRenderingContext2D(canvas) { | ||
function CanvasRenderingContext2D(canvas, contextAttributes) { | ||
var _this = this; | ||
@@ -155,2 +155,3 @@ | ||
this._canvas = canvas; | ||
this._contextAttributes = contextAttributes; | ||
} | ||
@@ -185,2 +186,7 @@ | ||
}, { | ||
key: "getContextAttributes", | ||
value: function getContextAttributes() { | ||
return this._contextAttributes; | ||
} | ||
}, { | ||
key: "arc", | ||
@@ -187,0 +193,0 @@ value: function arc(x, y, radius, startAngle, endAngle) { |
@@ -20,2 +20,4 @@ "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 = | ||
@@ -27,4 +29,9 @@ /*#__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)); | ||
} | ||
@@ -34,12 +41,92 @@ | ||
key: "getContext", | ||
value: function getContext(arg) { | ||
switch (arg) { | ||
case "2d": | ||
return new _CanvasRenderingContext2D["default"](this); | ||
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); | ||
case "webgl": | ||
case "experimental-webgl": | ||
return new _WebGLRenderingContext["default"](this); | ||
var _ctx = new _WebGLRenderingContext["default"](this); | ||
this.generatedContexts.set(this, _ctx); | ||
return _ctx; | ||
} | ||
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'; | ||
} | ||
}]); | ||
@@ -46,0 +133,0 @@ |
@@ -14,3 +14,3 @@ "use strict"; | ||
var testFuncs = ['activeTexture', 'attachShader', 'bindAttribLocation', 'bindBuffer', 'bindFramebuffer', 'bindRenderbuffer', 'bindTexture', 'blendColor', 'blendEquation', 'blendEquationSeparate', 'blendFunc', 'blendFuncSeparate', 'bufferData', 'bufferSubData', 'checkFramebufferStatus', 'clear', 'clearColor', 'clearDepth', 'clearStencil', 'colorMask', 'compileShader', 'compressedTexImage2D', 'compressedTexSubImage2D', 'copyTexImage2D', 'copyTexSubImage2D', 'createBuffer', 'createFramebuffer', 'createProgram', 'createRenderbuffer', 'createShader', 'createTexture', 'cullFace', 'deleteBuffer', 'deleteFramebuffer', 'deleteProgram', 'deleteRenderbuffer', 'deleteShader', 'deleteTexture', 'depthFunc', 'depthMask', 'depthRange', 'detachShader', 'disable', 'disableVertexAttribArray', 'drawArrays', 'drawElements', 'enable', 'enableVertexAttribArray', 'finish', 'flush', 'framebufferRenderbuffer', 'framebufferTexture2D', 'frontFace', 'generateMipmap', 'getActiveAttrib', 'getActiveUniform', 'getAttachedShaders', 'getAttribLocation', 'getBufferParameter', 'getContextAttributes', 'getError', 'getFramebufferAttachmentParameter', 'getProgramParameter', 'getProgramInfoLog', 'getRenderbufferParameter', 'getShaderParameter', 'getShaderInfoLog', 'getShaderPrecisionFormat', 'getShaderSource', 'getSupportedExtensions', 'getTexParameter', 'getUniform', 'getUniformLocation', 'getVertexAttrib', 'getVertexAttribOffset', 'hint', 'isBuffer', 'isContextLost', 'isEnabled', 'isFramebuffer', 'isProgram', 'isRenderbuffer', 'isShader', 'isTexture', 'lineWidth', 'linkProgram', 'pixelStorei', 'polygonOffset', 'readPixels', 'renderbufferStorage', 'sampleCoverage', 'scissor', 'shaderSource', 'stencilFunc', 'stencilFuncSeparate', 'stencilMask', 'stencilMaskSeparate', 'stencilOp', 'stencilOpSeparate', 'texParameterf', 'texParameteri', 'texImage2D', 'texSubImage2D', 'uniform1f', 'uniform1fv', 'uniform1i', 'uniform1iv', 'uniform2f', 'uniform2fv', 'uniform2i', 'uniform2iv', 'uniform3f', 'uniform3fv', 'uniform3i', 'uniform3iv', 'uniform4f', 'uniform4fv', 'uniform4i', 'uniform4iv', 'uniformMatrix2fv', 'uniformMatrix3fv', 'uniformMatrix4fv', 'useProgram', 'validateProgram', 'vertexAttrib1f', 'vertexAttrib1fv', 'vertexAttrib2f', 'vertexAttrib2fv', 'vertexAttrib3f', 'vertexAttrib3fv', 'vertexAttrib4f', 'vertexAttrib4fv', 'vertexAttribPointer', 'viewport']; | ||
var testFuncs = ['activeTexture', 'attachShader', 'bindAttribLocation', 'bindBuffer', 'bindFramebuffer', 'bindRenderbuffer', 'bindTexture', 'blendColor', 'blendEquation', 'blendEquationSeparate', 'blendFunc', 'blendFuncSeparate', 'bufferData', 'bufferSubData', 'checkFramebufferStatus', 'clear', 'clearColor', 'clearDepth', 'clearStencil', 'colorMask', 'compileShader', 'compressedTexImage2D', 'compressedTexSubImage2D', 'copyTexImage2D', 'copyTexSubImage2D', 'createBuffer', 'createFramebuffer', 'createProgram', 'createRenderbuffer', 'createShader', 'createTexture', 'cullFace', 'deleteBuffer', 'deleteFramebuffer', 'deleteProgram', 'deleteRenderbuffer', 'deleteShader', 'deleteTexture', 'depthFunc', 'depthMask', 'depthRange', 'detachShader', 'disable', 'disableVertexAttribArray', 'drawArrays', 'drawElements', 'enable', 'enableVertexAttribArray', 'finish', 'flush', 'framebufferRenderbuffer', 'framebufferTexture2D', 'frontFace', 'generateMipmap', 'getActiveAttrib', 'getActiveUniform', 'getAttachedShaders', 'getAttribLocation', 'getBufferParameter', 'getError', 'getFramebufferAttachmentParameter', 'getProgramParameter', 'getProgramInfoLog', 'getRenderbufferParameter', 'getShaderParameter', 'getShaderInfoLog', 'getShaderPrecisionFormat', 'getShaderSource', 'getSupportedExtensions', 'getTexParameter', 'getUniform', 'getUniformLocation', 'getVertexAttrib', 'getVertexAttribOffset', 'hint', 'isBuffer', 'isContextLost', 'isEnabled', 'isFramebuffer', 'isProgram', 'isRenderbuffer', 'isShader', 'isTexture', 'lineWidth', 'linkProgram', 'pixelStorei', 'polygonOffset', 'readPixels', 'renderbufferStorage', 'sampleCoverage', 'scissor', 'shaderSource', 'stencilFunc', 'stencilFuncSeparate', 'stencilMask', 'stencilMaskSeparate', 'stencilOp', 'stencilOpSeparate', 'texParameterf', 'texParameteri', 'texImage2D', 'texSubImage2D', 'uniform1f', 'uniform1fv', 'uniform1i', 'uniform1iv', 'uniform2f', 'uniform2fv', 'uniform2i', 'uniform2iv', 'uniform3f', 'uniform3fv', 'uniform3i', 'uniform3iv', 'uniform4f', 'uniform4fv', 'uniform4i', 'uniform4iv', 'uniformMatrix2fv', 'uniformMatrix3fv', 'uniformMatrix4fv', 'useProgram', 'validateProgram', 'vertexAttrib1f', 'vertexAttrib1fv', 'vertexAttrib2f', 'vertexAttrib2fv', 'vertexAttrib3f', 'vertexAttrib3fv', 'vertexAttrib4f', 'vertexAttrib4fv', 'vertexAttribPointer', 'viewport']; | ||
var mockEnums = { | ||
@@ -320,3 +320,4 @@ 'DEPTH_BUFFER_BIT': 256, | ||
'WEBGL_lose_context': { | ||
restoreContext: function restoreContext() {} | ||
restoreContext: function restoreContext() {}, | ||
loseContext: function loseContext() {} | ||
}, | ||
@@ -358,5 +359,6 @@ 'OES_standard_derivatives': {}, | ||
function () { | ||
function WebGLRenderingContext(canvas) { | ||
function WebGLRenderingContext(canvas, contextAttributes) { | ||
_classCallCheck(this, WebGLRenderingContext); | ||
this._contextAttributes = contextAttributes; | ||
testFuncs.forEach(function (key) { | ||
@@ -383,2 +385,7 @@ WebGLRenderingContext.prototype[key] = function () { | ||
} | ||
}, { | ||
key: "getContextAttributes", | ||
value: function getContextAttributes() { | ||
return this._contextAttributes; | ||
} | ||
}]); | ||
@@ -385,0 +392,0 @@ |
@@ -22,3 +22,3 @@ "use strict"; | ||
var ver = "0.2.1"; | ||
var ver = "0.2.2"; | ||
exports.ver = ver; |
@@ -14,3 +14,2 @@ "use strict"; | ||
// import HTMLCanvasElement from "../classes/HTMLCanvasElement"; | ||
function mockPrototype() { | ||
@@ -29,3 +28,3 @@ /** | ||
var getContext2D = jest.fn(function getContext2d(type) { | ||
function getContext(type, options) { | ||
if (type === '2d') { | ||
@@ -37,3 +36,3 @@ /** | ||
if (generatedContexts.has(this)) return generatedContexts.get(this); | ||
var ctx = new _CanvasRenderingContext2D["default"](this); | ||
var ctx = new _CanvasRenderingContext2D["default"](this, options); | ||
generatedContexts.set(this, ctx); | ||
@@ -44,3 +43,3 @@ return ctx; | ||
var _ctx = new _WebGLRenderingContext["default"](this); | ||
var _ctx = new _WebGLRenderingContext["default"](this, options); | ||
@@ -51,18 +50,7 @@ generatedContexts.set(this, _ctx); | ||
try { | ||
if (!this.dataset.internalRequireTest) require('canvas'); | ||
} catch (_unused) { | ||
return null; | ||
} | ||
return getContext2D.internal.call(this, type); | ||
}); | ||
if (!jest.isMockFunction(HTMLCanvasElement.prototype.getContext)) { | ||
getContext2D.internal = HTMLCanvasElement.prototype.getContext; | ||
} else { | ||
getContext2D.internal = HTMLCanvasElement.prototype.getContext.internal; | ||
return getContext.internal.call(this, type, options); | ||
} | ||
HTMLCanvasElement.prototype.getContext = getContext2D; | ||
getContext.internal = HTMLCanvasElement.prototype.getContext; | ||
HTMLCanvasElement.prototype.getContext = getContext; | ||
/** | ||
@@ -69,0 +57,0 @@ * This function technically throws SecurityError at runtime, but it cannot be mocked, because |
@@ -28,4 +28,2 @@ "use strict"; | ||
var _HTMLCanvasElement = _interopRequireDefault(require("./classes/HTMLCanvasElement")); | ||
var _Image = _interopRequireDefault(require("./classes/Image")); | ||
@@ -80,3 +78,2 @@ | ||
if (!win.createImageBitmap) win.createImageBitmap = _createImageBitmap["default"]; | ||
if (!win.HTMLCanvasElement) win.HTMLCanvasElement = _HTMLCanvasElement["default"]; | ||
if (!win.Image) win.Image = _Image["default"]; | ||
@@ -83,0 +80,0 @@ if (!win.HTMLImageElement) win.HTMLImageElement = _Image["default"]; |
{ | ||
"name": "jest-webgl-canvas-mock", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"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
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
135541
2514