jest-canvas-mock 2.0.0-beta.1 → 2.1.2
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/.github/FUNDING.yml +12 -0
- package/CHANGELOG.md +33 -0
- package/README.md +60 -2
- package/lib/classes/CanvasGradient.js +4 -4
- package/lib/classes/CanvasPattern.js +2 -2
- package/lib/classes/CanvasRenderingContext2D.js +778 -101
- package/lib/classes/DOMMatrix.js +2 -2
- package/lib/classes/ImageBitmap.js +2 -2
- package/lib/classes/ImageData.js +38 -10
- package/lib/classes/Path2D.js +15 -4
- package/lib/classes/TextMetrics.js +2 -2
- package/lib/index.js +3 -3
- package/lib/mock/createCanvasEvent.js +29 -0
- package/lib/mock/createImageBitmap.js +5 -5
- package/lib/mock/prototype.js +18 -2
- package/lib/window.js +13 -13
- package/package.json +14 -11
- package/types/index.d.ts +54 -0
- package/lib/canvas.js +0 -28
- package/lib/classes/context2d.js +0 -107
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# These are supported funding model platforms
|
|
2
|
+
|
|
3
|
+
github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
|
|
4
|
+
patreon: # Replace with a single Patreon username
|
|
5
|
+
open_collective: # Replace with a single Open Collective username
|
|
6
|
+
ko_fi: # Replace with a single Ko-fi username
|
|
7
|
+
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
|
|
8
|
+
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
|
|
9
|
+
liberapay: # Replace with a single Liberapay username
|
|
10
|
+
issuehunt: # Replace with a single IssueHunt username
|
|
11
|
+
otechie: # Replace with a single Otechie username
|
|
12
|
+
custom: ['https://paypal.me/hustcc', 'https://atool.vip']
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,38 @@
|
|
|
1
1
|
# CHANGELOG
|
|
2
2
|
|
|
3
|
+
# Version 2.1.1
|
|
4
|
+
|
|
5
|
+
- Feature: Support for ImageData instantiation using a source array(#45)
|
|
6
|
+
|
|
7
|
+
# Version 2.1.0
|
|
8
|
+
|
|
9
|
+
This minor version bump now has some major snapshot support but is backwards compatible.
|
|
10
|
+
|
|
11
|
+
Changes:
|
|
12
|
+
|
|
13
|
+
- Feature: Add `static` `CanvasRenderingContext2D` method: `#.__getEvents(ctx)`
|
|
14
|
+
- Feature: Every successful modification of the `CanvasRenderingContext2D` state machine logs an `_event`
|
|
15
|
+
- Feature: Add `static` `CanvasRenderingContext2D` method: `#.__getPath(ctx)`
|
|
16
|
+
- Feature: Every path call adds a `_path` item and can be accessed via `__getPath(ctx)`
|
|
17
|
+
- Feature: `beginPath()` empties the `_path`
|
|
18
|
+
- Feature: Add `static` `CanvasRenderingContext2D` method: `#.__getDrawCalls(ctx)`
|
|
19
|
+
- Feature: Every draw call adds a `_drawCall` item and can be accessed via `__getDrawCall(ctx)`
|
|
20
|
+
- Feature: Add `types/index.d.ts` file for tooling types (in jest environment)
|
|
21
|
+
- Feature: Support node 12
|
|
22
|
+
- Docs
|
|
23
|
+
- Updated arc example
|
|
24
|
+
- Added snapshot testing documentation
|
|
25
|
+
- Bug: `createLinearGradient` now accepts strings
|
|
26
|
+
- Bug: `createRadialGradient` now accepts strings
|
|
27
|
+
- Bug: `globalAlpha` now accepts `null` per `Number` coercion
|
|
28
|
+
- Feature: Faster finite values checks
|
|
29
|
+
- Feature: Add `_path` and `_events` to `Path2D`
|
|
30
|
+
- Testing: Add and test snapshot outputs
|
|
31
|
+
|
|
32
|
+
# Version 2.0.0
|
|
33
|
+
|
|
34
|
+
Just publish a stable version.
|
|
35
|
+
|
|
3
36
|
# Version 2.0.0-beta.1
|
|
4
37
|
|
|
5
38
|
## Class Instances
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
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)
|
|
@@ -73,8 +73,11 @@ error. For instance, the `CanvasRenderingContext2D#arc` function will throw a `T
|
|
|
73
73
|
radius is negative, or if it was not provided with enough parameters.
|
|
74
74
|
|
|
75
75
|
```ts
|
|
76
|
+
// arc throws a TypeError when the argument length is less than 5
|
|
76
77
|
expect(() => ctx.arc(1, 2, 3, 4)).toThrow(TypeError);
|
|
77
|
-
|
|
78
|
+
|
|
79
|
+
// when radius is negative, arc throws a dom exception when all parameters are finite
|
|
80
|
+
expect(() => ctx.arc(0, 0, -10, 0, Math.PI * 2)).toThrow(DOMException);
|
|
78
81
|
```
|
|
79
82
|
|
|
80
83
|
The function will do `Number` type coercion and verify the inputs exactly like the browser does. So
|
|
@@ -95,6 +98,61 @@ expect(() => ctx.fill(new Path2D(), "invalid!")).toThrow(TypeError);
|
|
|
95
98
|
|
|
96
99
|
We try to follow the ECMAScript specification as closely as possible.
|
|
97
100
|
|
|
101
|
+
# Snapshots
|
|
102
|
+
|
|
103
|
+
There are multiple ways to validate canvas state. There are currently three `static` methods attached
|
|
104
|
+
to the `CanvasRenderingContext2D` class. The first way to use this feature is by using the `__getEvents`
|
|
105
|
+
method.
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
/**
|
|
109
|
+
* In order to see which functions and properties were used for the test, you can use `__getEvents`
|
|
110
|
+
* to gather this information.
|
|
111
|
+
*/
|
|
112
|
+
const events = ctx.__getEvents();
|
|
113
|
+
|
|
114
|
+
expect(events).toMatchSnapshot(); // jest will assert the events match the snapshot
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
The second way is to inspect the current path associated with the context.
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
ctx.beginPath();
|
|
121
|
+
ctx.arc(1, 2, 3, 4, 5);
|
|
122
|
+
ctx.moveTo(6, 7);
|
|
123
|
+
ctx.rect(6, 7, 8, 9);
|
|
124
|
+
ctx.closePath();
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Any method that modifies the current path (and subpath) will be pushed to an event array. When
|
|
128
|
+
* using the `__getPath` method, that array will sliced and usable for snapshots.
|
|
129
|
+
*/
|
|
130
|
+
const path = ctx.__getPath();
|
|
131
|
+
expect(path).toMatchSnapshot();
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
The third way is to inspect all of the success draw calls submitted to the context.
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
ctx.drawImage(img, 0, 0);
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Every drawImage, fill, stroke, fillText, or strokeText function call will be logged in an event
|
|
141
|
+
* array. This method will return those events here for inspection.
|
|
142
|
+
*/
|
|
143
|
+
const calls = ctx.__getDrawCalls();
|
|
144
|
+
expect(calls).toMatchSnapshot();
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Override default mock return value
|
|
148
|
+
|
|
149
|
+
You can override the default mock return value in your test to suit your need. For example, to override return value of `toDataURL`:
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
canvas.toDataURL.mockReturnValueOnce(
|
|
153
|
+
'data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='
|
|
154
|
+
);
|
|
155
|
+
```
|
|
98
156
|
|
|
99
157
|
## License
|
|
100
158
|
|
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
6
|
+
exports["default"] = void 0;
|
|
7
7
|
|
|
8
8
|
var _parseColor = _interopRequireDefault(require("parse-color"));
|
|
9
9
|
|
|
10
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
11
11
|
|
|
12
12
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
13
13
|
|
|
@@ -34,7 +34,7 @@ function () {
|
|
|
34
34
|
throw new DOMException('IndexSizeError', 'Failed to execute \'addColorStop\' on \'CanvasGradient\': The provided value (\'' + numoffset + '\') is outside the range (0.0, 1.0)');
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
var output = (0, _parseColor
|
|
37
|
+
var output = (0, _parseColor["default"])(colorstr);
|
|
38
38
|
|
|
39
39
|
if (!output.hex) {
|
|
40
40
|
throw new SyntaxError('Failed to execute \'addColorStop\' on \'CanvasGradient\': The value provided (\'' + color + '\') could not be parsed as a color.');
|
|
@@ -45,4 +45,4 @@ function () {
|
|
|
45
45
|
return CanvasGradient;
|
|
46
46
|
}();
|
|
47
47
|
|
|
48
|
-
exports
|
|
48
|
+
exports["default"] = CanvasGradient;
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
6
|
+
exports["default"] = void 0;
|
|
7
7
|
|
|
8
8
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
9
9
|
|
|
@@ -30,4 +30,4 @@ function () {
|
|
|
30
30
|
return CanvasPattern;
|
|
31
31
|
}();
|
|
32
32
|
|
|
33
|
-
exports
|
|
33
|
+
exports["default"] = CanvasPattern;
|