jest-canvas-mock 1.0.3 → 1.1.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.
@@ -4,6 +4,7 @@
4
4
 
5
5
  import createCanvas from '../src/canvas';
6
6
  import createContext2d from '../src/context2d';
7
+ import mockWindow from '../src/window';
7
8
 
8
9
  describe('canvas', () => {
9
10
  beforeEach(() => {
@@ -110,4 +111,37 @@ describe('canvas', () => {
110
111
  expect(canvas1).not.toBe(canvas2);
111
112
  expect(ctx1).not.toBe(ctx2);
112
113
  });
114
+
115
+ test('Path2D', () => {
116
+ const path = new Path2D();
117
+
118
+ expect(path).toBeDefined();
119
+
120
+ Object.keys(path.constructor.prototype).forEach(key => {
121
+ if(typeof path[key] === "function"){
122
+ path[key]();
123
+ }
124
+ });
125
+
126
+ [
127
+ 'addPath',
128
+ 'closePath',
129
+ 'moveTo',
130
+ 'lineTo',
131
+ 'bezierCurveTo',
132
+ 'quadraticCurveTo',
133
+ 'arc',
134
+ 'arcTo',
135
+ 'ellipse',
136
+ 'rect',
137
+ ].forEach((key) => {
138
+ expect(path[key]).toBeCalled();
139
+ });
140
+ });
141
+
142
+ test('Path2D not override', () => {
143
+ const saved = window.Path2D;
144
+ mockWindow(window);
145
+ expect(saved === window.Path2D).toBe(true);
146
+ });
113
147
  });
package/lib/window.js CHANGED
@@ -10,6 +10,11 @@ var _canvas2 = _interopRequireDefault(_canvas);
10
10
 
11
11
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
12
 
13
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /**
14
+ * Created by hustcc 17/12/25.
15
+ * Contract: i@hust.cc
16
+ */
17
+
13
18
  exports.default = function (win) {
14
19
  var d = win.document;
15
20
  var f = win.document.createElement;
@@ -38,8 +43,15 @@ exports.default = function (win) {
38
43
  win.document.createElement = function (param) {
39
44
  return param.toString().toLowerCase() === 'canvas' ? (0, _canvas2.default)('canvas') : f.call(d, param);
40
45
  };
46
+
47
+ if (!win.Path2D) {
48
+ win.Path2D = function Path2D() {
49
+ _classCallCheck(this, Path2D);
50
+ };
51
+ ['addPath', 'closePath', 'moveTo', 'lineTo', 'bezierCurveTo', 'quadraticCurveTo', 'arc', 'arcTo', 'ellipse', 'rect'].forEach(function (key) {
52
+ win.Path2D.prototype[key] = jest.fn();
53
+ });
54
+ }
55
+
41
56
  return win;
42
- }; /**
43
- * Created by hustcc 17/12/25.
44
- * Contract: i@hust.cc
45
- */
57
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jest-canvas-mock",
3
- "version": "1.0.3",
3
+ "version": "1.1.0",
4
4
  "description": "A module to mock canvas in Jest.",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
package/src/window.js CHANGED
@@ -33,5 +33,24 @@ export default win => {
33
33
  win.document.createElement = (param) => param.toString().toLowerCase() === 'canvas'
34
34
  ? createCanvas('canvas')
35
35
  : f.call(d, param);
36
+
37
+ if (!win.Path2D) {
38
+ win.Path2D = class Path2D {};
39
+ [
40
+ 'addPath',
41
+ 'closePath',
42
+ 'moveTo',
43
+ 'lineTo',
44
+ 'bezierCurveTo',
45
+ 'quadraticCurveTo',
46
+ 'arc',
47
+ 'arcTo',
48
+ 'ellipse',
49
+ 'rect',
50
+ ].forEach((key) => {
51
+ win.Path2D.prototype[key] = jest.fn();
52
+ })
53
+ }
54
+
36
55
  return win;
37
56
  };