jest-canvas-mock 2.1.2 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,29 +3,29 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports["default"] = mockPrototype;
6
+ exports.default = mockPrototype;
7
7
 
8
8
  function mockPrototype() {
9
- /**
10
- * This weakmap is designed to contain all of the generated canvas contexts. It's keys are the
11
- * jsdom canvases obtained by using the `this` keyword inside the `#getContext('2d')` function
12
- * call. It's values are the generated `CanvasRenderingContext2D` objects.
9
+ /**
10
+ * This weakmap is designed to contain all of the generated canvas contexts. It's keys are the
11
+ * jsdom canvases obtained by using the `this` keyword inside the `#getContext('2d')` function
12
+ * call. It's values are the generated `CanvasRenderingContext2D` objects.
13
13
  */
14
- var generatedContexts = new WeakMap();
15
- /**
16
- * Overrides getContext. Every test run will create a new function that overrides the current
17
- * value of getContext. It attempts to preserve the original getContext function by storing it on
18
- * the callback as a property.
14
+ const generatedContexts = new WeakMap();
15
+ /**
16
+ * Overrides getContext. Every test run will create a new function that overrides the current
17
+ * value of getContext. It attempts to preserve the original getContext function by storing it on
18
+ * the callback as a property.
19
19
  */
20
20
 
21
- var getContext2D = jest.fn(function getContext2d(type) {
21
+ const getContext2D = jest.fn(function getContext2d(type) {
22
22
  if (type === '2d') {
23
- /**
24
- * Contexts must be indempotent. Once they are generated, they should be returned when
25
- * getContext() is called on the same canvas object multiple times.
23
+ /**
24
+ * Contexts must be indempotent. Once they are generated, they should be returned when
25
+ * getContext() is called on the same canvas object multiple times.
26
26
  */
27
27
  if (generatedContexts.has(this)) return generatedContexts.get(this);
28
- var ctx = new CanvasRenderingContext2D(this);
28
+ const ctx = new CanvasRenderingContext2D(this);
29
29
  generatedContexts.set(this, ctx);
30
30
  return ctx;
31
31
  }
@@ -46,17 +46,17 @@ function mockPrototype() {
46
46
  }
47
47
 
48
48
  HTMLCanvasElement.prototype.getContext = getContext2D;
49
- /**
50
- * This function technically throws SecurityError at runtime, but it cannot be mocked, because
51
- * we don't know if the canvas is tainted. These kinds of errors will be silent.
49
+ /**
50
+ * This function technically throws SecurityError at runtime, but it cannot be mocked, because
51
+ * we don't know if the canvas is tainted. These kinds of errors will be silent.
52
52
  */
53
53
 
54
- var toBlobOverride = jest.fn(function toBlobOverride(callback, mimetype) {
54
+ const toBlobOverride = jest.fn(function toBlobOverride(callback, mimetype) {
55
55
  if (arguments.length < 1) throw new TypeError('Failed to execute \'toBlob\' on \'HTMLCanvasElement\': 1 argument required, but only 0 present.');
56
56
  if (typeof callback !== 'function') throw new TypeError('Failed to execute \'toBlob\' on \'HTMLCanvasElement\': The callback provided as parameter 1 is not a function.');
57
- /**
58
- * Mime type must be image/jpeg or image/webp exactly for the browser to accept it, otherwise
59
- * it's image/png.
57
+ /**
58
+ * Mime type must be image/jpeg or image/webp exactly for the browser to accept it, otherwise
59
+ * it's image/png.
60
60
  */
61
61
 
62
62
  switch (mimetype) {
@@ -69,20 +69,18 @@ function mockPrototype() {
69
69
  default:
70
70
  mimetype = 'image/png';
71
71
  }
72
- /**
73
- * This section creates a blob of size width * height * 4. This is not actually valid, because
74
- * jpeg size is variable, and so is png. TODO: Is there a better way to do this?
72
+ /**
73
+ * This section creates a blob of size width * height * 4. This is not actually valid, because
74
+ * jpeg size is variable, and so is png. TODO: Is there a better way to do this?
75
75
  */
76
76
 
77
77
 
78
- var length = this.width * this.height * 4;
79
- var data = new Uint8Array(length);
80
- var blob = new window.Blob([data], {
78
+ const length = this.width * this.height * 4;
79
+ const data = new Uint8Array(length);
80
+ const blob = new window.Blob([data], {
81
81
  type: mimetype
82
82
  });
83
- setTimeout(function () {
84
- return callback(blob);
85
- }, 0);
83
+ setTimeout(() => callback(blob), 0);
86
84
  });
87
85
 
88
86
  if (!jest.isMockFunction(HTMLCanvasElement.prototype.toBlob)) {
@@ -92,12 +90,12 @@ function mockPrototype() {
92
90
  }
93
91
 
94
92
  HTMLCanvasElement.prototype.toBlob = toBlobOverride;
95
- /**
96
- * This section creates a dataurl with a validated mime type. This is not actually valid, because
97
- * jpeg size is variable, and so is png. TODO: Is there a better way to do this?
93
+ /**
94
+ * This section creates a dataurl with a validated mime type. This is not actually valid, because
95
+ * jpeg size is variable, and so is png. TODO: Is there a better way to do this?
98
96
  */
99
97
 
100
- var toDataURLOverride = jest.fn(function toDataURLOverride(type, encoderOptions) {
98
+ const toDataURLOverride = jest.fn(function toDataURLOverride(type, encoderOptions) {
101
99
  switch (type) {
102
100
  case 'image/jpeg':
103
101
  break;
@@ -108,8 +106,8 @@ function mockPrototype() {
108
106
  default:
109
107
  type = 'image/png';
110
108
  }
111
- /**
112
- * This is the smallest valid data url I could generate.
109
+ /**
110
+ * This is the smallest valid data url I could generate.
113
111
  */
114
112
 
115
113
 
package/lib/window.js CHANGED
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports["default"] = void 0;
6
+ exports.default = void 0;
7
7
 
8
8
  var _Path2D = _interopRequireDefault(require("./classes/Path2D"));
9
9
 
@@ -25,15 +25,15 @@ var _prototype = _interopRequireDefault(require("./mock/prototype"));
25
25
 
26
26
  var _createImageBitmap = _interopRequireDefault(require("./mock/createImageBitmap"));
27
27
 
28
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
28
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
29
 
30
- /**
31
- * Created by hustcc 17/12/25.
32
- * Contract: i@hust.cc
30
+ /**
31
+ * Created by hustcc 17/12/25.
32
+ * Contract: i@hust.cc
33
33
  */
34
- var _default = function _default(win) {
35
- var d = win.document;
36
- var f = win.document.createElement; // jsdom@11.6.2 || jest@^22.0.0, console.error in Function getContext();
34
+ var _default = win => {
35
+ const d = win.document;
36
+ const f = win.document.createElement; // jsdom@11.6.2 || jest@^22.0.0, console.error in Function getContext();
37
37
  // https://github.com/jsdom/jsdom/blob/4c7698f760fc64f20b2a0ddff450eddbdd193176/lib/jsdom/living/nodes/HTMLCanvasElement-impl.js#L55-L58
38
38
  // cosole.error will make ci error.
39
39
  // try {
@@ -54,24 +54,24 @@ var _default = function _default(win) {
54
54
  // if ctx not exist, mock it.
55
55
  // just mock canvas creator.
56
56
 
57
- /*
58
- win.document.createElement = param => param.toString().toLowerCase() === 'canvas'
59
- ? createCanvas('canvas')
60
- : f.call(d, param);
57
+ /*
58
+ win.document.createElement = param => param.toString().toLowerCase() === 'canvas'
59
+ ? createCanvas('canvas')
60
+ : f.call(d, param);
61
61
  */
62
62
  // if not exist, then mock it.
63
63
 
64
- if (!win.Path2D) win.Path2D = _Path2D["default"];
65
- if (!win.CanvasGradient) win.CanvasGradient = _CanvasGradient["default"];
66
- if (!win.CanvasPattern) win.CanvasPattern = _CanvasPattern["default"];
67
- if (!win.CanvasRenderingContext2D) win.CanvasRenderingContext2D = _CanvasRenderingContext2D["default"];
68
- if (!win.DOMMatrix) win.DOMMatrix = _DOMMatrix["default"];
69
- if (!win.ImageData) win.ImageData = _ImageData["default"];
70
- if (!win.TextMetrics) win.TextMetrics = _TextMetrics["default"];
71
- if (!win.ImageBitmap) win.ImageBitmap = _ImageBitmap["default"];
72
- if (!win.createImageBitmap) win.createImageBitmap = _createImageBitmap["default"];
73
- (0, _prototype["default"])();
64
+ if (!win.Path2D) win.Path2D = _Path2D.default;
65
+ if (!win.CanvasGradient) win.CanvasGradient = _CanvasGradient.default;
66
+ if (!win.CanvasPattern) win.CanvasPattern = _CanvasPattern.default;
67
+ if (!win.CanvasRenderingContext2D) win.CanvasRenderingContext2D = _CanvasRenderingContext2D.default;
68
+ if (!win.DOMMatrix) win.DOMMatrix = _DOMMatrix.default;
69
+ if (!win.ImageData) win.ImageData = _ImageData.default;
70
+ if (!win.TextMetrics) win.TextMetrics = _TextMetrics.default;
71
+ if (!win.ImageBitmap) win.ImageBitmap = _ImageBitmap.default;
72
+ if (!win.createImageBitmap) win.createImageBitmap = _createImageBitmap.default;
73
+ (0, _prototype.default)();
74
74
  return win;
75
75
  };
76
76
 
77
- exports["default"] = _default;
77
+ exports.default = _default;
package/package.json CHANGED
@@ -1,70 +1,70 @@
1
- {
2
- "name": "jest-canvas-mock",
3
- "version": "2.1.2",
4
- "description": "Mock a canvas in your jest tests.",
5
- "main": "lib/index.js",
6
- "types": "types/index.d.ts",
7
- "scripts": {
8
- "test": "jest --no-cache",
9
- "build": "babel src --out-dir lib",
10
- "coveralls": "cat ./coverage/lcov.info | coveralls",
11
- "prepare": "npm run build"
12
- },
13
- "dependencies": {
14
- "cssfontparser": "^1.2.1",
15
- "parse-color": "^1.0.0"
16
- },
17
- "devDependencies": {
18
- "@babel/cli": "^7.4.4",
19
- "@babel/core": "^7.4.4",
20
- "@babel/plugin-proposal-class-properties": "^7.4.4",
21
- "@babel/preset-env": "^7.4.4",
22
- "@commitlint/cli": "^7.5.2",
23
- "@commitlint/config-angular": "^7.5.0",
24
- "babel-jest": "^24.8.0",
25
- "babel-plugin-version": "^0.2.3",
26
- "coveralls": "^3.0.3",
27
- "husky": "^2.2.0",
28
- "jest": "^24.8.0"
29
- },
30
- "commitlint": {
31
- "extends": [
32
- "@commitlint/config-angular"
33
- ]
34
- },
35
- "husky": {
36
- "hooks": {
37
- "commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
38
- "pre-commit": "npm run test && npm run build"
39
- }
40
- },
41
- "jest": {
42
- "collectCoverage": true,
43
- "collectCoverageFrom": [
44
- "src/classes/**/*.js",
45
- "src/mock/**/*.js"
46
- ],
47
- "setupFiles": [
48
- "./src/index.js"
49
- ]
50
- },
51
- "repository": {
52
- "type": "git",
53
- "url": "git+https://github.com/hustcc/jest-canvas-mock.git"
54
- },
55
- "keywords": [
56
- "mock",
57
- "jest",
58
- "jest-mock",
59
- "echarts",
60
- "canvas",
61
- "test",
62
- "unit"
63
- ],
64
- "author": "hustcc",
65
- "license": "MIT",
66
- "bugs": {
67
- "url": "https://github.com/hustcc/jest-canvas-mock/issues"
68
- },
69
- "homepage": "https://github.com/hustcc/jest-canvas-mock#readme"
70
- }
1
+ {
2
+ "name": "jest-canvas-mock",
3
+ "version": "2.2.0",
4
+ "description": "Mock a canvas in your jest tests.",
5
+ "main": "lib/index.js",
6
+ "types": "types/index.d.ts",
7
+ "scripts": {
8
+ "test": "jest --no-cache",
9
+ "build": "babel src --out-dir lib",
10
+ "coveralls": "cat ./coverage/lcov.info | coveralls",
11
+ "prepare": "npm run build"
12
+ },
13
+ "dependencies": {
14
+ "cssfontparser": "^1.2.1",
15
+ "parse-color": "^1.0.0"
16
+ },
17
+ "devDependencies": {
18
+ "@babel/cli": "^7.6.4",
19
+ "@babel/core": "^7.6.4",
20
+ "@babel/plugin-proposal-class-properties": "^7.5.5",
21
+ "@babel/preset-env": "^7.6.3",
22
+ "@commitlint/cli": "^8.2.0",
23
+ "@commitlint/config-angular": "^8.2.0",
24
+ "babel-jest": "^24.9.0",
25
+ "babel-plugin-version": "^0.2.3",
26
+ "coveralls": "^3.0.7",
27
+ "husky": "^3.0.9",
28
+ "jest": "^24.9.0"
29
+ },
30
+ "commitlint": {
31
+ "extends": [
32
+ "@commitlint/config-angular"
33
+ ]
34
+ },
35
+ "husky": {
36
+ "hooks": {
37
+ "commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
38
+ "pre-commit": "npm run test && npm run build"
39
+ }
40
+ },
41
+ "jest": {
42
+ "collectCoverage": true,
43
+ "collectCoverageFrom": [
44
+ "src/classes/**/*.js",
45
+ "src/mock/**/*.js"
46
+ ],
47
+ "setupFiles": [
48
+ "./src/index.js"
49
+ ]
50
+ },
51
+ "repository": {
52
+ "type": "git",
53
+ "url": "git+https://github.com/hustcc/jest-canvas-mock.git"
54
+ },
55
+ "keywords": [
56
+ "mock",
57
+ "jest",
58
+ "jest-mock",
59
+ "echarts",
60
+ "canvas",
61
+ "test",
62
+ "unit"
63
+ ],
64
+ "author": "hustcc",
65
+ "license": "MIT",
66
+ "bugs": {
67
+ "url": "https://github.com/hustcc/jest-canvas-mock/issues"
68
+ },
69
+ "homepage": "https://github.com/hustcc/jest-canvas-mock#readme"
70
+ }
package/types/index.d.ts CHANGED
@@ -1,54 +1,86 @@
1
- export interface CanvasRenderingContext2DEvent {
2
- /**
3
- * This is the type of canvas event that occured.
4
- */
5
- type: string;
6
- /**
7
- * This is a six element array that contains the current state of the canvas `currentTransform`
8
- * value.
9
- */
10
- transform: [number, number, number, number, number, number];
11
- /**
12
- * These are the relevant properties related to this canvas event.
13
- */
14
- props: {
15
- [key: string]: any;
16
- };
17
- }
18
-
19
- declare global {
20
- interface CanvasRenderingContext2D {
21
- /**
22
- * Get all the events associated with this CanvasRenderingContext2D object.
23
- *
24
- * This method cannot be used in a production environment, only with `jest` using
25
- * `jest-canvas-mock` and should only be used for testing.
26
- *
27
- * @example
28
- * expect(ctx.__getEvents()).toMatchSnapshot();
29
- */
30
- __getEvents(): CanvasRenderingContext2DEvent[];
31
-
32
- /**
33
- * Get all the successful draw calls associated with this CanvasRenderingContext2D object.
34
- *
35
- * This method cannot be used in a production environment, only with `jest` using
36
- * `jest-canvas-mock` and should only be used for testing.
37
- *
38
- * @example
39
- * expect(ctx.__getDrawCalls()).toMatchSnapshot();
40
- */
41
- __getDrawCalls(): CanvasRenderingContext2DEvent[];
42
-
43
- /**
44
- * Get the current path associated with this CanvasRenderingContext2D object.
45
- *
46
- * This method cannot be used in a production environment, only with `jest` using
47
- * `jest-canvas-mock` and should only be used for testing.
48
- *
49
- * @example
50
- * expect(ctx.__getPath()).toMatchSnapshot();
51
- */
52
- __getPath(): CanvasRenderingContext2DEvent[];
53
- }
54
- }
1
+ export interface CanvasRenderingContext2DEvent {
2
+ /**
3
+ * This is the type of canvas event that occured.
4
+ */
5
+ type: string;
6
+ /**
7
+ * This is a six element array that contains the current state of the canvas `currentTransform`
8
+ * value.
9
+ */
10
+ transform: [number, number, number, number, number, number];
11
+ /**
12
+ * These are the relevant properties related to this canvas event.
13
+ */
14
+ props: {
15
+ [key: string]: any;
16
+ };
17
+ }
18
+
19
+ declare global {
20
+ interface CanvasRenderingContext2D {
21
+ /**
22
+ * Get all the events associated with this CanvasRenderingContext2D object.
23
+ *
24
+ * This method cannot be used in a production environment, only with `jest` using
25
+ * `jest-canvas-mock` and should only be used for testing.
26
+ *
27
+ * @example
28
+ * expect(ctx.__getEvents()).toMatchSnapshot();
29
+ */
30
+ __getEvents(): CanvasRenderingContext2DEvent[];
31
+
32
+ /**
33
+ * Clear all the events associated with this CanvasRenderingContext2D object.
34
+ *
35
+ * This method cannot be used in a production environment, only with `jest` using
36
+ * `jest-canvas-mock` and should only be used for testing.
37
+ *
38
+ * @example
39
+ * ctx.__clearEvents());
40
+ * expect(ctx.__getEvents()).toBe([]);
41
+ */
42
+ __clearEvents(): void;
43
+
44
+ /**
45
+ * Get all the successful draw calls associated with this CanvasRenderingContext2D object.
46
+ *
47
+ * This method cannot be used in a production environment, only with `jest` using
48
+ * `jest-canvas-mock` and should only be used for testing.
49
+ *
50
+ * @example
51
+ * expect(ctx.__getDrawCalls()).toMatchSnapshot();
52
+ */
53
+ __getDrawCalls(): CanvasRenderingContext2DEvent[];
54
+
55
+ /**
56
+ * Clear all the successful draw calls associated with this CanvasRenderingContext2D object.
57
+ *
58
+ * This method cannot be used in a production environment, only with `jest` using
59
+ * `jest-canvas-mock` and should only be used for testing.
60
+ *
61
+ * @example
62
+ * ctx.__clearDrawCalls());
63
+ * expect(ctx.__getDrawCalls()).toBe([]);
64
+ */
65
+ __clearDrawCalls(): void;
66
+
67
+ /**
68
+ * Get the current path associated with this CanvasRenderingContext2D object.
69
+ *
70
+ * This method cannot be used in a production environment, only with `jest` using
71
+ * `jest-canvas-mock` and should only be used for testing.
72
+ *
73
+ * @example
74
+ * expect(ctx.__getPath()).toMatchSnapshot();
75
+ */
76
+ __getPath(): CanvasRenderingContext2DEvent[];
77
+
78
+ /**
79
+ * Clears the current path associated with this CanvasRenderingContext2D object.
80
+ *
81
+ * This method cannot be used in a production environment, only with `jest` using
82
+ * `jest-canvas-mock` and should be only used for testing.
83
+ */
84
+ __clearPath(): void;
85
+ }
86
+ }