- feat: export a function to re-initialize mocks (#98)
- docs: fix some typos (#104)
- fix: copy & paste mistake: toBlob -> toDataURL (#101)
- chore: add Contributors LitoMore (#94)
- fix(window): avoid global.window redefinition (#91)
- feat: add translate, translateSelf, scale and scaleSelf to DOMMatrix (#83)
- optimize(Path2D): replace reassign-concat with for-push (#76)
- test: add test for vis @antv/g2plot (#79)
- fix(setLineDash): rename parameter value to segments (#74)
- fix(clip): delete clipping region with restore (#73)
- Added Prettier code style
- Deleted .npmignore and switched to
package.json
files field - Added CONTRIBUTING and CODE_OF_CONDUCT docs
- Switched to
moo-color
for color parsing - Added contributors to markdown document
- New Contributor @evanoc0
- Bug: Slice canvas transform value when pushing (#50)
- Remove support for node 6 and 7
- switch babel env to target node 8
- add support for node 13
- update package versions
- fix lineWidth bug
- Feature: Support for ImageData instantiation using a source array (#45)
This minor version bump now has some major snapshot support but is backwards compatible.
Changes:
- Feature: Add
static
CanvasRenderingContext2D
method:#.__getEvents(ctx)
- Feature: Every successful modification of the
CanvasRenderingContext2D
state machine logs an_event
- Feature: Every successful modification of the
- Feature: Add
static
CanvasRenderingContext2D
method:#.__getPath(ctx)
- Feature: Every path call adds a
_path
item and can be accessed via__getPath(ctx)
- Feature:
beginPath()
empties the_path
- Feature: Every path call adds a
- Feature: Add
static
CanvasRenderingContext2D
method:#.__getDrawCalls(ctx)
- Feature: Every draw call adds a
_drawCall
item and can be accessed via__getDrawCall(ctx)
- Feature: Every draw call adds a
- Feature: Add
types/index.d.ts
file for tooling types (in jest environment) - Feature: Support node 12
- Docs
- Updated arc example
- Added snapshot testing documentation
- Bug:
createLinearGradient
now accepts strings - Bug:
createRadialGradient
now accepts strings - Bug:
globalAlpha
now acceptsnull
perNumber
coercion - Feature: Faster finite values checks
- Feature: Add
_path
and_events
toPath2D
- Testing: Add and test snapshot outputs
Just publish a stable version.
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.
Now the following expectation works as expected.
const ctx = document.createElement('canvas').getContext('2d');
expect(ctx).toBeInstanceOf(CanvasRenderingContext2D);
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.
The following example demonstrates that canvas methods can be called, and parameters are verified.
const PI_2 = Math.PI * 2;
// create a circle at 0,0 with radius 100
ctx.arc(0, 0, 100, 0, PI_2);
expect(ctx.arc).toBeCalledWith(0, 0, 100, 0, PI_2);
// negative radius values throw DOMException errors
expect(() => ctx.arc(0, 0, -10, 0, PI_2)).toThrow(DOMExpection);
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.
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.
// create a context
const ctx = document.createElement('canvas').getContext('2d');
// save the current state of the canvas
ctx.save();
// set some color and font values
ctx.fillStyle = 'blue';
ctx.font = '12pt Times New Roman';
// now the fillStyle property parses the color
expect(ctx.fillStyle).toBe('#00F');
// font parsing also works as intended
expect(ctx.font).toBe('16px "Times New Roman"');
// restore the previously saved state
ctx.restore():
// the fillStyle was restored to default
expect(ctx.fillStyle).toBe('#000');
// the font was restored too
expect(ctx.font).toBe('10px sans-serif');
For all these reasons, the jest-canvas-mock
package was bumped a major version to 2.0.0
.
- Implemented Stack Properties for the following items:
direction
fillStyle
filter
font
globalAlpha
globalCompositeOperation
imageSmoothingEnabled
imageSmoothingQuality
lineCap
lineDashOffset
lineDash
(viagetLineDash()
/setLineDash()
)lineJoin
lineWidth
miterLimit
shadowBlur
shadowColor
shadowOffsetX
shadowOffsetY
stack
strokeStyle
textAlign
textBaseline
transform
(viasetTransform
etc.)
- function
constructor
binds the following functionssetLineDash
getLineDash
setTransform
getTransform
getImageData
save
restore
createPattern
createRadialGradient
addHitRegion
arc
arcTo
beginPath
clip
closePath
scale
stroke
clearHitRegions
clearRect
fillRect
strokeRect
rect
resetTransform
translate
moveTo
lineTo
bezierCurveTo
createLinearGradient
ellipse
measureText
rotate
drawImage
drawFocusIfNeeded
isPointInPath
isPointInStroke
putImageData
strokeText
fillText
quadraticCurveTo
removeHitRegion
fill
transform
scrollPathIntoView
createImageData
- function
addHitRegion
- verifies
path
orid
parameter is set, throwsDOMException
otherwise - verifies
fillRule
if set, throwsTypeError
if invalid
- verifies
- function
arc
- throws
TypeError
ifarguments.length < 5
- throws
DOMException(IndexSizeError)
if values are finite, but radius is negative
- throws
- function
arcTo
- throws
TypeError
ifarguments.length < 5
- throws
DOMException(IndexSizeError)
if values are finite, but radius is negative
- throws
- function
beginPath
(empty noOp stub)- if path length eventually needs to be verified, this can be changed
- function
bezierCurveTo
- throws
TypeError
ifarguments.length < 6
- throws
- readonly property
canvas
- returns the parent
HTMLCanvasElement
- returns the parent
- function
clearHitRegions
(empty noOp stub) - function
clearRect
- throws
TypeError
ifarguments.length < 4
- throws
- function
clip
- if
fillRule
is provided, throwsTypeErorr
ifFillRule
is invalid - if
path
is provided, throwsTypeError
if path is notinstanceof
Path2D
- if
- function
closePath
(added noOp stub) - function
createImageData
- throws
TypeError
ifarguments.length === 1
and parameter is notinstanceof
ImageData
- throws
TypeError
ifarguments.length >= 2
andwidth
is not finite - throws
TypeError
ifarguments.length >= 2
andheight
is not finite - returns
ImageData
- throws
- function
createLinearGradient
- throws
TypeError
ifarguments.length < 4
- throws
TypeError
ifx0
is not finite - throws
TypeError
ify0
is not finite - throws
TypeError
ifx1
is not finite - throws
TypeError
ify1
is not finite - returns
CanvasGradient
- throws
- function
createPattern
- throws
TypeError
if image is not supported injest-canvas-mock
- throws
TypeError
ifarguments.length < 4
- throws
DOMException('SyntaxError')
if image is detached - returns
CanvasPattern
if image isHTMLImageElement
,ImageBitmap
,HTMLVideoElement
,HTMLCanvasElement
- throws
- function
createRadialGradient
- throws
TypeError
ifarguments.length < 4
- throws
TypeError
ifx0
is not finite - throws
TypeError
ify0
is not finite - throws
TypeError
ifr0
is not finite - throws
TypeError
ifx1
is not finite - throws
TypeError
ify1
is not finite - throws
TypeError
ifr1
is not finite - throws
DOMException('DataError')
ifr0
is negative - throws
DOMException('DataError')
ifr1
is negative
- throws
- computed property
currentTransform
- sets transform stack value if
value instanceof DOMMatrix
- returns new
DOMMatrix
with current transform values
- sets transform stack value if
- computed property
direction
- sets direction stack value when valid
- returns current direction stack value
- function
drawFocusIfNeeded
- throws
TypeError
ifarguments.length === 0
- throws
TypeError
ifarguments.length === 2
andpath
is not instanceofPath2D
- throws
TypeError
ifelement
is not instanceofElement
- throws
- function drawImage
- Valid arities are: [3, 5, 9] (throws
TypeError
ifarguments.length
is not valid) - throws
TypeError
if image is not supported byjest-canvas-mock
- Valid arities are: [3, 5, 9] (throws
- function
ellipse
- throws
TypeError
ifarguments.length < 7
- throws
DOMException('IndexSizeError')
ifradiusX
is negative and all parameters are finite - throws
DOMException('IndexSizeError')
ifradiusY
is negative and all parameters are finite
- throws
- function
fill
- throws
TypeError
iffillRule
is not validFillRule
- throws
TypeError
ifpath
is not instanceofPath2D
- throws
- function
fillRect
- throws
TypeError
ifarguments.length < 4
- throws
- computed property
fillStyle
- sets current
fillStyle
stack value if it's a valid css color, aCanvasGradient
or aCanvasPattern
- returns current
fillStyle
stack value
- sets current
- function
fillText
- throws
TypeError
ifarguments.length < 3
- throws
- computed property
filter
- sets the current
filter
stack value if it's a string - returns the current
filter
stack value - TODO: add custom parser for filter values to check validity
- sets the current
- computed property
font
- sets the current
font
stack value if it's a valid font - returns the current
font
stack value
- sets the current
- function
getImageData
- returns new
ImageData
with the same size as the parentcanvas
- returns new
- function
getLineDash
- returns the current
lineDash
stack value
- returns the current
- function
setLineDash
- throws
TypeError
iflineDash
value is not a valid sequence - sets the current
lineDash
stack value - properly concatenates itself if the
lineDash
length is odd
- throws
- function
getTransform
- returns current
transform
stack value in the form of aDOMMatrix
- returns current
- computed property
globalAlpha
- sets current
globalAlpha
stack value ifvalue
finite and between the inclusive range[0 .. 1]
- returns the current
globalAlpha
stack value
- sets current
- computed property
globalCompositeOperation
- sets the current
globalCompositeOperation
stack value ifvalue
is a valid GlobalCompositeOperation value - returns the current
globalCompositeOperation
stack value
- sets the current
- computed property
imageSmoothingEnabled
- sets current
imageSmoothingEnabled
stack value tovalue
coerced to a boolean - returns current
imageSmoothingEnabled
stack value
- sets current
- computed property
imageSmoothingQuality
- sets the
imageSmoothingQuality
stack value if value is a valid ImageSmoothingQuality value - returns the current
imageSmoothingQuality
stack value
- sets the
- function
isPointInPath
- throws
TypeError
ifarguments.length < 2
- throws
TypeError
if providedfillRule
is not a validFillRule
- TODO: Implement pathing operations and perform an actual
isPointInPath()
operation - always returns false
- throws
- function
isPointInStroke
- throws
TypeError
ifarguments.length < 2
- always returns false
- throws
- computed property
lineCap
- sets the current
lineCap
stack value if value is a validLineCap
- returns the current
lineCap
stack value
- sets the current
- computed property
lineDashOffset
- sets the current
lineDashOffset
stack value if value is finite - returns the current
lineDashOffset
stack value
- sets the current
- computed property
lineJoin
- sets the current
lineJoin
stack value if value is a validLineJoin
- returns the current
lineJoin
stack value
- sets the current
- function
lineTo
- throws
TypeError
ifarguments.length < 2
- throws
- computed property
lineWidth
- sets the current
lineWidth
stack value if value is finite and greater than 0 - returns the current
lineWidth
stack value
- sets the current
- function
measureText
- throws
TypeError
ifarguments.length < 1
- returns a
TextMetrics
object
- throws
- computed property
miterLimit
- sets the current
miterLimit
stack value if value is finite and greater than 0 - returns the current
miterLimit
stack value
- sets the current
- function
moveTo
- throws
TypeError
ifarguments.length < 2
- throws
- function
putImageData
- Valid arities are: [3, 7], throws
TypeError
ifarguments.length
is not valid arity - throws
TypeError
ifdata
is not instanceofImageData
- Valid arities are: [3, 7], throws
- function
quadraticCurveTo
- throws
TypeError
ifarguments.length < 4
- throws
- function
rect
- throws
TypeError
ifarguments.length < 4
- throws
- function
removeHitRegion
- throws
TypeError
ifarguments.length < 1
- throws
- function
resetTransform
- sets current
transform
stack value to the 2d identity matrix
- sets current
- function
restore
- pops all the property stack values and decreases the stack index
- function
rotate
- throws
TypeError
ifarguments.length < 1
- rotates the current
transform
stack value ifangle
is finite
- throws
- function
save
- pushes the current property stack values to the next item on the stack
- increases the stack index
- function
scale
- throws
TypeError
ifarguments.length < 2
- scales the current
transform
stack value if thex
andy
values are finite
- throws
- function
scrollPathIntoView
(empty noOp stub) - function
setTransform
- if
arguments.length === 0
sets the currenttransform
stack value to the 2d identity matrix - if
arguments.length == 1
- throws
TypeError
if value is not instanceofDOMMatrix
- sets the current
transform
stack value to the provided matrix
- throws
- throws
TypeError
ifarguments.length < 6
- coerces each parameter to a number via
Number()
- sets the current
transform
stack value if the provided values are all finite
- if
- computed property
shadowBlur
- sets the current
shadowBlur
stack value if value is finite and greater than 0 - returns the current
shadowBlur
stack value
- sets the current
- computed property
shadowColor
- sets the current
shadowColor
stack value if value is a valid css color - returns the current
shadowColor
stack value
- sets the current
- computed property
shadowOffsetX
- sets the current
shadowOffsetX
stack value if value is finite - returns the current
shadowOffsetX
stack value
- sets the current
- computed property
shadowOffsetY
- sets the current
shadowOffsetY
stack value if value is finite - returns the current
shadowOffsetY
stack value
- sets the current
- function
stroke
- throws
TypeError
ifpath
is not instanceofPath2D
- throws
- function
strokeRect
- throws
TypeError
ifarguments.length < 4
- throws
- computed property
strokeStyle
- sets current
fillStyle
stack value if it's a valid css color, aCanvasGradient
or aCanvasPattern
- returns current
fillStyle
stack value
- sets current
- function
strokeText
- throws
TypeError
ifarguments.length < 3
- throws
- computed property
textAlign
- sets the current
textAlign
stack value if value is a validTextAlign
value - returns the current
textAlign
stack value
- sets the current
- computed property
textBaseline
- sets the current
textBaseline
stack value if value is a validTextBaseline
value - returns the current
textBaseline
stack value
- sets the current
- function
transform
- throws
TypeError
ifarguments.length < 6
- coerces each value to number via
Number()
- performs a transform operation on the current
transform
stack value if every parameter is finite
- throws
- function
translate
- throws
TypeError
ifarguments.length < 2
- performs a translate operation on the current
transform
stack value if every parameter is finite
- throws
- src/CanvasGradient.js
- Added Class for
instanceof
checks - bound functions:
addColorStop
- function
addColorStop
- throws
IndexSizeError
DOMException
if resulting offset is not finite - throws
SyntaxError
when color cannot be parsed
- throws
- Added Class for
- src/CanvasPattern.js
- Added Class for
instanceof
checks - bound functions:
setTransform
- function
setTransform
- throws
TypeError
if argument.length > 1 and parameter is not an object
- throws
- Added Class for
- src/DOMMatrix.js
- Added minimal
DOMMatrix
implementation - Added Class for
instanceof
checks - function
constructor
- constructs 3d matrix when parameter is ArrayLike and
length === 16
- constructs 2d matrix parameter is ArrayLike and
length === 6
- throws else if provided a first argument
- constructs an identity 2d matrix if no arguments are passed
- constructs 3d matrix when parameter is ArrayLike and
- property
matrix.isIdentity
- returns
true
if matrix is an identity matrix
- returns
- computed properties
a-f
- returns and sets values according to the HTML Living Specification
- property
is2D
- returns
true
if matrix was constructed as a 2d matrix
- returns
- TODO:
- Make
m11-m44
computed properties - Perform
Number()
coercion in setters
- Make
- Added minimal
- src/ImageBitmap.js
- Added helper Class for
instanceof
checks - bound functions:
close
- function
close
- "closes" the bitmap and causes
drawImage
function calls to fail
- "closes" the bitmap and causes
- Added helper Class for
- src/ImageData.js
- Added helper class for
instanceof
checks - computed readonly
width
,height
anddata
properties - function
constructor
- throws
TypeError
if width is not finite - throws
TypeError
if width is0
- throws
TypeError
if height is not finite - throws
TypeError
if height is0
- creates an empty
Uint8ClampedArray
ofwidth * height * 4
size
- throws
- Added helper class for
- src/Path2D.js
- Added helper class for
instanceof
checks - Borrows path function definitions from
CanvasRenderingContext2D
for convenienceclosePath
implemented fromCanvasRenderingContext2D
moveTo
implemented fromCanvasRenderingContext2D
lineTo
implemented fromCanvasRenderingContext2D
bezierCurveTo
implemented fromCanvasRenderingContext2D
quadraticCurveTo
implemented fromCanvasRenderingContext2D
arc
implemented fromCanvasRenderingContext2D
arcTo
implemented fromCanvasRenderingContext2D
ellipse
implemented fromCanvasRenderingContext2D
rect
implemented fromCanvasRenderingContext2D
- function
addPath
- throws
TypeError
ifarguments.length < 1
- throws
TypeError
if providedpath
is notinstanceof
Path2D
- throws
- Added helper class for
- src/TextMetrics.js
- Added helper class for
instanceof
checks - Implemented data properties
width
actualBoundingBoxLeft
actualBoundingBoxRight
fontBoundingBoxAscent
fontBoundingBoxDescent
actualBoundingBoxAscent
actualBoundingBoxDescent
emHeightAscent
emHeightDescent
hangingBaseline
alphabeticBaseline
ideographicBaseline
- function
constructor
- This function cannot normally be constructed
- mocked to accept a
text
parameter - sets
width
property totext.length
- Added helper class for