Repository: hustcc/jest-canvas-mock
Pull request #98 (2022-08-03 21:07:55)

index: export a function to re-initialize mocks
This allows consumers of this library to use the mock with `jest.resetAllMocks()`

Should allow workarounds for https://github.com/hustcc/jest-canvas-mock/issues/72


Patch:
diff --git a/.gitignore b/.gitignore
index bc7a6d1..cbb0c3b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,3 +5,6 @@ coverage
 package-lock.json
 yarn.lock
 lib
+.yarn
+.pnp.cjs
+.pnp.loader.mjs
diff --git a/README.md b/README.md
index 0aaf571..1f935c5 100644
--- a/README.md
+++ b/README.md
@@ -63,6 +63,20 @@ Add that file to your `setupFiles` array:
 }
 ```
 
+## Reset
+
+If you reset the jest mocks (for example, with `jest.resetAllMocks()`), you can
+call `setupJestCanvasMock()` to re-create it.
+
+```
+import { setupJestCanvasMock } from 'jest-canvas-mock';
+
+beforeEach(() => {
+  jest.resetAllMocks();
+  setupJestCanvasMock();
+});
+```
+
 ## Mock Strategy
 
 This mock strategy implements all the canvas functions and actually verifies the parameters. If a
diff --git a/src/index.js b/src/index.js
index f6e4994..aa1ec9a 100644
--- a/src/index.js
+++ b/src/index.js
@@ -12,3 +12,7 @@ if (typeof window !== 'undefined') {
 }
 
 export const ver = '__VERSION__';
+
+export function setupJestCanvasMock(window) {
+  mockWindow(window || global.window);
+}
diff --git a/src/mock/prototype.js b/src/mock/prototype.js
index 062ea39..0a733a7 100644
--- a/src/mock/prototype.js
+++ b/src/mock/prototype.js
@@ -13,7 +13,7 @@ export default function mockPrototype() {
   const getContext2D = jest.fn(function getContext2d(type) {
     if (type === '2d') {
       /**
-       * Contexts must be indempotent. Once they are generated, they should be returned when
+       * Contexts must be idempotent. Once they are generated, they should be returned when
        * getContext() is called on the same canvas object multiple times.
        */
       if (generatedContexts.has(this)) return generatedContexts.get(this);
diff --git a/src/window.js b/src/window.js
index 7ddc242..74d8e72 100644
--- a/src/window.js
+++ b/src/window.js
@@ -20,7 +20,7 @@ export default (win) => {
 
   // jsdom@11.6.2 || jest@^22.0.0, console.error in Function getContext();
   // https://github.com/jsdom/jsdom/blob/4c7698f760fc64f20b2a0ddff450eddbdd193176/lib/jsdom/living/nodes/HTMLCanvasElement-impl.js#L55-L58
-  // cosole.error will make ci error.
+  // console.error will make ci error.
   // try {
   //   // get the context 2d.
   //   const ctx = d.createElement('canvas').getContext('2d');
diff --git a/types/index.d.ts b/types/index.d.ts
index 9882705..e719f28 100644
--- a/types/index.d.ts
+++ b/types/index.d.ts
@@ -1,6 +1,8 @@
+export function setupJestCanvasMock(window?: Window) {}
+
 export interface CanvasRenderingContext2DEvent {
   /**
-   * This is the type of canvas event that occured.
+   * This is the type of canvas event that occurred.
    */
   type: string;
   /**


Test patch:
diff --git a/__tests__/index.js b/__tests__/index.js
index dd9354f..300c85d 100644
--- a/__tests__/index.js
+++ b/__tests__/index.js
@@ -2,7 +2,7 @@
  * test canvas
  */
 
-import { ver } from '../src';
+import { ver, setupJestCanvasMock } from '../src';
 import pkg from '../package.json';
 
 let canvas;
@@ -16,3 +16,14 @@ describe('canvas', () => {
     expect(ver).toBe(pkg.version);
   });
 });
+
+describe('setupJestCanvasMock', () => {
+  it('should setup after resetAllMocks', () => {
+    jest.resetAllMocks();
+    expect(document.createElement('canvas').getContext('2d')).toBe(undefined);
+    setupJestCanvasMock();
+    expect(document.createElement('canvas').getContext('2d')).toHaveProperty(
+      'createImageData'
+    );
+  });
+});
diff --git a/__tests__/mock/prototype.js b/__tests__/mock/prototype.js
index 2e8f22b..aa8f0c8 100644
--- a/__tests__/mock/prototype.js
+++ b/__tests__/mock/prototype.js
@@ -101,7 +101,7 @@ describe('mock', () => {
 
   /**
    * This test is very special, because it helps increase the code coverage to 100%. It patches
-   * console.error to supress calls to console.error, sets an internal dataset value to force the
+   * console.error to suppress calls to console.error, sets an internal dataset value to force the
    * getContext() function to call it's internal getContext() provided by jsdom.
    */
   it('should call internal function if "canvas" is installed', () => {