Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(clip): add clippath #58

Merged
merged 2 commits into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![Coverage Status](https://coveralls.io/repos/github/hustcc/jest-canvas-mock/badge.svg?branch=master)](https://coveralls.io/github/hustcc/jest-canvas-mock)
[![npm](https://img.shields.io/npm/v/jest-canvas-mock.svg)](https://www.npmjs.com/package/jest-canvas-mock)
[![npm](https://img.shields.io/npm/dm/jest-canvas-mock.svg)](https://www.npmjs.com/package/jest-canvas-mock)

[![Mentioned in Awesome Jest](https://awesome.re/mentioned-badge.svg)](https://github.com/jest-community/awesome-jest)

## Install

Expand All @@ -16,7 +16,6 @@ This should only be installed as a development dependency (`devDependencies`) as
npm i --save-dev jest-canvas-mock
```


## Setup

In your `package.json` under the `jest`, create a `setupFiles` array and add `jest-canvas-mock` to the array.
Expand All @@ -41,7 +40,6 @@ If you already have a `setupFiles` attribute you can also append `jest-canvas-mo

More about in [configuration section](https://facebook.github.io/jest/docs/en/configuration.html#content).


## Setup file

Alternatively you can create a new setup file which then requires this module or
Expand Down Expand Up @@ -98,7 +96,7 @@ expect(() => ctx.fill(new Path2D(), "invalid!")).toThrow(TypeError);

We try to follow the ECMAScript specification as closely as possible.

# Snapshots
## Snapshots

There are multiple ways to validate canvas state. There are currently three `static` methods attached
to the `CanvasRenderingContext2D` class. The first way to use this feature is by using the `__getEvents`
Expand Down
55 changes: 55 additions & 0 deletions __tests__/classes/CanvasRenderingContext2D.__getClippingPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
let ctx;
beforeEach(() => {
// get a new context each test
ctx = document.createElement('canvas')
.getContext('2d');
});

afterEach(() => {
const drawCalls = ctx.__getClippingRegion();
expect(drawCalls).toMatchSnapshot();
});

describe('__getClippingRegion', () => {
it("should be empty when there are no path elements", () => {
ctx.clip();
});

it("should store the clipping region", () => {
ctx.rect(1, 2, 3, 4);
ctx.arc(1, 2, 3, 4, 5);
ctx.clip();
});

it("shouldn't store the whole clipping region twice when clip is called twice", () => {
ctx.rect(1, 2, 3, 4);
ctx.arc(1, 2, 3, 4, 5);
ctx.clip();
ctx.rect(1, 2, 3, 4);
ctx.arc(1, 2, 3, 4, 5);
ctx.clip();
});

it("should save the clipping region correctly when saved", () => {
ctx.rect(1, 2, 3, 4);
ctx.arc(1, 2, 3, 4, 5);
ctx.clip();
const region = ctx.__getClippingRegion();
ctx.save();
expect(region).toStrictEqual(ctx.__getClippingRegion());
});

it("should save the clipping region correctly when saved", () => {
ctx.rect(1, 2, 3, 4);
ctx.arc(1, 2, 3, 4, 5);
ctx.clip();
const region = ctx.__getClippingRegion();
ctx.save();
ctx.rect(1, 2, 3, 4);
ctx.arc(1, 2, 3, 4, 5);
ctx.clip();
expect(region).not.toStrictEqual(ctx.__getClippingRegion());
ctx.restore();
expect(region).toStrictEqual(ctx.__getClippingRegion());
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`__getClippingRegion should be empty when there are no path elements 1`] = `Array []`;

exports[`__getClippingRegion should save the clipping region correctly when saved 1`] = `
Array [
Object {
"props": Object {
"height": 4,
"width": 3,
"x": 1,
"y": 2,
},
"transform": Array [
1,
0,
0,
1,
0,
0,
],
"type": "rect",
},
Object {
"props": Object {
"anticlockwise": false,
"endAngle": 5,
"radius": 3,
"startAngle": 4,
"x": 1,
"y": 2,
},
"transform": Array [
1,
0,
0,
1,
0,
0,
],
"type": "arc",
},
]
`;

exports[`__getClippingRegion should save the clipping region correctly when saved 2`] = `
Array [
Object {
"props": Object {
"height": 4,
"width": 3,
"x": 1,
"y": 2,
},
"transform": Array [
1,
0,
0,
1,
0,
0,
],
"type": "rect",
},
Object {
"props": Object {
"anticlockwise": false,
"endAngle": 5,
"radius": 3,
"startAngle": 4,
"x": 1,
"y": 2,
},
"transform": Array [
1,
0,
0,
1,
0,
0,
],
"type": "arc",
},
]
`;

exports[`__getClippingRegion should store the clipping region 1`] = `
Array [
Object {
"props": Object {
"height": 4,
"width": 3,
"x": 1,
"y": 2,
},
"transform": Array [
1,
0,
0,
1,
0,
0,
],
"type": "rect",
},
Object {
"props": Object {
"anticlockwise": false,
"endAngle": 5,
"radius": 3,
"startAngle": 4,
"x": 1,
"y": 2,
},
"transform": Array [
1,
0,
0,
1,
0,
0,
],
"type": "arc",
},
]
`;

exports[`__getClippingRegion shouldn't store the whole clipping region twice when clip is called twice 1`] = `
Array [
Object {
"props": Object {
"height": 4,
"width": 3,
"x": 1,
"y": 2,
},
"transform": Array [
1,
0,
0,
1,
0,
0,
],
"type": "rect",
},
Object {
"props": Object {
"anticlockwise": false,
"endAngle": 5,
"radius": 3,
"startAngle": 4,
"x": 1,
"y": 2,
},
"transform": Array [
1,
0,
0,
1,
0,
0,
],
"type": "arc",
},
Object {
"props": Object {
"fillRule": "nonzero",
"path": Array [
Object {
"props": Object {},
"transform": Array [
1,
0,
0,
1,
0,
0,
],
"type": "beginPath",
},
Object {
"props": Object {
"height": 4,
"width": 3,
"x": 1,
"y": 2,
},
"transform": Array [
1,
0,
0,
1,
0,
0,
],
"type": "rect",
},
Object {
"props": Object {
"anticlockwise": false,
"endAngle": 5,
"radius": 3,
"startAngle": 4,
"x": 1,
"y": 2,
},
"transform": Array [
1,
0,
0,
1,
0,
0,
],
"type": "arc",
},
],
},
"transform": Array [
1,
0,
0,
1,
0,
0,
],
"type": "clip",
},
Object {
"props": Object {
"height": 4,
"width": 3,
"x": 1,
"y": 2,
},
"transform": Array [
1,
0,
0,
1,
0,
0,
],
"type": "rect",
},
Object {
"props": Object {
"anticlockwise": false,
"endAngle": 5,
"radius": 3,
"startAngle": 4,
"x": 1,
"y": 2,
},
"transform": Array [
1,
0,
0,
1,
0,
0,
],
"type": "arc",
},
]
`;
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
"parse-color": "^1.0.0"
},
"devDependencies": {
"@babel/cli": "^7.6.4",
"@babel/core": "^7.6.4",
"@babel/plugin-proposal-class-properties": "^7.5.5",
"@babel/preset-env": "^7.6.3",
"@babel/cli": "^7.7.0",
"@babel/core": "^7.7.2",
"@babel/plugin-proposal-class-properties": "^7.7.0",
"@babel/preset-env": "^7.7.1",
"@commitlint/cli": "^8.2.0",
"@commitlint/config-angular": "^8.2.0",
"babel-jest": "^24.9.0",
"babel-plugin-version": "^0.2.3",
"coveralls": "^3.0.7",
"husky": "^3.0.9",
"coveralls": "^3.0.8",
"husky": "^3.1.0",
"jest": "^24.9.0"
},
"commitlint": {
Expand Down
Loading