jest-canvas-mock 2.0.0-alpha.3 → 2.1.1

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