Skip to content

Commit

Permalink
fix(jest-canvas-mock): account for possible TypeError (#116)
Browse files Browse the repository at this point in the history
* fix: account for incomplete window object

* test: add unit tests

* chore: v2.5.2
see: #116 (comment)
  • Loading branch information
virtuoushub authored Jun 28, 2023
1 parent 3da0cba commit dc35dbc
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 14 deletions.
32 changes: 32 additions & 0 deletions __tests__/mock/prototype.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { JSDOM } from 'jsdom';
import mockPrototype from '../../src/mock/prototype';

let canvas;

beforeEach(() => {
Expand Down Expand Up @@ -117,4 +120,33 @@ describe('mock', () => {
const second = canvas.getContext('2d');
expect(first).toBe(second);
});

it('should work in both cases where passed in window object has and does not have .HTMLCanvasElement.prototype', () => {
// arrange/act
const mockWin = new JSDOM().window;
mockPrototype();
// assert (should not have effected the mockWindow since we did not pass it in )
expect(
jest.isMockFunction(mockWin.HTMLCanvasElement.prototype.getContext)
).toBe(false);
expect(
jest.isMockFunction(mockWin.HTMLCanvasElement.prototype.toBlob)
).toBe(false);
expect(
jest.isMockFunction(mockWin.HTMLCanvasElement.prototype.toDataURL)
).toBe(false);

// act
mockPrototype(mockWin);
// assert ( should mock out expected fields in passed in window object )
expect(
jest.isMockFunction(mockWin.HTMLCanvasElement.prototype.getContext)
).toBe(true);
expect(
jest.isMockFunction(mockWin.HTMLCanvasElement.prototype.toBlob)
).toBe(true);
expect(
jest.isMockFunction(mockWin.HTMLCanvasElement.prototype.toDataURL)
).toBe(true);
});
});
17 changes: 17 additions & 0 deletions __tests__/mock/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,21 @@ describe('mockWindow', () => {
true
);
});

it('mocks without a fully formed passed in window object', () => {
const win = mockWindow({ document: {} });

expect(win.Path2D).not.toBeNull();
expect(win.CanvasGradient).not.toBeNull();
expect(win.CanvasPattern).not.toBeNull();
expect(win.CanvasRenderingContext2D).not.toBeNull();
expect(win.DOMMatrix).not.toBeNull();
expect(win.ImageData).not.toBeNull();
expect(win.TextMetrics).not.toBeNull();
expect(win.ImageBitmap).not.toBeNull();
expect(win.createImageBitmap).not.toBeNull();

expect(jest.isMockFunction(win.HTMLCanvasElement)).toBe(false);
expect(win.HTMLCanvasElement).toBeUndefined();
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jest-canvas-mock",
"version": "2.5.1",
"version": "2.5.2",
"description": "Mock a canvas in your jest tests.",
"main": "lib/index.js",
"types": "types/index.d.ts",
Expand Down
30 changes: 17 additions & 13 deletions src/mock/prototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@ export default function mockPrototype(win) {
return getContext2D.internal.call(this, type);
});

if (!jest.isMockFunction(win.HTMLCanvasElement.prototype.getContext)) {
getContext2D.internal = win.HTMLCanvasElement.prototype.getContext;
let htmlCanvasElementPrototype = HTMLCanvasElement.prototype;
if (win?.HTMLCanvasElement?.prototype) {
htmlCanvasElementPrototype = win?.HTMLCanvasElement?.prototype;
}

if (!jest.isMockFunction(htmlCanvasElementPrototype.getContext)) {
getContext2D.internal = htmlCanvasElementPrototype.getContext;
} else {
getContext2D.internal = win.HTMLCanvasElement.prototype.getContext.internal;
getContext2D.internal = htmlCanvasElementPrototype.getContext.internal;
}
win.HTMLCanvasElement.prototype.getContext = getContext2D;
htmlCanvasElementPrototype.getContext = getContext2D;

/**
* This function technically throws SecurityError at runtime, but it cannot be mocked, because
Expand Down Expand Up @@ -73,12 +78,12 @@ export default function mockPrototype(win) {
setTimeout(() => callback(blob), 0);
});

if (!jest.isMockFunction(win.HTMLCanvasElement.prototype.toBlob)) {
toBlobOverride.internal = win.HTMLCanvasElement.prototype.toBlob;
if (!jest.isMockFunction(htmlCanvasElementPrototype.toBlob)) {
toBlobOverride.internal = htmlCanvasElementPrototype.toBlob;
} else {
toBlobOverride.internal = win.HTMLCanvasElement.prototype.toBlob.internal;
toBlobOverride.internal = htmlCanvasElementPrototype.toBlob.internal;
}
win.HTMLCanvasElement.prototype.toBlob = toBlobOverride;
htmlCanvasElementPrototype.toBlob = toBlobOverride;

/**
* This section creates a dataurl with a validated mime type. This is not actually valid, because
Expand All @@ -103,11 +108,10 @@ export default function mockPrototype(win) {
return 'data:' + type + ';base64,00';
});

if (!jest.isMockFunction(win.HTMLCanvasElement.prototype.toDataURL)) {
toDataURLOverride.internal = win.HTMLCanvasElement.prototype.toDataURL;
if (!jest.isMockFunction(htmlCanvasElementPrototype.toDataURL)) {
toDataURLOverride.internal = htmlCanvasElementPrototype.toDataURL;
} else {
toDataURLOverride.internal =
win.HTMLCanvasElement.prototype.toDataURL.internal;
toDataURLOverride.internal = htmlCanvasElementPrototype.toDataURL.internal;
}
win.HTMLCanvasElement.prototype.toDataURL = toDataURLOverride;
htmlCanvasElementPrototype.toDataURL = toDataURLOverride;
}

0 comments on commit dc35dbc

Please sign in to comment.