jest-canvas-mock 2.0.0 → 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.
package/package.json CHANGED
@@ -1,67 +1,70 @@
1
- {
2
- "name": "jest-canvas-mock",
3
- "version": "2.0.0",
4
- "description": "A module to mock canvas in Jest.",
5
- "main": "lib/index.js",
6
- "scripts": {
7
- "test": "jest --no-cache",
8
- "build": "babel src --out-dir lib",
9
- "coveralls": "cat ./coverage/lcov.info | coveralls"
10
- },
11
- "dependencies": {
12
- "cssfontparser": "^1.2.1",
13
- "parse-color": "^1.0.0"
14
- },
15
- "devDependencies": {
16
- "@babel/cli": "^7.2.3",
17
- "@babel/plugin-proposal-class-properties": "^7.3.0",
18
- "@babel/preset-env": "^7.3.1",
19
- "@commitlint/cli": "^7.5.2",
20
- "@commitlint/config-angular": "^7.5.0",
21
- "babel-jest": "^24.0.0",
22
- "babel-plugin-version": "^0.2.3",
23
- "coveralls": "^3.0.2",
24
- "husky": "^1.3.1",
25
- "jest": "^24.0.0"
26
- },
27
- "commitlint": {
28
- "extends": [
29
- "@commitlint/config-angular"
30
- ]
31
- },
32
- "husky": {
33
- "hooks": {
34
- "commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
35
- "pre-commit": "npm run test && npm run build"
36
- }
37
- },
38
- "jest": {
39
- "collectCoverage": true,
40
- "collectCoverageFrom": [
41
- "src/classes/**/*.js",
42
- "src/mock/**/*.js"
43
- ],
44
- "setupFiles": [
45
- "./src/index.js"
46
- ]
47
- },
48
- "repository": {
49
- "type": "git",
50
- "url": "git+https://github.com/hustcc/jest-canvas-mock.git"
51
- },
52
- "keywords": [
53
- "mock",
54
- "jest",
55
- "jest-mock",
56
- "echarts",
57
- "canvas",
58
- "test",
59
- "unit"
60
- ],
61
- "author": "hustcc",
62
- "license": "ISC",
63
- "bugs": {
64
- "url": "https://github.com/hustcc/jest-canvas-mock/issues"
65
- },
66
- "homepage": "https://github.com/hustcc/jest-canvas-mock#readme"
67
- }
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
+ }
@@ -0,0 +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
+ * 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
+ }
package/lib/canvas.js DELETED
@@ -1,28 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
-
7
- var _context2d = require('./classes/context2d');
8
-
9
- var _context2d2 = _interopRequireDefault(_context2d);
10
-
11
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
-
13
- var createCanvas = function createCanvas() {
14
- var div = document.createElement('div'); // use div to mock it's api
15
-
16
- div.getContext = function (param) {
17
- return param === '2d' ? (0, _context2d2.default)('2d', div) : {};
18
- };
19
- div.toDataURL = jest.fn(function () {
20
- return '';
21
- });
22
- return div;
23
- }; /**
24
- * Created by hustcc 17/12/25.
25
- * Contract: i@hust.cc
26
- */
27
-
28
- exports.default = jest.fn(createCanvas);
@@ -1,107 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
-
7
- var _CanvasGradient = require('./CanvasGradient');
8
-
9
- var _CanvasGradient2 = _interopRequireDefault(_CanvasGradient);
10
-
11
- var _CanvasPattern = require('./CanvasPattern');
12
-
13
- var _CanvasPattern2 = _interopRequireDefault(_CanvasPattern);
14
-
15
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
-
17
- /**
18
- * Created by hustcc 17/12/25.
19
- * Contract: i@hust.cc
20
- */
21
-
22
- var context2d = {
23
- fillRect: function fillRect() {},
24
- clearRect: function clearRect() {},
25
- getImageData: function getImageData() {
26
- var x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
27
- var y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
28
- var w = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
29
- var h = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
30
- return {
31
- data: new Array(w * h * 4)
32
- };
33
- },
34
- setLineDash: function setLineDash() {},
35
- getLineDash: function getLineDash() {
36
- return [];
37
- },
38
- measureText: function measureText() {
39
- var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
40
- return {
41
- width: 12 * (text.length || 0),
42
- height: 14
43
- };
44
- },
45
- putImageData: function putImageData() {},
46
- createImageData: function createImageData() {
47
- return [];
48
- },
49
- setTransform: function setTransform() {},
50
- resetTransform: function resetTransform() {},
51
- drawImage: function drawImage() {},
52
- save: function save() {},
53
- fillText: function fillText() {},
54
- restore: function restore() {},
55
- beginPath: function beginPath() {},
56
- moveTo: function moveTo() {},
57
- lineTo: function lineTo() {},
58
- closePath: function closePath() {},
59
- stroke: function stroke() {},
60
- strokeRect: function strokeRect() {},
61
- strokeText: function strokeText() {},
62
- t2: function t2() {},
63
- transform: function transform() {},
64
- translate: function translate() {},
65
- scale: function scale() {},
66
- rotate: function rotate() {},
67
- arc: function arc() {},
68
- arcTo: function arcTo() {},
69
- fill: function fill() {},
70
- rect: function rect() {},
71
- quadraticCurveTo: function quadraticCurveTo() {},
72
- createPattern: function createPattern() {
73
- return new _CanvasPattern2.default();
74
- },
75
- createLinearGradient: function createLinearGradient() {
76
- return new _CanvasGradient2.default();
77
- },
78
- createRadialGradient: function createRadialGradient() {
79
- return new _CanvasGradient2.default();
80
- },
81
- bezierCurveTo: function bezierCurveTo() {},
82
- drawFocusIfNeeded: function drawFocusIfNeeded() {},
83
- clip: function clip() {},
84
- ellipse: function ellipse() {},
85
- isPointInPath: function isPointInPath() {
86
- return true;
87
- },
88
- isPointInStroke: function isPointInStroke() {
89
- return true;
90
- }
91
- };
92
-
93
- var jestWrapper = function jestWrapper(fs) {
94
- var ctx = {}; // immutable
95
- Object.keys(fs).forEach(function (key) {
96
- ctx[key] = jest.fn(fs[key]);
97
- });
98
- return ctx;
99
- };
100
-
101
- var createContext2d = jest.fn(function (type, canvas) {
102
- var ctx = jestWrapper(context2d);
103
- ctx.canvas = canvas;
104
- return ctx;
105
- });
106
-
107
- exports.default = createContext2d;