Skip to content

Commit

Permalink
test: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
virtuoushub committed May 29, 2023
1 parent 2c07e0e commit d1a8d2e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 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();
});
});

0 comments on commit d1a8d2e

Please sign in to comment.