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