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