From a9649b2bb5059d6575fc103510e82d49bce975be Mon Sep 17 00:00:00 2001 From: chenshenhai Date: Mon, 13 Sep 2021 10:48:35 +0800 Subject: [PATCH] test: init testing for @idraw/core src/lib/context --- .../lib/__snapshots__/context.test.ts.snap | 108 ++++++++++++++++++ packages/board/__tests__/lib/context.test.ts | 33 ++++++ 2 files changed, 141 insertions(+) create mode 100644 packages/board/__tests__/lib/__snapshots__/context.test.ts.snap create mode 100644 packages/board/__tests__/lib/context.test.ts diff --git a/packages/board/__tests__/lib/__snapshots__/context.test.ts.snap b/packages/board/__tests__/lib/__snapshots__/context.test.ts.snap new file mode 100644 index 0000000..9e63f23 --- /dev/null +++ b/packages/board/__tests__/lib/__snapshots__/context.test.ts.snap @@ -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", + }, +] +`; diff --git a/packages/board/__tests__/lib/context.test.ts b/packages/board/__tests__/lib/context.test.ts new file mode 100644 index 0000000..4dc75bc --- /dev/null +++ b/packages/board/__tests__/lib/context.test.ts @@ -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(); + }); +}) +