Skip to content

Commit

Permalink
Pass window object to mockPrototype (#100)
Browse files Browse the repository at this point in the history
When mocking the canvas on a non-global window object, the prototype
mocks should apply to the same window object.
  • Loading branch information
jdufresne committed May 18, 2023
1 parent 94162b7 commit ca41794
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
30 changes: 30 additions & 0 deletions __tests__/mock/window.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { JSDOM } from 'jsdom';
import mockWindow from '../../src/window';

describe('mockWindow', () => {
it('mocks the passed object', () => {
const win = new JSDOM().window;

mockWindow(win);

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.prototype.getContext)
).toBe(true);
expect(jest.isMockFunction(win.HTMLCanvasElement.prototype.toBlob)).toBe(
true
);
expect(jest.isMockFunction(win.HTMLCanvasElement.prototype.toDataURL)).toBe(
true
);
});
});
27 changes: 14 additions & 13 deletions src/mock/prototype.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function mockPrototype() {
export default function mockPrototype(win) {
/**
* This weakmap is designed to contain all of the generated canvas contexts. It's keys are the
* jsdom canvases obtained by using the `this` keyword inside the `#getContext('2d')` function
Expand Down Expand Up @@ -29,12 +29,12 @@ export default function mockPrototype() {
return getContext2D.internal.call(this, type);
});

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

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

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

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

if (!jest.isMockFunction(HTMLCanvasElement.prototype.toDataURL)) {
toDataURLOverride.internal = HTMLCanvasElement.prototype.toDataURL;
if (!jest.isMockFunction(win.HTMLCanvasElement.prototype.toDataURL)) {
toDataURLOverride.internal = win.HTMLCanvasElement.prototype.toDataURL;
} else {
toDataURLOverride.internal = HTMLCanvasElement.prototype.toDataURL.internal;
toDataURLOverride.internal =
win.HTMLCanvasElement.prototype.toDataURL.internal;
}
HTMLCanvasElement.prototype.toDataURL = toDataURLOverride;
win.HTMLCanvasElement.prototype.toDataURL = toDataURLOverride;
}
2 changes: 1 addition & 1 deletion src/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default (win) => {
if (!win.ImageBitmap) win.ImageBitmap = ImageBitmap;
if (!win.createImageBitmap) win.createImageBitmap = createImageBitmap;

mockPrototype();
mockPrototype(win);

return win;
};

0 comments on commit ca41794

Please sign in to comment.