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: test 支持 snapshot 对比 #1310

Merged
merged 1 commit into from
Dec 10, 2021
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
20 changes: 20 additions & 0 deletions jest-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// eslint-disable-next-line
const { toMatchImageSnapshot } = require('jest-image-snapshot');
// eslint-disable-next-line
const CanvasConverter = require('canvas-to-buffer');

expect.extend({
toMatchImageSnapshot(received) {
const converter = new CanvasConverter(received.canvas, {
image: { types: ['png'] },
});

const execPath = process.execPath;
// toMatchImageSnapshot 需要使用 node 环境
process.execPath = 'node';
const result = toMatchImageSnapshot.call(this, converter.toBuffer());
process.execPath = execPath;

return result;
},
});
11 changes: 3 additions & 8 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@ module.exports = {
testEnvironment: 'jest-electron/environment',
preset: 'ts-jest',
collectCoverage: false,
collectCoverageFrom: [
'packages/*/src/**/*.{ts,tsx,js}',
'!**/node_modules/**',
],
modulePathIgnorePatterns: ["packages/*/dist"],
collectCoverageFrom: ['packages/*/src/**/*.{ts,tsx,js}', '!**/node_modules/**'],
modulePathIgnorePatterns: ['packages/*/dist'],
testPathIgnorePatterns: [],
testRegex: '/test/.*\\.test\\.tsx?$',
transform: {
'\\.(less|css)$': 'jest-less-loader',
},
setupFilesAfterEnv: ['<rootDir>/jest-setup.js'],
};
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@
"@babel/preset-env": "^7.12.10",
"@babel/preset-typescript": "^7.12.7",
"@types/jest": "^26.0.20",
"@types/jest-image-snapshot": "^4.3.1",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"babel-jest": "^26.6.3",
"canvas-to-buffer": "^1.1.1",
"eslint": "^7.32.0",
"father": "^2.30.0",
"jest": "^26.6.3",
"jest-electron": "^0.1.12",
"jest-less-loader": "^0.1.1",
"jest-image-snapshot": "^4.5.1",
"lerna": "^3.22.1",
"pre-commit": "^1.2.2",
"ts-jest": "^26.4.4",
Expand All @@ -57,6 +59,7 @@
"test-live": "npm run test-watch packages",
"test-watch": "DEBUG_MODE=1 jest --watch",
"coverage": "jest --coverage",
"snapshot": "jest --updateSnapshot",
"lint": "eslint ./",
"lint-fix": "eslint --fix ./",
"publish": "npm run build && npm run test && lerna publish prerelease --dist-tag next",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 6 additions & 3 deletions packages/f2/test/canvas/graphic.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { jsx, Canvas, Component } from '../../src';
import { createContext } from '../util';
import { createContext, delay } from '../util';
const context = createContext();

function View() {
Expand Down Expand Up @@ -39,15 +39,18 @@ function View() {
}

describe('Canvas', () => {
it('图形绘制', () => {
it('图形绘制', async () => {
const ref = { current: null };
const { type, props } = (
const { props } = (
<Canvas context={context} pixelRatio={1}>
<View />
</Canvas>
);

const canvas = new Canvas(props);
canvas.render();

await delay(100);
expect(context).toMatchImageSnapshot();
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 7 additions & 13 deletions packages/f2/test/chart/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,9 @@ const data = [
describe('Chart', () => {
it('Chart render', async () => {
const chartRef = { current: null };
const { props } = (
const { type, props } = (
<Canvas context={context} pixelRatio={1}>
<Chart
ref={chartRef}
data={data}
coord={
{
// type: Polar,
// transposed: true,
}
}
scale={{}}
>
<Chart data={data} ref={chartRef}>
<Axis field="genre" />
<Axis field="sold" />
<Interval x="genre" y="sold" color="genre" />
Expand All @@ -45,7 +35,9 @@ describe('Chart', () => {
expect(chart.coord.left).toBeCloseTo(33.62);
expect(chart.coord.width).toBeCloseTo(251.38);

await delay(500);
await delay(1000);
expect(context).toMatchImageSnapshot();

canvas.update(
(
<Canvas context={context} pixelRatio={1}>
Expand All @@ -67,5 +59,7 @@ describe('Chart', () => {

expect(chart.coord.left).toBeCloseTo(83.62);
expect(chart.coord.width).toBeCloseTo(51.38);
await delay(1000);
expect(context).toMatchImageSnapshot();
});
});
21 changes: 11 additions & 10 deletions packages/f2/test/components/line/line.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,8 @@ describe('折线图', () => {
showCrosshairs
crosshairsType="xy"
crosshairsStyle={{
stroke: "#1577FE",
lineWidth: "1px",
stroke: '#1577FE',
lineWidth: '1px',
lineDash: [2, 2],
}}
custom
Expand Down Expand Up @@ -997,13 +997,14 @@ describe('折线图', () => {
}

didMount() {
// 更新数据
for (let i = 0; i <= 40; i++) {
setTimeout(() => {
const { data } = this.state;
this.setState({ data: [].concat(data, getRecord(i)) });
}, i * 30);
}
setTimeout(() => {
const { data } = this.state;
let newData = [].concat(data);
for (let i = 0; i <= 10; i++) {
newData.push(getRecord(i));
}
this.setState({ data: newData });
}, 20);
}

render() {
Expand Down Expand Up @@ -1045,7 +1046,7 @@ describe('折线图', () => {

expect(polyline.get('attrs').points.length).toBe(3);

await delay(200);
await delay(300);

const newPolyline = container
.get('children')[0]
Expand Down
2 changes: 1 addition & 1 deletion packages/f2/test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function delay(time) {
});
}

const createContext = (title = '', { height = '225px', width = '300px' }: any = {}) => {
const createContext = (title = '', { width = '300px', height = '225px' }: any = {}) => {
const canvasEl = document.createElement('canvas');
const titleEl = document.createElement('p');
titleEl.innerText = title + ':';
Expand Down
7 changes: 5 additions & 2 deletions packages/graphic/src/engine/canvas-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ class CanvasElement extends EventEmit {
super();
this.context = ctx;
// canvas实际的宽高 (width/height) * pixelRatio
this.width = 0;
this.height = 0;

// 有可能是 node canvas 创建的 context 对象
const canvas = ctx.canvas || {};
this.width = canvas.width || 0;
this.height = canvas.height || 0;
this.style = {};
this.currentStyle = {};
this.attrs = {};
Expand Down
12 changes: 3 additions & 9 deletions packages/graphic/src/engine/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,13 @@ class Canvas extends EventEmit {
}

if (context && canvas && !canvas.getContext) {
canvas.getContext = function () {
canvas.getContext = function() {
return context;
};
}
let width = this.get('width');
if (!width) {
width = getWidth(canvas);
}
const width = this.get('width') || getWidth(canvas) || canvas.width;

let height = this.get('height');
if (!height) {
height = getHeight(canvas);
}
const height = this.get('height') || getHeight(canvas) || canvas.height;

this.set('canvas', this);
this.set('el', canvas);
Expand Down