jest-canvas-mock 1.0.0 → 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.
- package/__tests__/index.js +50 -0
- package/__tests__/server-side.js +15 -0
- package/lib/canvas.js +3 -0
- package/lib/index.js +6 -4
- package/lib/window.js +36 -12
- package/package.json +1 -1
- package/src/canvas.js +1 -0
- package/src/index.js +3 -1
- package/src/window.js +41 -8
package/__tests__/index.js
CHANGED
|
@@ -4,8 +4,13 @@
|
|
|
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', () => {
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
createCanvas.mockClear();
|
|
12
|
+
});
|
|
13
|
+
|
|
9
14
|
test('document.createElement(canvas)', () => {
|
|
10
15
|
const canvas = document.createElement('canvas');
|
|
11
16
|
expect(canvas.getContext).not.toBe(undefined);
|
|
@@ -15,6 +20,12 @@ describe('canvas', () => {
|
|
|
15
20
|
expect(div.getContext).toBe(undefined);
|
|
16
21
|
});
|
|
17
22
|
|
|
23
|
+
test('document.createElement(CANVAS)', () => {
|
|
24
|
+
const canvas = document.createElement('CANVAS');
|
|
25
|
+
expect(canvas.getContext).not.toBe(undefined);
|
|
26
|
+
expect(createCanvas).toHaveBeenLastCalledWith('canvas');
|
|
27
|
+
});
|
|
28
|
+
|
|
18
29
|
test('canvas.getContext(2d)', () => {
|
|
19
30
|
const canvas = document.createElement('canvas');
|
|
20
31
|
const ctx = canvas.getContext('2d');
|
|
@@ -24,6 +35,12 @@ describe('canvas', () => {
|
|
|
24
35
|
expect(canvas.getContext('webgl')).toEqual({});
|
|
25
36
|
});
|
|
26
37
|
|
|
38
|
+
test('canvas.toDataURL()', () => {
|
|
39
|
+
const canvas = document.createElement('canvas');
|
|
40
|
+
expect(canvas.toDataURL('image/jpeg', 1.0)).toEqual('');
|
|
41
|
+
expect(canvas.toDataURL).toHaveBeenLastCalledWith('image/jpeg', 1.0);
|
|
42
|
+
});
|
|
43
|
+
|
|
27
44
|
test('ctx.functions', () => {
|
|
28
45
|
const canvas = document.createElement('canvas');
|
|
29
46
|
const ctx = canvas.getContext('2d');
|
|
@@ -94,4 +111,37 @@ describe('canvas', () => {
|
|
|
94
111
|
expect(canvas1).not.toBe(canvas2);
|
|
95
112
|
expect(ctx1).not.toBe(ctx2);
|
|
96
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
|
+
});
|
|
97
147
|
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment node
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* test canvas mock in a node environment
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import createCanvas from '../src/canvas';
|
|
10
|
+
|
|
11
|
+
describe('canvas', () => {
|
|
12
|
+
test('running in node environment fails silently', () => {
|
|
13
|
+
expect(createCanvas).toBeDefined();
|
|
14
|
+
})
|
|
15
|
+
});
|
package/lib/canvas.js
CHANGED
|
@@ -16,6 +16,9 @@ var createCanvas = function createCanvas() {
|
|
|
16
16
|
div.getContext = function (param) {
|
|
17
17
|
return param === '2d' ? (0, _context2d2.default)('2d', div) : {};
|
|
18
18
|
};
|
|
19
|
+
div.toDataURL = jest.fn(function () {
|
|
20
|
+
return '';
|
|
21
|
+
});
|
|
19
22
|
return div;
|
|
20
23
|
}; /**
|
|
21
24
|
* Created by hustcc 17/12/25.
|
package/lib/index.js
CHANGED
|
@@ -7,7 +7,9 @@ var _window2 = _interopRequireDefault(_window);
|
|
|
7
7
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
8
8
|
|
|
9
9
|
// mock global window
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
if (typeof window !== 'undefined') {
|
|
11
|
+
global.window = (0, _window2.default)(window);
|
|
12
|
+
} /**
|
|
13
|
+
* Created by hustcc 17/12/25.
|
|
14
|
+
* Contract: i@hust.cc
|
|
15
|
+
*/
|
package/lib/window.js
CHANGED
|
@@ -10,24 +10,48 @@ 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;
|
|
16
21
|
|
|
17
|
-
//
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
// jsdom@11.6.2 || jest@^22.0.0, console.error in Function getContext();
|
|
23
|
+
// https://github.com/jsdom/jsdom/blob/4c7698f760fc64f20b2a0ddff450eddbdd193176/lib/jsdom/living/nodes/HTMLCanvasElement-impl.js#L55-L58
|
|
24
|
+
// cosole.error will make ci error.
|
|
25
|
+
// try {
|
|
26
|
+
// // get the context 2d.
|
|
27
|
+
// const ctx = d.createElement('canvas').getContext('2d');
|
|
28
|
+
//
|
|
29
|
+
// // if canvas and context2d all exist, means mock is not needed.
|
|
30
|
+
// if (ctx) {
|
|
31
|
+
// console.warn('Context 2d of canvas is exist! No need to mock');
|
|
32
|
+
// return win;
|
|
33
|
+
// }
|
|
34
|
+
// } catch (_) {
|
|
35
|
+
// // catch the throw `Error: Not implemented: HTMLCanvasElement.prototype.getContext`
|
|
36
|
+
// // https://github.com/jsdom/jsdom/blob/4c7698f760fc64f20b2a0ddff450eddbdd193176/lib/jsdom/living/nodes/HTMLCanvasElement-impl.js
|
|
37
|
+
// // when throw error, means mock is needed.
|
|
38
|
+
// // code continue
|
|
39
|
+
// }
|
|
24
40
|
// if ctx not exist, mock it.
|
|
25
41
|
// just mock canvas creator.
|
|
42
|
+
|
|
26
43
|
win.document.createElement = function (param) {
|
|
27
|
-
return param === 'canvas' ? (0, _canvas2.default)('canvas') : f.call(d, param);
|
|
44
|
+
return param.toString().toLowerCase() === 'canvas' ? (0, _canvas2.default)('canvas') : f.call(d, param);
|
|
28
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
|
+
|
|
29
56
|
return win;
|
|
30
|
-
};
|
|
31
|
-
* Created by hustcc 17/12/25.
|
|
32
|
-
* Contract: i@hust.cc
|
|
33
|
-
*/
|
|
57
|
+
};
|
package/package.json
CHANGED
package/src/canvas.js
CHANGED
package/src/index.js
CHANGED
package/src/window.js
CHANGED
|
@@ -9,15 +9,48 @@ export default win => {
|
|
|
9
9
|
const d = win.document;
|
|
10
10
|
const f = win.document.createElement;
|
|
11
11
|
|
|
12
|
-
//
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
12
|
+
// jsdom@11.6.2 || jest@^22.0.0, console.error in Function getContext();
|
|
13
|
+
// https://github.com/jsdom/jsdom/blob/4c7698f760fc64f20b2a0ddff450eddbdd193176/lib/jsdom/living/nodes/HTMLCanvasElement-impl.js#L55-L58
|
|
14
|
+
// cosole.error will make ci error.
|
|
15
|
+
// try {
|
|
16
|
+
// // get the context 2d.
|
|
17
|
+
// const ctx = d.createElement('canvas').getContext('2d');
|
|
18
|
+
//
|
|
19
|
+
// // if canvas and context2d all exist, means mock is not needed.
|
|
20
|
+
// if (ctx) {
|
|
21
|
+
// console.warn('Context 2d of canvas is exist! No need to mock');
|
|
22
|
+
// return win;
|
|
23
|
+
// }
|
|
24
|
+
// } catch (_) {
|
|
25
|
+
// // catch the throw `Error: Not implemented: HTMLCanvasElement.prototype.getContext`
|
|
26
|
+
// // https://github.com/jsdom/jsdom/blob/4c7698f760fc64f20b2a0ddff450eddbdd193176/lib/jsdom/living/nodes/HTMLCanvasElement-impl.js
|
|
27
|
+
// // when throw error, means mock is needed.
|
|
28
|
+
// // code continue
|
|
29
|
+
// }
|
|
19
30
|
// if ctx not exist, mock it.
|
|
20
31
|
// just mock canvas creator.
|
|
21
|
-
|
|
32
|
+
|
|
33
|
+
win.document.createElement = (param) => param.toString().toLowerCase() === 'canvas'
|
|
34
|
+
? createCanvas('canvas')
|
|
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
|
+
|
|
22
55
|
return win;
|
|
23
56
|
};
|