jest-canvas-mock 2.1.0 → 2.3.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/CHANGELOG.md +32 -9
- package/README.md +38 -12
- package/lib/classes/CanvasGradient.js +16 -33
- package/lib/classes/CanvasPattern.js +8 -22
- package/lib/classes/CanvasRenderingContext2D.js +1243 -1263
- package/lib/classes/DOMMatrix.js +72 -79
- package/lib/classes/ImageBitmap.js +10 -24
- package/lib/classes/ImageData.js +50 -38
- package/lib/classes/Path2D.js +17 -33
- package/lib/classes/TextMetrics.js +19 -20
- package/lib/index.js +3 -3
- package/lib/mock/createCanvasEvent.js +4 -4
- package/lib/mock/createImageBitmap.js +13 -15
- package/lib/mock/prototype.js +12 -14
- package/lib/window.js +16 -16
- package/package.json +21 -14
- package/types/index.d.ts +40 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,30 @@
|
|
|
1
1
|
# CHANGELOG
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
## Version 2.3.0
|
|
4
|
+
|
|
5
|
+
- Added Prettier code style
|
|
6
|
+
- Deleted .npmignore and switched to `package.json` files field
|
|
7
|
+
- Added [CONTRIBUTING](./CONTRIBUTING.md) and [CODE_OF_CONDUCT](./CODE_OF_CONDUCT.md) docs
|
|
8
|
+
- Switched to `moo-color` for color parsing
|
|
9
|
+
- Added contributors to markdown document
|
|
10
|
+
- New Contributor [@evanoc0](https://github.com/evanoc0)
|
|
11
|
+
- New Contributor [@FeliciousX](https://github.com/FeliciousX)
|
|
12
|
+
- Add 2 methods of DOMMatrix [#70](https://github.com/hustcc/jest-canvas-mock/pull/70)
|
|
13
|
+
|
|
14
|
+
## Version 2.2.0
|
|
15
|
+
|
|
16
|
+
- Bug: Slice canvas transform value when pushing (#50)
|
|
17
|
+
- Remove support for node 6 and 7
|
|
18
|
+
- switch babel env to target node 8
|
|
19
|
+
- add support for node 13
|
|
20
|
+
- update package versions
|
|
21
|
+
- fix lineWidth bug
|
|
22
|
+
|
|
23
|
+
## Version 2.1.1
|
|
24
|
+
|
|
25
|
+
- Feature: Support for ImageData instantiation using a source array (#45)
|
|
26
|
+
|
|
27
|
+
## Version 2.1.0
|
|
4
28
|
|
|
5
29
|
This minor version bump now has some major snapshot support but is backwards compatible.
|
|
6
30
|
|
|
@@ -25,13 +49,13 @@ Changes:
|
|
|
25
49
|
- Feature: Add `_path` and `_events` to `Path2D`
|
|
26
50
|
- Testing: Add and test snapshot outputs
|
|
27
51
|
|
|
28
|
-
|
|
52
|
+
## Version 2.0.0
|
|
29
53
|
|
|
30
54
|
Just publish a stable version.
|
|
31
55
|
|
|
32
|
-
|
|
56
|
+
## Version 2.0.0-beta.1
|
|
33
57
|
|
|
34
|
-
|
|
58
|
+
### Class Instances
|
|
35
59
|
|
|
36
60
|
The Version 2.0.0-beta.1 of `jest-canvas-mock` is a complete overhaul of the mocking strategy entirely. Originally, the `canvas.getCanvas('2d')` method would return a single object that contained a set of methods without any property definitions. This caused problems for people who wanted to use `jest` to test and verify `instanceof` checks.
|
|
37
61
|
|
|
@@ -43,7 +67,7 @@ const ctx = document.createElement('canvas').getContext('2d');
|
|
|
43
67
|
expect(ctx).toBeInstanceOf(CanvasRenderingContext2D);
|
|
44
68
|
```
|
|
45
69
|
|
|
46
|
-
|
|
70
|
+
### Bound Mock Functions
|
|
47
71
|
|
|
48
72
|
When each `CanvasRenderingContext2D` object is created, all the methods are properly mocked with the `jest.fn()` method, and bound to the instance. It's still possible to verify that a function was called on the context. The main difference now is that the methods actually perform runtime checks on the passed parameters.
|
|
49
73
|
|
|
@@ -60,7 +84,7 @@ expect(ctx.arc).toBeCalledWith(0, 0, 100, 0, PI_2);
|
|
|
60
84
|
expect(() => ctx.arc(0, 0, -10, 0, PI_2)).toThrow(DOMExpection);
|
|
61
85
|
```
|
|
62
86
|
|
|
63
|
-
|
|
87
|
+
### Parsing Colors and Fonts, Saving, Restoring
|
|
64
88
|
|
|
65
89
|
Another really big change is that the mocking strategy now attempts to conform to the html living specification. In order to do this, two packages were added as dependencies so that css colors and fonts can be more properly parsed. This is not perfect, and any problems with the color parser or font parser should be reported in the Issues tab.
|
|
66
90
|
|
|
@@ -95,7 +119,7 @@ expect(ctx.font).toBe('10px sans-serif');
|
|
|
95
119
|
|
|
96
120
|
For all these reasons, the `jest-canvas-mock` package was bumped a major version to `2.0.0`.
|
|
97
121
|
|
|
98
|
-
|
|
122
|
+
### CanvasRenderingContext2D prototype
|
|
99
123
|
|
|
100
124
|
- Implemented Stack Properties for the following items:
|
|
101
125
|
- `direction`
|
|
@@ -367,7 +391,7 @@ For all these reasons, the `jest-canvas-mock` package was bumped a major version
|
|
|
367
391
|
- throws `TypeError` if `arguments.length < 2`
|
|
368
392
|
- performs a translate operation on the current `transform` stack value if every parameter is finite
|
|
369
393
|
|
|
370
|
-
|
|
394
|
+
### Other Changes By File
|
|
371
395
|
|
|
372
396
|
- src/CanvasGradient.js
|
|
373
397
|
- Added Class for `instanceof` checks
|
|
@@ -448,4 +472,3 @@ For all these reasons, the `jest-canvas-mock` package was bumped a major version
|
|
|
448
472
|
- This function cannot normally be constructed
|
|
449
473
|
- mocked to accept a `text` parameter
|
|
450
474
|
- sets `width` property to `text.length`
|
|
451
|
-
|
package/README.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
# jest-canvas-mock
|
|
2
2
|
|
|
3
|
-
> Mock `canvas` when run unit test cases with jest.
|
|
3
|
+
> Mock `canvas` when run unit test cases with jest. For more browser environment, you can use [jest-electron](https://github.com/hustcc/jest-electron) for real browser runtime.
|
|
4
4
|
|
|
5
5
|
[](https://travis-ci.org/hustcc/jest-canvas-mock)
|
|
6
6
|
[](https://coveralls.io/github/hustcc/jest-canvas-mock)
|
|
7
7
|
[](https://www.npmjs.com/package/jest-canvas-mock)
|
|
8
8
|
[](https://www.npmjs.com/package/jest-canvas-mock)
|
|
9
|
-
|
|
9
|
+
[](https://github.com/jest-community/awesome-jest)
|
|
10
10
|
|
|
11
11
|
## Install
|
|
12
12
|
|
|
@@ -16,7 +16,6 @@ This should only be installed as a development dependency (`devDependencies`) as
|
|
|
16
16
|
npm i --save-dev jest-canvas-mock
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
-
|
|
20
19
|
## Setup
|
|
21
20
|
|
|
22
21
|
In your `package.json` under the `jest`, create a `setupFiles` array and add `jest-canvas-mock` to the array.
|
|
@@ -41,7 +40,6 @@ If you already have a `setupFiles` attribute you can also append `jest-canvas-mo
|
|
|
41
40
|
|
|
42
41
|
More about in [configuration section](https://facebook.github.io/jest/docs/en/configuration.html#content).
|
|
43
42
|
|
|
44
|
-
|
|
45
43
|
## Setup file
|
|
46
44
|
|
|
47
45
|
Alternatively you can create a new setup file which then requires this module or
|
|
@@ -84,7 +82,7 @@ The function will do `Number` type coercion and verify the inputs exactly like t
|
|
|
84
82
|
this is valid input.
|
|
85
83
|
|
|
86
84
|
```ts
|
|
87
|
-
expect(() => ctx.arc(
|
|
85
|
+
expect(() => ctx.arc('10', '10', '20', '0', '6.14')).not.toThrow();
|
|
88
86
|
```
|
|
89
87
|
|
|
90
88
|
Another part of the strategy is to validate input types. When using the
|
|
@@ -92,17 +90,17 @@ Another part of the strategy is to validate input types. When using the
|
|
|
92
90
|
`TypeError` just like the browser does.
|
|
93
91
|
|
|
94
92
|
```ts
|
|
95
|
-
expect(() => ctx.fill(
|
|
96
|
-
expect(() => ctx.fill(new Path2D(),
|
|
93
|
+
expect(() => ctx.fill('invalid!')).toThrow(TypeError);
|
|
94
|
+
expect(() => ctx.fill(new Path2D(), 'invalid!')).toThrow(TypeError);
|
|
97
95
|
```
|
|
98
96
|
|
|
99
97
|
We try to follow the ECMAScript specification as closely as possible.
|
|
100
98
|
|
|
101
|
-
|
|
99
|
+
## Snapshots
|
|
102
100
|
|
|
103
|
-
There are multiple ways to validate canvas state. There are currently three
|
|
104
|
-
to the `CanvasRenderingContext2D` class. The first way to use this feature is by using the
|
|
105
|
-
method.
|
|
101
|
+
There are multiple ways to validate canvas state using snapshots. There are currently three methods
|
|
102
|
+
attached to the `CanvasRenderingContext2D` class. The first way to use this feature is by using the
|
|
103
|
+
`__getEvents` method.
|
|
106
104
|
|
|
107
105
|
```ts
|
|
108
106
|
/**
|
|
@@ -131,7 +129,7 @@ const path = ctx.__getPath();
|
|
|
131
129
|
expect(path).toMatchSnapshot();
|
|
132
130
|
```
|
|
133
131
|
|
|
134
|
-
The third way is to inspect all of the
|
|
132
|
+
The third way is to inspect all of the successful draw calls submitted to the context.
|
|
135
133
|
|
|
136
134
|
```ts
|
|
137
135
|
ctx.drawImage(img, 0, 0);
|
|
@@ -144,6 +142,27 @@ const calls = ctx.__getDrawCalls();
|
|
|
144
142
|
expect(calls).toMatchSnapshot();
|
|
145
143
|
```
|
|
146
144
|
|
|
145
|
+
In some cases it may be useful to clear the events or draw calls that have already been logged.
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
// Clear events
|
|
149
|
+
ctx.__clearEvents();
|
|
150
|
+
|
|
151
|
+
// Clear draw calls
|
|
152
|
+
ctx.__clearDrawCalls();
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Finally, it's possible to inspect the clipping region calls by using the `__getClippingRegion`
|
|
156
|
+
function.
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
const clippingRegion = ctx.__getClippingRegion();
|
|
160
|
+
expect(clippingRegion).toMatchSnapshot();
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
The clipping region cannot be cleared because it's based on the stack values and when the `.clip()`
|
|
164
|
+
function is called.
|
|
165
|
+
|
|
147
166
|
## Override default mock return value
|
|
148
167
|
|
|
149
168
|
You can override the default mock return value in your test to suit your need. For example, to override return value of `toDataURL`:
|
|
@@ -154,6 +173,13 @@ canvas.toDataURL.mockReturnValueOnce(
|
|
|
154
173
|
);
|
|
155
174
|
```
|
|
156
175
|
|
|
176
|
+
## Contributors
|
|
177
|
+
|
|
178
|
+
- [@hustcc](https://github.com/hustcc)
|
|
179
|
+
- [@jtenner](https://github.com/jtenner)
|
|
180
|
+
- [@evanoc0](https://github.com/evanoc0)
|
|
181
|
+
- [@FeliciousX](https://github.com/FeliciousX)
|
|
182
|
+
|
|
157
183
|
## License
|
|
158
184
|
|
|
159
185
|
MIT@[hustcc](https://github.com/hustcc).
|
|
@@ -3,46 +3,29 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
6
|
+
exports.default = void 0;
|
|
7
7
|
|
|
8
|
-
var
|
|
9
|
-
|
|
10
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
11
|
-
|
|
12
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
13
|
-
|
|
14
|
-
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
15
|
-
|
|
16
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
17
|
-
|
|
18
|
-
var CanvasGradient =
|
|
19
|
-
/*#__PURE__*/
|
|
20
|
-
function () {
|
|
21
|
-
function CanvasGradient() {
|
|
22
|
-
_classCallCheck(this, CanvasGradient);
|
|
8
|
+
var _mooColor = require("moo-color");
|
|
23
9
|
|
|
10
|
+
class CanvasGradient {
|
|
11
|
+
constructor() {
|
|
24
12
|
this.addColorStop = jest.fn(this.addColorStop.bind(this));
|
|
25
13
|
}
|
|
26
14
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
value: function addColorStop(offset, color) {
|
|
30
|
-
var numoffset = Number(offset);
|
|
31
|
-
var colorstr = String(color);
|
|
32
|
-
|
|
33
|
-
if (!Number.isFinite(numoffset) || numoffset < 0 || numoffset > 1) {
|
|
34
|
-
throw new DOMException('IndexSizeError', 'Failed to execute \'addColorStop\' on \'CanvasGradient\': The provided value (\'' + numoffset + '\') is outside the range (0.0, 1.0)');
|
|
35
|
-
}
|
|
15
|
+
addColorStop(offset, color) {
|
|
16
|
+
const numoffset = Number(offset);
|
|
36
17
|
|
|
37
|
-
|
|
18
|
+
if (!Number.isFinite(numoffset) || numoffset < 0 || numoffset > 1) {
|
|
19
|
+
throw new DOMException('IndexSizeError', "Failed to execute 'addColorStop' on 'CanvasGradient': The provided value ('" + numoffset + "') is outside the range (0.0, 1.0)");
|
|
20
|
+
}
|
|
38
21
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
22
|
+
try {
|
|
23
|
+
new _mooColor.MooColor(color);
|
|
24
|
+
} catch (e) {
|
|
25
|
+
throw new SyntaxError("Failed to execute 'addColorStop' on 'CanvasGradient': The value provided ('" + color + "') could not be parsed as a color.");
|
|
42
26
|
}
|
|
43
|
-
}
|
|
27
|
+
}
|
|
44
28
|
|
|
45
|
-
|
|
46
|
-
}();
|
|
29
|
+
}
|
|
47
30
|
|
|
48
|
-
exports
|
|
31
|
+
exports.default = CanvasGradient;
|
|
@@ -3,31 +3,17 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
7
|
-
|
|
8
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
9
|
-
|
|
10
|
-
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
11
|
-
|
|
12
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
13
|
-
|
|
14
|
-
var CanvasPattern =
|
|
15
|
-
/*#__PURE__*/
|
|
16
|
-
function () {
|
|
17
|
-
function CanvasPattern() {
|
|
18
|
-
_classCallCheck(this, CanvasPattern);
|
|
6
|
+
exports.default = void 0;
|
|
19
7
|
|
|
8
|
+
class CanvasPattern {
|
|
9
|
+
constructor() {
|
|
20
10
|
this.setTransform = jest.fn(this.setTransform.bind(this));
|
|
21
11
|
}
|
|
22
12
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if (arguments.length > 0 && !(value instanceof Object)) throw new TypeError('Failed to execute \'setTransform\' on \'CanvasPattern\': parameter 1 (\'transform\') is not an object.');
|
|
27
|
-
}
|
|
28
|
-
}]);
|
|
13
|
+
setTransform(value) {
|
|
14
|
+
if (arguments.length > 0 && !(value instanceof Object)) throw new TypeError("Failed to execute 'setTransform' on 'CanvasPattern': parameter 1 ('transform') is not an object.");
|
|
15
|
+
}
|
|
29
16
|
|
|
30
|
-
|
|
31
|
-
}();
|
|
17
|
+
}
|
|
32
18
|
|
|
33
|
-
exports
|
|
19
|
+
exports.default = CanvasPattern;
|