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/CHANGELOG.md CHANGED
@@ -1,427 +1,463 @@
1
- # CHANGELOG
2
-
3
- # Version 2.0.0
4
-
5
- Just publish a stable version.
6
-
7
-
8
- # Version 2.0.0-beta.1
9
-
10
- ## Class Instances
11
-
12
- 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.
13
-
14
- Now the following expectation works as expected.
15
-
16
- ```ts
17
- const ctx = document.createElement('canvas').getContext('2d');
18
-
19
- expect(ctx).toBeInstanceOf(CanvasRenderingContext2D);
20
- ```
21
-
22
- ## Bound Mock Functions
23
-
24
- 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.
25
-
26
- The following example demonstrates that canvas methods can be called, and parameters are verified.
27
-
28
- ```ts
29
- const PI_2 = Math.PI * 2;
30
-
31
- // create a circle at 0,0 with radius 100
32
- ctx.arc(0, 0, 100, 0, PI_2);
33
- expect(ctx.arc).toBeCalledWith(0, 0, 100, 0, PI_2);
34
-
35
- // negative radius values throw DOMException errors
36
- expect(() => ctx.arc(0, 0, -10, 0, PI_2)).toThrow(DOMExpection);
37
- ```
38
-
39
- # Parsing Colors and Fonts, Saving, Restoring
40
-
41
- 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.
42
-
43
- This change comes with overhauled `ctx.save()` and `ctx.restore()` functions. These function calls work almost entirely as intended. For instance, `ctx.save()` actually pushes all the current property values to a stack, and this operation is undone when `ctx.restore()` is called. Take the following snippet of code as an example.
44
-
45
- ```ts
46
- // create a context
47
- const ctx = document.createElement('canvas').getContext('2d');
48
-
49
- // save the current state of the canvas
50
- ctx.save();
51
-
52
- // set some color and font values
53
- ctx.fillStyle = 'blue';
54
- ctx.font = '12pt Times New Roman';
55
-
56
- // now the fillStyle property parses the color
57
- expect(ctx.fillStyle).toBe('#00F');
58
-
59
- // font parsing also works as intended
60
- expect(ctx.font).toBe('16px "Times New Roman"');
61
-
62
- // restore the previously saved state
63
- ctx.restore():
64
-
65
- // the fillStyle was restored to default
66
- expect(ctx.fillStyle).toBe('#000');
67
-
68
- // the font was restored too
69
- expect(ctx.font).toBe('10px sans-serif');
70
- ```
71
-
72
- For all these reasons, the `jest-canvas-mock` package was bumped a major version to `2.0.0`.
73
-
74
- ## CanvasRenderingContext2D prototype
75
-
76
- - Implemented Stack Properties for the following items:
77
- - `direction`
78
- - `fillStyle`
79
- - `filter`
80
- - `font`
81
- - `globalAlpha`
82
- - `globalCompositeOperation`
83
- - `imageSmoothingEnabled`
84
- - `imageSmoothingQuality`
85
- - `lineCap`
86
- - `lineDashOffset`
87
- - `lineDash` (via `getLineDash()` / `setLineDash()`)
88
- - `lineJoin`
89
- - `lineWidth`
90
- - `miterLimit`
91
- - `shadowBlur`
92
- - `shadowColor`
93
- - `shadowOffsetX`
94
- - `shadowOffsetY`
95
- - `stack`
96
- - `strokeStyle`
97
- - `textAlign`
98
- - `textBaseline`
99
- - `transform` (via `setTransform` etc.)
100
- - function `constructor` binds the following functions
101
- - `setLineDash`
102
- - `getLineDash`
103
- - `setTransform`
104
- - `getTransform`
105
- - `getImageData`
106
- - `save`
107
- - `restore`
108
- - `createPattern`
109
- - `createRadialGradient`
110
- - `addHitRegion`
111
- - `arc`
112
- - `arcTo`
113
- - `beginPath`
114
- - `clip`
115
- - `closePath`
116
- - `scale`
117
- - `stroke`
118
- - `clearHitRegions`
119
- - `clearRect`
120
- - `fillRect`
121
- - `strokeRect`
122
- - `rect`
123
- - `resetTransform`
124
- - `translate`
125
- - `moveTo`
126
- - `lineTo`
127
- - `bezierCurveTo`
128
- - `createLinearGradient`
129
- - `ellipse`
130
- - `measureText`
131
- - `rotate`
132
- - `drawImage`
133
- - `drawFocusIfNeeded`
134
- - `isPointInPath`
135
- - `isPointInStroke`
136
- - `putImageData`
137
- - `strokeText`
138
- - `fillText`
139
- - `quadraticCurveTo`
140
- - `removeHitRegion`
141
- - `fill`
142
- - `transform`
143
- - `scrollPathIntoView`
144
- - `createImageData`
145
- - function `addHitRegion`
146
- - verifies `path` or `id` parameter is set, throws `DOMException` otherwise
147
- - verifies `fillRule` if set, throws `TypeError` if invalid
148
- - function `arc`
149
- - throws `TypeError` if `arguments.length < 5`
150
- - throws `DOMException(IndexSizeError)` if values are finite, but radius is negative
151
- - function `arcTo`
152
- - throws `TypeError` if `arguments.length < 5`
153
- - throws `DOMException(IndexSizeError)` if values are finite, but radius is negative
154
- - function `beginPath` (empty noOp stub)
155
- - if path length eventually needs to be verified, this can be changed
156
- - function `bezierCurveTo`
157
- - throws `TypeError` if `arguments.length < 6`
158
- - readonly property `canvas`
159
- - returns the parent `HTMLCanvasElement`
160
- - function `clearHitRegions` (empty noOp stub)
161
- - function `clearRect`
162
- - throws `TypeError` if `arguments.length < 4`
163
- - function `clip`
164
- - if `fillRule` is provided, throws `TypeErorr` if `FillRule` is invalid
165
- - if `path` is provided, throws `TypeError` if path is not `instanceof` `Path2D`
166
- - function `closePath` (added noOp stub)
167
- - function `createImageData`
168
- - throws `TypeError` if `arguments.length === 1` and parameter is not `instanceof` `ImageData`
169
- - throws `TypeError` if `arguments.length >= 2` and `width` is not finite
170
- - throws `TypeError` if `arguments.length >= 2` and `height` is not finite
171
- - returns `ImageData`
172
- - function `createLinearGradient`
173
- - throws `TypeError` if `arguments.length < 4`
174
- - throws `TypeError` if `x0` is not finite
175
- - throws `TypeError` if `y0` is not finite
176
- - throws `TypeError` if `x1` is not finite
177
- - throws `TypeError` if `y1` is not finite
178
- - returns `CanvasGradient`
179
- - function `createPattern`
180
- - throws `TypeError` if image is not supported in `jest-canvas-mock`
181
- - throws `TypeError` if `arguments.length < 4`
182
- - throws `DOMException('SyntaxError')` if image is detached
183
- - returns `CanvasPattern` if image is `HTMLImageElement`, `ImageBitmap`, `HTMLVideoElement`, `HTMLCanvasElement`
184
- - function `createRadialGradient`
185
- - throws `TypeError` if `arguments.length < 4`
186
- - throws `TypeError` if `x0` is not finite
187
- - throws `TypeError` if `y0` is not finite
188
- - throws `TypeError` if `r0` is not finite
189
- - throws `TypeError` if `x1` is not finite
190
- - throws `TypeError` if `y1` is not finite
191
- - throws `TypeError` if `r1` is not finite
192
- - throws `DOMException('DataError')` if `r0` is negative
193
- - throws `DOMException('DataError')` if `r1` is negative
194
- - computed property `currentTransform`
195
- - sets transform stack value if `value instanceof DOMMatrix`
196
- - returns new `DOMMatrix` with current transform values
197
- - computed property `direction`
198
- - sets direction stack value when valid
199
- - returns current direction stack value
200
- - function `drawFocusIfNeeded`
201
- - throws `TypeError` if `arguments.length === 0`
202
- - throws `TypeError` if `arguments.length === 2` and `path` is not instanceof `Path2D`
203
- - throws `TypeError` if `element` is not instanceof `Element`
204
- - function drawImage
205
- - Valid arities are: [3, 5, 9] (throws `TypeError` if `arguments.length` is not valid)
206
- - throws `TypeError` if image is not supported by `jest-canvas-mock`
207
- - function `ellipse`
208
- - throws `TypeError` if `arguments.length < 7`
209
- - throws `DOMException('IndexSizeError')` if `radiusX` is negative and all parameters are finite
210
- - throws `DOMException('IndexSizeError')` if `radiusY` is negative and all parameters are finite
211
- - function `fill`
212
- - throws `TypeError` if `fillRule` is not valid `FillRule`
213
- - throws `TypeError` if `path` is not instanceof `Path2D`
214
- - function `fillRect`
215
- - throws `TypeError` if `arguments.length < 4`
216
- - computed property `fillStyle`
217
- - sets current `fillStyle` stack value if it's a valid css color, a `CanvasGradient` or a `CanvasPattern`
218
- - returns current `fillStyle` stack value
219
- - function `fillText`
220
- - throws `TypeError` if `arguments.length < 3`
221
- - computed property `filter`
222
- - sets the current `filter` stack value if it's a string
223
- - returns the current `filter` stack value
224
- - TODO: add custom parser for filter values to check validity
225
- - computed property `font`
226
- - sets the current `font` stack value if it's a valid font
227
- - returns the current `font` stack value
228
- - function `getImageData`
229
- - returns new `ImageData` with the same size as the parent `canvas`
230
- - function `getLineDash`
231
- - returns the current `lineDash` stack value
232
- - function `setLineDash`
233
- - throws `TypeError` if `lineDash` value is not a valid sequence
234
- - sets the current `lineDash` stack value
235
- - properly concatenates itself if the `lineDash` length is odd
236
- - function `getTransform`
237
- - returns current `transform` stack value in the form of a `DOMMatrix`
238
- - computed property `globalAlpha`
239
- - sets current `globalAlpha` stack value if `value` finite and between the inclusive range `[0 .. 1]`
240
- - returns the current `globalAlpha` stack value
241
- - computed property `globalCompositeOperation`
242
- - sets the current `globalCompositeOperation` stack value if `value` is a valid GlobalCompositeOperation value
243
- - returns the current `globalCompositeOperation` stack value
244
- - computed property `imageSmoothingEnabled`
245
- - sets current `imageSmoothingEnabled` stack value to `value` coerced to a boolean
246
- - returns current `imageSmoothingEnabled` stack value
247
- - computed property `imageSmoothingQuality`
248
- - sets the `imageSmoothingQuality` stack value if value is a valid ImageSmoothingQuality value
249
- - returns the current `imageSmoothingQuality` stack value
250
- - function `isPointInPath`
251
- - throws `TypeError` if `arguments.length < 2`
252
- - throws `TypeError` if provided `fillRule` is not a valid `FillRule`
253
- - TODO: Implement pathing operations and perform an actual `isPointInPath()` operation
254
- - always returns false
255
- - function `isPointInStroke`
256
- - throws `TypeError` if `arguments.length < 2`
257
- - always returns false
258
- - computed property `lineCap`
259
- - sets the current `lineCap` stack value if value is a valid `LineCap`
260
- - returns the current `lineCap` stack value
261
- - computed property `lineDashOffset`
262
- - sets the current `lineDashOffset` stack value if value is finite
263
- - returns the current `lineDashOffset` stack value
264
- - computed property `lineJoin`
265
- - sets the current `lineJoin` stack value if value is a valid `LineJoin`
266
- - returns the current `lineJoin` stack value
267
- - function `lineTo`
268
- - throws `TypeError` if `arguments.length < 2`
269
- - computed property `lineWidth`
270
- - sets the current `lineWidth` stack value if value is finite and greater than 0
271
- - returns the current `lineWidth` stack value
272
- - function `measureText`
273
- - throws `TypeError` if `arguments.length < 1`
274
- - returns a `TextMetrics` object
275
- - computed property `miterLimit`
276
- - sets the current `miterLimit` stack value if value is finite and greater than 0
277
- - returns the current `miterLimit` stack value
278
- - function `moveTo`
279
- - throws `TypeError` if `arguments.length < 2`
280
- - function `putImageData`
281
- - Valid arities are: [3, 7], throws `TypeError` if `arguments.length` is not valid arity
282
- - throws `TypeError` if `data` is not instanceof `ImageData`
283
- - function `quadraticCurveTo`
284
- - throws `TypeError` if `arguments.length < 4`
285
- - function `rect`
286
- - throws `TypeError` if `arguments.length < 4`
287
- - function `removeHitRegion`
288
- - throws `TypeError` if `arguments.length < 1`
289
- - function `resetTransform`
290
- - sets current `transform` stack value to the 2d identity matrix
291
- - function `restore`
292
- - pops all the property stack values and decreases the stack index
293
- - function `rotate`
294
- - throws `TypeError` if `arguments.length < 1`
295
- - rotates the current `transform` stack value if `angle` is finite
296
- - function `save`
297
- - pushes the current property stack values to the next item on the stack
298
- - increases the stack index
299
- - function `scale`
300
- - throws `TypeError` if `arguments.length < 2`
301
- - scales the current `transform` stack value if the `x` and `y` values are finite
302
- - function `scrollPathIntoView` (empty noOp stub)
303
- - function `setTransform`
304
- - if `arguments.length === 0` sets the current `transform` stack value to the 2d identity matrix
305
- - if `arguments.length == 1`
306
- - throws `TypeError` if value is not instanceof `DOMMatrix`
307
- - sets the current `transform` stack value to the provided matrix
308
- - throws `TypeError` if `arguments.length < 6`
309
- - coerces each parameter to a number via `Number()`
310
- - sets the current `transform` stack value if the provided values are all finite
311
- - computed property `shadowBlur`
312
- - sets the current `shadowBlur` stack value if value is finite and greater than 0
313
- - returns the current `shadowBlur` stack value
314
- - computed property `shadowColor`
315
- - sets the current `shadowColor` stack value if value is a valid css color
316
- - returns the current `shadowColor` stack value
317
- - computed property `shadowOffsetX`
318
- - sets the current `shadowOffsetX` stack value if value is finite
319
- - returns the current `shadowOffsetX` stack value
320
- - computed property `shadowOffsetY`
321
- - sets the current `shadowOffsetY` stack value if value is finite
322
- - returns the current `shadowOffsetY` stack value
323
- - function `stroke`
324
- - throws `TypeError` if `path` is not instanceof `Path2D`
325
- - function `strokeRect`
326
- - throws `TypeError` if `arguments.length < 4`
327
- - computed property `strokeStyle`
328
- - sets current `fillStyle` stack value if it's a valid css color, a `CanvasGradient` or a `CanvasPattern`
329
- - returns current `fillStyle` stack value
330
- - function `strokeText`
331
- - throws `TypeError` if `arguments.length < 3`
332
- - computed property `textAlign`
333
- - sets the current `textAlign` stack value if value is a valid `TextAlign` value
334
- - returns the current `textAlign` stack value
335
- - computed property `textBaseline`
336
- - sets the current `textBaseline` stack value if value is a valid `TextBaseline` value
337
- - returns the current `textBaseline` stack value
338
- - function `transform`
339
- - throws `TypeError` if `arguments.length < 6`
340
- - coerces each value to number via `Number()`
341
- - performs a transform operation on the current `transform` stack value if every parameter is finite
342
- - function `translate`
343
- - throws `TypeError` if `arguments.length < 2`
344
- - performs a translate operation on the current `transform` stack value if every parameter is finite
345
-
346
- ## Other Changes By File
347
-
348
- - src/CanvasGradient.js
349
- - Added Class for `instanceof` checks
350
- - bound functions:
351
- - `addColorStop`
352
- - function `addColorStop`
353
- - throws `IndexSizeError` `DOMException` if resulting offset is not finite
354
- - throws `SyntaxError` when color cannot be parsed
355
- - src/CanvasPattern.js
356
- - Added Class for `instanceof` checks
357
- - bound functions:
358
- - `setTransform`
359
- - function `setTransform`
360
- - throws `TypeError` if argument.length > 1 and parameter is not an object
361
- - src/DOMMatrix.js
362
- - Added minimal `DOMMatrix` implementation
363
- - Added Class for `instanceof` checks
364
- - function `constructor`
365
- - constructs 3d matrix when parameter is ArrayLike and `length === 16`
366
- - constructs 2d matrix parameter is ArrayLike and `length === 6`
367
- - throws else if provided a first argument
368
- - constructs an identity 2d matrix if no arguments are passed
369
- - property `matrix.isIdentity`
370
- - returns `true` if matrix is an identity matrix
371
- - computed properties `a-f`
372
- - returns and sets values according to the HTML Living Specification
373
- - property `is2D`
374
- - returns `true` if matrix was constructed as a 2d matrix
375
- - TODO:
376
- - Make `m11-m44` computed properties
377
- - Perform `Number()` coercion in setters
378
- - src/ImageBitmap.js
379
- - Added helper Class for `instanceof` checks
380
- - bound functions:
381
- - `close`
382
- - function `close`
383
- - "closes" the bitmap and causes `drawImage` function calls to fail
384
- - src/ImageData.js
385
- - Added helper class for `instanceof` checks
386
- - computed readonly `width`, `height` and `data` properties
387
- - function `constructor`
388
- - throws `TypeError` if width is not finite
389
- - throws `TypeError` if width is `0`
390
- - throws `TypeError` if height is not finite
391
- - throws `TypeError` if height is `0`
392
- - creates an empty `Uint8ClampedArray` of `width * height * 4` size
393
- - src/Path2D.js
394
- - Added helper class for `instanceof` checks
395
- - Borrows path function definitions from `CanvasRenderingContext2D` for convenience
396
- - `closePath` implemented from `CanvasRenderingContext2D`
397
- - `moveTo` implemented from `CanvasRenderingContext2D`
398
- - `lineTo` implemented from `CanvasRenderingContext2D`
399
- - `bezierCurveTo` implemented from `CanvasRenderingContext2D`
400
- - `quadraticCurveTo` implemented from `CanvasRenderingContext2D`
401
- - `arc` implemented from `CanvasRenderingContext2D`
402
- - `arcTo` implemented from `CanvasRenderingContext2D`
403
- - `ellipse` implemented from `CanvasRenderingContext2D`
404
- - `rect` implemented from `CanvasRenderingContext2D`
405
- - function `addPath`
406
- - throws `TypeError` if `arguments.length < 1`
407
- - throws `TypeError` if provided `path` is not `instanceof` `Path2D`
408
- - src/TextMetrics.js
409
- - Added helper class for `instanceof` checks
410
- - Implemented data properties
411
- - `width`
412
- - `actualBoundingBoxLeft`
413
- - `actualBoundingBoxRight`
414
- - `fontBoundingBoxAscent`
415
- - `fontBoundingBoxDescent`
416
- - `actualBoundingBoxAscent`
417
- - `actualBoundingBoxDescent`
418
- - `emHeightAscent`
419
- - `emHeightDescent`
420
- - `hangingBaseline`
421
- - `alphabeticBaseline`
422
- - `ideographicBaseline`
423
- - function `constructor`
424
- - This function cannot normally be constructed
425
- - mocked to accept a `text` parameter
426
- - sets `width` property to `text.length`
427
-
1
+ # CHANGELOG
2
+
3
+ ## Version 2.2.0
4
+
5
+ - Bug: Slice canvas transform value when pushing (#50)
6
+ - Remove support for node 6 and 7
7
+ - switch babel env to target node 8
8
+ - add support for node 13
9
+ - update package versions
10
+ - fix lineWidth bug
11
+
12
+ ## Version 2.1.1
13
+
14
+ - Feature: Support for ImageData instantiation using a source array (#45)
15
+
16
+ ## Version 2.1.0
17
+
18
+ This minor version bump now has some major snapshot support but is backwards compatible.
19
+
20
+ Changes:
21
+
22
+ - Feature: Add `static` `CanvasRenderingContext2D` method: `#.__getEvents(ctx)`
23
+ - Feature: Every successful modification of the `CanvasRenderingContext2D` state machine logs an `_event`
24
+ - Feature: Add `static` `CanvasRenderingContext2D` method: `#.__getPath(ctx)`
25
+ - Feature: Every path call adds a `_path` item and can be accessed via `__getPath(ctx)`
26
+ - Feature: `beginPath()` empties the `_path`
27
+ - Feature: Add `static` `CanvasRenderingContext2D` method: `#.__getDrawCalls(ctx)`
28
+ - Feature: Every draw call adds a `_drawCall` item and can be accessed via `__getDrawCall(ctx)`
29
+ - Feature: Add `types/index.d.ts` file for tooling types (in jest environment)
30
+ - Feature: Support node 12
31
+ - Docs
32
+ - Updated arc example
33
+ - Added snapshot testing documentation
34
+ - Bug: `createLinearGradient` now accepts strings
35
+ - Bug: `createRadialGradient` now accepts strings
36
+ - Bug: `globalAlpha` now accepts `null` per `Number` coercion
37
+ - Feature: Faster finite values checks
38
+ - Feature: Add `_path` and `_events` to `Path2D`
39
+ - Testing: Add and test snapshot outputs
40
+
41
+ ## Version 2.0.0
42
+
43
+ Just publish a stable version.
44
+
45
+ ## Version 2.0.0-beta.1
46
+
47
+ ### Class Instances
48
+
49
+ 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.
50
+
51
+ Now the following expectation works as expected.
52
+
53
+ ```ts
54
+ const ctx = document.createElement('canvas').getContext('2d');
55
+
56
+ expect(ctx).toBeInstanceOf(CanvasRenderingContext2D);
57
+ ```
58
+
59
+ ### Bound Mock Functions
60
+
61
+ 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.
62
+
63
+ The following example demonstrates that canvas methods can be called, and parameters are verified.
64
+
65
+ ```ts
66
+ const PI_2 = Math.PI * 2;
67
+
68
+ // create a circle at 0,0 with radius 100
69
+ ctx.arc(0, 0, 100, 0, PI_2);
70
+ expect(ctx.arc).toBeCalledWith(0, 0, 100, 0, PI_2);
71
+
72
+ // negative radius values throw DOMException errors
73
+ expect(() => ctx.arc(0, 0, -10, 0, PI_2)).toThrow(DOMExpection);
74
+ ```
75
+
76
+ ### Parsing Colors and Fonts, Saving, Restoring
77
+
78
+ 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.
79
+
80
+ This change comes with overhauled `ctx.save()` and `ctx.restore()` functions. These function calls work almost entirely as intended. For instance, `ctx.save()` actually pushes all the current property values to a stack, and this operation is undone when `ctx.restore()` is called. Take the following snippet of code as an example.
81
+
82
+ ```ts
83
+ // create a context
84
+ const ctx = document.createElement('canvas').getContext('2d');
85
+
86
+ // save the current state of the canvas
87
+ ctx.save();
88
+
89
+ // set some color and font values
90
+ ctx.fillStyle = 'blue';
91
+ ctx.font = '12pt Times New Roman';
92
+
93
+ // now the fillStyle property parses the color
94
+ expect(ctx.fillStyle).toBe('#00F');
95
+
96
+ // font parsing also works as intended
97
+ expect(ctx.font).toBe('16px "Times New Roman"');
98
+
99
+ // restore the previously saved state
100
+ ctx.restore():
101
+
102
+ // the fillStyle was restored to default
103
+ expect(ctx.fillStyle).toBe('#000');
104
+
105
+ // the font was restored too
106
+ expect(ctx.font).toBe('10px sans-serif');
107
+ ```
108
+
109
+ For all these reasons, the `jest-canvas-mock` package was bumped a major version to `2.0.0`.
110
+
111
+ ### CanvasRenderingContext2D prototype
112
+
113
+ - Implemented Stack Properties for the following items:
114
+ - `direction`
115
+ - `fillStyle`
116
+ - `filter`
117
+ - `font`
118
+ - `globalAlpha`
119
+ - `globalCompositeOperation`
120
+ - `imageSmoothingEnabled`
121
+ - `imageSmoothingQuality`
122
+ - `lineCap`
123
+ - `lineDashOffset`
124
+ - `lineDash` (via `getLineDash()` / `setLineDash()`)
125
+ - `lineJoin`
126
+ - `lineWidth`
127
+ - `miterLimit`
128
+ - `shadowBlur`
129
+ - `shadowColor`
130
+ - `shadowOffsetX`
131
+ - `shadowOffsetY`
132
+ - `stack`
133
+ - `strokeStyle`
134
+ - `textAlign`
135
+ - `textBaseline`
136
+ - `transform` (via `setTransform` etc.)
137
+ - function `constructor` binds the following functions
138
+ - `setLineDash`
139
+ - `getLineDash`
140
+ - `setTransform`
141
+ - `getTransform`
142
+ - `getImageData`
143
+ - `save`
144
+ - `restore`
145
+ - `createPattern`
146
+ - `createRadialGradient`
147
+ - `addHitRegion`
148
+ - `arc`
149
+ - `arcTo`
150
+ - `beginPath`
151
+ - `clip`
152
+ - `closePath`
153
+ - `scale`
154
+ - `stroke`
155
+ - `clearHitRegions`
156
+ - `clearRect`
157
+ - `fillRect`
158
+ - `strokeRect`
159
+ - `rect`
160
+ - `resetTransform`
161
+ - `translate`
162
+ - `moveTo`
163
+ - `lineTo`
164
+ - `bezierCurveTo`
165
+ - `createLinearGradient`
166
+ - `ellipse`
167
+ - `measureText`
168
+ - `rotate`
169
+ - `drawImage`
170
+ - `drawFocusIfNeeded`
171
+ - `isPointInPath`
172
+ - `isPointInStroke`
173
+ - `putImageData`
174
+ - `strokeText`
175
+ - `fillText`
176
+ - `quadraticCurveTo`
177
+ - `removeHitRegion`
178
+ - `fill`
179
+ - `transform`
180
+ - `scrollPathIntoView`
181
+ - `createImageData`
182
+ - function `addHitRegion`
183
+ - verifies `path` or `id` parameter is set, throws `DOMException` otherwise
184
+ - verifies `fillRule` if set, throws `TypeError` if invalid
185
+ - function `arc`
186
+ - throws `TypeError` if `arguments.length < 5`
187
+ - throws `DOMException(IndexSizeError)` if values are finite, but radius is negative
188
+ - function `arcTo`
189
+ - throws `TypeError` if `arguments.length < 5`
190
+ - throws `DOMException(IndexSizeError)` if values are finite, but radius is negative
191
+ - function `beginPath` (empty noOp stub)
192
+ - if path length eventually needs to be verified, this can be changed
193
+ - function `bezierCurveTo`
194
+ - throws `TypeError` if `arguments.length < 6`
195
+ - readonly property `canvas`
196
+ - returns the parent `HTMLCanvasElement`
197
+ - function `clearHitRegions` (empty noOp stub)
198
+ - function `clearRect`
199
+ - throws `TypeError` if `arguments.length < 4`
200
+ - function `clip`
201
+ - if `fillRule` is provided, throws `TypeErorr` if `FillRule` is invalid
202
+ - if `path` is provided, throws `TypeError` if path is not `instanceof` `Path2D`
203
+ - function `closePath` (added noOp stub)
204
+ - function `createImageData`
205
+ - throws `TypeError` if `arguments.length === 1` and parameter is not `instanceof` `ImageData`
206
+ - throws `TypeError` if `arguments.length >= 2` and `width` is not finite
207
+ - throws `TypeError` if `arguments.length >= 2` and `height` is not finite
208
+ - returns `ImageData`
209
+ - function `createLinearGradient`
210
+ - throws `TypeError` if `arguments.length < 4`
211
+ - throws `TypeError` if `x0` is not finite
212
+ - throws `TypeError` if `y0` is not finite
213
+ - throws `TypeError` if `x1` is not finite
214
+ - throws `TypeError` if `y1` is not finite
215
+ - returns `CanvasGradient`
216
+ - function `createPattern`
217
+ - throws `TypeError` if image is not supported in `jest-canvas-mock`
218
+ - throws `TypeError` if `arguments.length < 4`
219
+ - throws `DOMException('SyntaxError')` if image is detached
220
+ - returns `CanvasPattern` if image is `HTMLImageElement`, `ImageBitmap`, `HTMLVideoElement`, `HTMLCanvasElement`
221
+ - function `createRadialGradient`
222
+ - throws `TypeError` if `arguments.length < 4`
223
+ - throws `TypeError` if `x0` is not finite
224
+ - throws `TypeError` if `y0` is not finite
225
+ - throws `TypeError` if `r0` is not finite
226
+ - throws `TypeError` if `x1` is not finite
227
+ - throws `TypeError` if `y1` is not finite
228
+ - throws `TypeError` if `r1` is not finite
229
+ - throws `DOMException('DataError')` if `r0` is negative
230
+ - throws `DOMException('DataError')` if `r1` is negative
231
+ - computed property `currentTransform`
232
+ - sets transform stack value if `value instanceof DOMMatrix`
233
+ - returns new `DOMMatrix` with current transform values
234
+ - computed property `direction`
235
+ - sets direction stack value when valid
236
+ - returns current direction stack value
237
+ - function `drawFocusIfNeeded`
238
+ - throws `TypeError` if `arguments.length === 0`
239
+ - throws `TypeError` if `arguments.length === 2` and `path` is not instanceof `Path2D`
240
+ - throws `TypeError` if `element` is not instanceof `Element`
241
+ - function drawImage
242
+ - Valid arities are: [3, 5, 9] (throws `TypeError` if `arguments.length` is not valid)
243
+ - throws `TypeError` if image is not supported by `jest-canvas-mock`
244
+ - function `ellipse`
245
+ - throws `TypeError` if `arguments.length < 7`
246
+ - throws `DOMException('IndexSizeError')` if `radiusX` is negative and all parameters are finite
247
+ - throws `DOMException('IndexSizeError')` if `radiusY` is negative and all parameters are finite
248
+ - function `fill`
249
+ - throws `TypeError` if `fillRule` is not valid `FillRule`
250
+ - throws `TypeError` if `path` is not instanceof `Path2D`
251
+ - function `fillRect`
252
+ - throws `TypeError` if `arguments.length < 4`
253
+ - computed property `fillStyle`
254
+ - sets current `fillStyle` stack value if it's a valid css color, a `CanvasGradient` or a `CanvasPattern`
255
+ - returns current `fillStyle` stack value
256
+ - function `fillText`
257
+ - throws `TypeError` if `arguments.length < 3`
258
+ - computed property `filter`
259
+ - sets the current `filter` stack value if it's a string
260
+ - returns the current `filter` stack value
261
+ - TODO: add custom parser for filter values to check validity
262
+ - computed property `font`
263
+ - sets the current `font` stack value if it's a valid font
264
+ - returns the current `font` stack value
265
+ - function `getImageData`
266
+ - returns new `ImageData` with the same size as the parent `canvas`
267
+ - function `getLineDash`
268
+ - returns the current `lineDash` stack value
269
+ - function `setLineDash`
270
+ - throws `TypeError` if `lineDash` value is not a valid sequence
271
+ - sets the current `lineDash` stack value
272
+ - properly concatenates itself if the `lineDash` length is odd
273
+ - function `getTransform`
274
+ - returns current `transform` stack value in the form of a `DOMMatrix`
275
+ - computed property `globalAlpha`
276
+ - sets current `globalAlpha` stack value if `value` finite and between the inclusive range `[0 .. 1]`
277
+ - returns the current `globalAlpha` stack value
278
+ - computed property `globalCompositeOperation`
279
+ - sets the current `globalCompositeOperation` stack value if `value` is a valid GlobalCompositeOperation value
280
+ - returns the current `globalCompositeOperation` stack value
281
+ - computed property `imageSmoothingEnabled`
282
+ - sets current `imageSmoothingEnabled` stack value to `value` coerced to a boolean
283
+ - returns current `imageSmoothingEnabled` stack value
284
+ - computed property `imageSmoothingQuality`
285
+ - sets the `imageSmoothingQuality` stack value if value is a valid ImageSmoothingQuality value
286
+ - returns the current `imageSmoothingQuality` stack value
287
+ - function `isPointInPath`
288
+ - throws `TypeError` if `arguments.length < 2`
289
+ - throws `TypeError` if provided `fillRule` is not a valid `FillRule`
290
+ - TODO: Implement pathing operations and perform an actual `isPointInPath()` operation
291
+ - always returns false
292
+ - function `isPointInStroke`
293
+ - throws `TypeError` if `arguments.length < 2`
294
+ - always returns false
295
+ - computed property `lineCap`
296
+ - sets the current `lineCap` stack value if value is a valid `LineCap`
297
+ - returns the current `lineCap` stack value
298
+ - computed property `lineDashOffset`
299
+ - sets the current `lineDashOffset` stack value if value is finite
300
+ - returns the current `lineDashOffset` stack value
301
+ - computed property `lineJoin`
302
+ - sets the current `lineJoin` stack value if value is a valid `LineJoin`
303
+ - returns the current `lineJoin` stack value
304
+ - function `lineTo`
305
+ - throws `TypeError` if `arguments.length < 2`
306
+ - computed property `lineWidth`
307
+ - sets the current `lineWidth` stack value if value is finite and greater than 0
308
+ - returns the current `lineWidth` stack value
309
+ - function `measureText`
310
+ - throws `TypeError` if `arguments.length < 1`
311
+ - returns a `TextMetrics` object
312
+ - computed property `miterLimit`
313
+ - sets the current `miterLimit` stack value if value is finite and greater than 0
314
+ - returns the current `miterLimit` stack value
315
+ - function `moveTo`
316
+ - throws `TypeError` if `arguments.length < 2`
317
+ - function `putImageData`
318
+ - Valid arities are: [3, 7], throws `TypeError` if `arguments.length` is not valid arity
319
+ - throws `TypeError` if `data` is not instanceof `ImageData`
320
+ - function `quadraticCurveTo`
321
+ - throws `TypeError` if `arguments.length < 4`
322
+ - function `rect`
323
+ - throws `TypeError` if `arguments.length < 4`
324
+ - function `removeHitRegion`
325
+ - throws `TypeError` if `arguments.length < 1`
326
+ - function `resetTransform`
327
+ - sets current `transform` stack value to the 2d identity matrix
328
+ - function `restore`
329
+ - pops all the property stack values and decreases the stack index
330
+ - function `rotate`
331
+ - throws `TypeError` if `arguments.length < 1`
332
+ - rotates the current `transform` stack value if `angle` is finite
333
+ - function `save`
334
+ - pushes the current property stack values to the next item on the stack
335
+ - increases the stack index
336
+ - function `scale`
337
+ - throws `TypeError` if `arguments.length < 2`
338
+ - scales the current `transform` stack value if the `x` and `y` values are finite
339
+ - function `scrollPathIntoView` (empty noOp stub)
340
+ - function `setTransform`
341
+ - if `arguments.length === 0` sets the current `transform` stack value to the 2d identity matrix
342
+ - if `arguments.length == 1`
343
+ - throws `TypeError` if value is not instanceof `DOMMatrix`
344
+ - sets the current `transform` stack value to the provided matrix
345
+ - throws `TypeError` if `arguments.length < 6`
346
+ - coerces each parameter to a number via `Number()`
347
+ - sets the current `transform` stack value if the provided values are all finite
348
+ - computed property `shadowBlur`
349
+ - sets the current `shadowBlur` stack value if value is finite and greater than 0
350
+ - returns the current `shadowBlur` stack value
351
+ - computed property `shadowColor`
352
+ - sets the current `shadowColor` stack value if value is a valid css color
353
+ - returns the current `shadowColor` stack value
354
+ - computed property `shadowOffsetX`
355
+ - sets the current `shadowOffsetX` stack value if value is finite
356
+ - returns the current `shadowOffsetX` stack value
357
+ - computed property `shadowOffsetY`
358
+ - sets the current `shadowOffsetY` stack value if value is finite
359
+ - returns the current `shadowOffsetY` stack value
360
+ - function `stroke`
361
+ - throws `TypeError` if `path` is not instanceof `Path2D`
362
+ - function `strokeRect`
363
+ - throws `TypeError` if `arguments.length < 4`
364
+ - computed property `strokeStyle`
365
+ - sets current `fillStyle` stack value if it's a valid css color, a `CanvasGradient` or a `CanvasPattern`
366
+ - returns current `fillStyle` stack value
367
+ - function `strokeText`
368
+ - throws `TypeError` if `arguments.length < 3`
369
+ - computed property `textAlign`
370
+ - sets the current `textAlign` stack value if value is a valid `TextAlign` value
371
+ - returns the current `textAlign` stack value
372
+ - computed property `textBaseline`
373
+ - sets the current `textBaseline` stack value if value is a valid `TextBaseline` value
374
+ - returns the current `textBaseline` stack value
375
+ - function `transform`
376
+ - throws `TypeError` if `arguments.length < 6`
377
+ - coerces each value to number via `Number()`
378
+ - performs a transform operation on the current `transform` stack value if every parameter is finite
379
+ - function `translate`
380
+ - throws `TypeError` if `arguments.length < 2`
381
+ - performs a translate operation on the current `transform` stack value if every parameter is finite
382
+
383
+ ### Other Changes By File
384
+
385
+ - src/CanvasGradient.js
386
+ - Added Class for `instanceof` checks
387
+ - bound functions:
388
+ - `addColorStop`
389
+ - function `addColorStop`
390
+ - throws `IndexSizeError` `DOMException` if resulting offset is not finite
391
+ - throws `SyntaxError` when color cannot be parsed
392
+ - src/CanvasPattern.js
393
+ - Added Class for `instanceof` checks
394
+ - bound functions:
395
+ - `setTransform`
396
+ - function `setTransform`
397
+ - throws `TypeError` if argument.length > 1 and parameter is not an object
398
+ - src/DOMMatrix.js
399
+ - Added minimal `DOMMatrix` implementation
400
+ - Added Class for `instanceof` checks
401
+ - function `constructor`
402
+ - constructs 3d matrix when parameter is ArrayLike and `length === 16`
403
+ - constructs 2d matrix parameter is ArrayLike and `length === 6`
404
+ - throws else if provided a first argument
405
+ - constructs an identity 2d matrix if no arguments are passed
406
+ - property `matrix.isIdentity`
407
+ - returns `true` if matrix is an identity matrix
408
+ - computed properties `a-f`
409
+ - returns and sets values according to the HTML Living Specification
410
+ - property `is2D`
411
+ - returns `true` if matrix was constructed as a 2d matrix
412
+ - TODO:
413
+ - Make `m11-m44` computed properties
414
+ - Perform `Number()` coercion in setters
415
+ - src/ImageBitmap.js
416
+ - Added helper Class for `instanceof` checks
417
+ - bound functions:
418
+ - `close`
419
+ - function `close`
420
+ - "closes" the bitmap and causes `drawImage` function calls to fail
421
+ - src/ImageData.js
422
+ - Added helper class for `instanceof` checks
423
+ - computed readonly `width`, `height` and `data` properties
424
+ - function `constructor`
425
+ - throws `TypeError` if width is not finite
426
+ - throws `TypeError` if width is `0`
427
+ - throws `TypeError` if height is not finite
428
+ - throws `TypeError` if height is `0`
429
+ - creates an empty `Uint8ClampedArray` of `width * height * 4` size
430
+ - src/Path2D.js
431
+ - Added helper class for `instanceof` checks
432
+ - Borrows path function definitions from `CanvasRenderingContext2D` for convenience
433
+ - `closePath` implemented from `CanvasRenderingContext2D`
434
+ - `moveTo` implemented from `CanvasRenderingContext2D`
435
+ - `lineTo` implemented from `CanvasRenderingContext2D`
436
+ - `bezierCurveTo` implemented from `CanvasRenderingContext2D`
437
+ - `quadraticCurveTo` implemented from `CanvasRenderingContext2D`
438
+ - `arc` implemented from `CanvasRenderingContext2D`
439
+ - `arcTo` implemented from `CanvasRenderingContext2D`
440
+ - `ellipse` implemented from `CanvasRenderingContext2D`
441
+ - `rect` implemented from `CanvasRenderingContext2D`
442
+ - function `addPath`
443
+ - throws `TypeError` if `arguments.length < 1`
444
+ - throws `TypeError` if provided `path` is not `instanceof` `Path2D`
445
+ - src/TextMetrics.js
446
+ - Added helper class for `instanceof` checks
447
+ - Implemented data properties
448
+ - `width`
449
+ - `actualBoundingBoxLeft`
450
+ - `actualBoundingBoxRight`
451
+ - `fontBoundingBoxAscent`
452
+ - `fontBoundingBoxDescent`
453
+ - `actualBoundingBoxAscent`
454
+ - `actualBoundingBoxDescent`
455
+ - `emHeightAscent`
456
+ - `emHeightDescent`
457
+ - `hangingBaseline`
458
+ - `alphabeticBaseline`
459
+ - `ideographicBaseline`
460
+ - function `constructor`
461
+ - This function cannot normally be constructed
462
+ - mocked to accept a `text` parameter
463
+ - sets `width` property to `text.length`