test: init testing for @idraw/core src/lib/context

This commit is contained in:
chenshenhai 2021-09-13 10:48:35 +08:00
parent 0963d1c4c0
commit a9649b2bb5
2 changed files with 141 additions and 0 deletions

View file

@ -0,0 +1,108 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`@idraw/board: src/lib/context Context 1`] = `
Array [
Object {
"props": Object {
"height": 1600,
"width": 2400,
"x": 0,
"y": 0,
},
"transform": Array [
1,
0,
0,
1,
0,
0,
],
"type": "clearRect",
},
Object {
"props": Object {
"height": 1600,
"width": 2400,
"x": 0,
"y": 0,
},
"transform": Array [
1,
0,
0,
1,
0,
0,
],
"type": "fillRect",
},
Object {
"props": Object {
"height": 480,
"width": 800,
"x": 40,
"y": 40,
},
"transform": Array [
1,
0,
0,
1,
0,
0,
],
"type": "fillRect",
},
Object {
"props": Object {
"height": 480,
"width": 800,
"x": 320,
"y": 320,
},
"transform": Array [
1,
0,
0,
1,
0,
0,
],
"type": "fillRect",
},
Object {
"props": Object {
"height": 480,
"width": 800,
"x": 640,
"y": 640,
},
"transform": Array [
1,
0,
0,
1,
0,
0,
],
"type": "fillRect",
},
Object {
"props": Object {
"height": 400,
"width": 800,
"x": 1560,
"y": 1160,
},
"transform": Array [
1,
0,
0,
1,
0,
0,
],
"type": "fillRect",
},
]
`;

View file

@ -0,0 +1,33 @@
import Context from './../../src/lib/context';
import { getData } from './../data';
describe('@idraw/board: src/lib/context', () => {
test('Context', async () => {
const opts = {
width: 600,
height: 400,
contextWidth: 1000,
contextHeight: 900,
devicePixelRatio: 4
}
const canvas = document.createElement('canvas');
canvas.width = opts.contextWidth;
canvas.height = opts.contextHeight;
const ctx2d: CanvasRenderingContext2D = canvas.getContext('2d') as CanvasRenderingContext2D;
const ctx = new Context(ctx2d, opts);
const data = getData();
ctx.clearRect(0, 0, opts.width, opts.height);
ctx.setFillStyle('#ffffff');
ctx.fillRect(0, 0, opts.width, opts.height);
data.elements.forEach(ele => {
ctx.setFillStyle(ele.desc.color);
ctx.fillRect(ele.x, ele.y, ele.w, ele.h);
});
// @ts-ignore;
const calls = ctx2d.__getDrawCalls();
expect(calls).toMatchSnapshot();
});
})