From bb5254fea56ec69b1cd5accb8654ecee4ae4d3c4 Mon Sep 17 00:00:00 2001 From: AliceLanniste <1399789151@qq.com> Date: Sun, 2 Jun 2024 17:20:34 +0800 Subject: [PATCH] =?UTF-8?q?feat(tests):=20=E5=A2=9E=E5=8A=A0=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E6=B5=8B=E8=AF=95=20(#402)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(tests): 增加插件测试 * feat(test): 完成DringPlugin测试 * feat(test): 修改type定义 * feat(test): 修改vitest配置 * feat(test): 修改DringPlugin.spec.ts测试单例 --- .../core/__tests__/plugin/DringPlugin.spec.ts | 52 +++++++++++++++ packages/core/__tests__/utils/common.ts | 63 +++++++++++++++++++ packages/core/__tests__/utils/setup.ts | 15 +++++ packages/core/plugin/DringPlugin.ts | 16 ++--- packages/core/vitest.config.ts | 5 ++ 5 files changed, 144 insertions(+), 7 deletions(-) create mode 100644 packages/core/__tests__/plugin/DringPlugin.spec.ts create mode 100644 packages/core/__tests__/utils/common.ts diff --git a/packages/core/__tests__/plugin/DringPlugin.spec.ts b/packages/core/__tests__/plugin/DringPlugin.spec.ts new file mode 100644 index 00000000..2d4611d6 --- /dev/null +++ b/packages/core/__tests__/plugin/DringPlugin.spec.ts @@ -0,0 +1,52 @@ +import { beforeEach, expect, test } from 'vitest'; +import { initPlugin } from '../utils/setup.ts'; +import { DringPlugin } from '../../plugin/DringPlugin.ts'; +import { wait, mouseDown, drag } from '../utils/common.ts'; +import { describe } from 'node:test'; + +describe('canvas:drag', async () => { + const { pluginInstance, cleanUp } = initPlugin(DringPlugin); + + beforeEach(() => { + return () => { + cleanUp(); + (pluginInstance as DringPlugin).destroy(); + }; + }); + test('dragMode is true', async () => { + const instance = pluginInstance as DringPlugin; + instance.dragMode = true; + const testCanvas = instance.canvas as ExtCanvas; + testCanvas.lastPosX = 0; + testCanvas.lastPosY = 0; + mouseDown(testCanvas, { x: 50, y: 50 }); + await wait(); + expect([testCanvas.lastPosX, testCanvas.lastPosY]).toEqual([50, 50]); + expect(testCanvas.isDragging).toEqual(true); + expect(testCanvas.selection).toEqual(false); + }); + + test('dragMode is false', async () => { + const instance = pluginInstance as DringPlugin; + instance.dragMode = false; + const testCanvas = instance.canvas as ExtCanvas; + testCanvas.lastPosX = 0; + testCanvas.lastPosY = 0; + mouseDown(testCanvas, { x: 50, y: 50 }); + await wait(); + expect([testCanvas.lastPosX, testCanvas.lastPosY]).toEqual([0, 0]); + }); + + test('canvas drag', async () => { + const instance = pluginInstance as DringPlugin; + instance.dragMode = false; + const testCanvas = pluginInstance.canvas as ExtCanvas; + testCanvas.lastPosX = 0; + testCanvas.lastPosY = 0; + drag(testCanvas, { x: 50, y: 50 }, { x: 100, y: 100 }); + await wait(); + expect([testCanvas.lastPosX, testCanvas.lastPosY]).toEqual([100, 100]); + expect(testCanvas.isDragging).toEqual(false); + expect(testCanvas.selection).toEqual(true); + }); +}); diff --git a/packages/core/__tests__/utils/common.ts b/packages/core/__tests__/utils/common.ts new file mode 100644 index 00000000..87eb0ab7 --- /dev/null +++ b/packages/core/__tests__/utils/common.ts @@ -0,0 +1,63 @@ +export function wait(time = 0) { + return new Promise((resolve) => { + requestAnimationFrame(() => { + setTimeout(resolve, time); + }); + }); +} + +/** + * simulate mouseDown event + * @param target + * @param position position relative to the target + */ +export function mouseDown(target: ExtCanvas, position: { x: number; y: number }) { + const clientX = target.lastPosX + position.x; + const clientY = target.lastPosY + position.y; + target.fire('mouse:down', { e: { clientX: clientX, clientY: clientY } }); +} + +/** + * simulate mouseMove event + * @param target + * @param position + */ + +export function mouseMove(target: ExtCanvas, position: { x: number; y: number }) { + const clientX = position.x; + const clientY = position.y; + target.fire('mouse:move', { e: { clientX: clientX, clientY: clientY } }); +} + +/** + * simulate mouseUp event + * @param target + */ + +export function mouseUp(target: ExtCanvas) { + target.fire('mouse:up'); +} + +export function drag( + target: ExtCanvas, + start: { x: number; y: number }, + end: { x: number; y: number }, + step = 5 +) { + mouseDown(target, start); + mouseMove(target, start); + if (step !== 0) { + const xStep = (end.x - start.x) / step; + const yStep = (end.y - start.y) / step; + + for (const [i] of Array.from({ length: step }).entries()) { + mouseMove(target, { + x: xStep * (i + 1), + y: yStep * (i + 1), + }); + } + } + + mouseMove(target, end); + mouseUp(target); +} diff --git a/packages/core/__tests__/utils/setup.ts b/packages/core/__tests__/utils/setup.ts index e6a1e74e..b933b5d2 100644 --- a/packages/core/__tests__/utils/setup.ts +++ b/packages/core/__tests__/utils/setup.ts @@ -19,3 +19,18 @@ export function createEditor() { cleanUp: editor.destory(), }; } + +export function initPlugin(plugin: any) { + const editor = new Editor(); + const canvasElement = document.createElement('canvas'); + canvasElement.id = 'canvas'; + const canvas = new fabric.Canvas('canvas', {}); + const pluginInstance = new plugin(canvas, editor); + + return { + pluginInstance, + cleanUp: () => { + editor.destory(); + }, + }; +} diff --git a/packages/core/plugin/DringPlugin.ts b/packages/core/plugin/DringPlugin.ts index 5864b1cb..ef8b7a1e 100644 --- a/packages/core/plugin/DringPlugin.ts +++ b/packages/core/plugin/DringPlugin.ts @@ -9,13 +9,7 @@ import Editor from '../Editor'; type IEditor = Editor; -declare type ExtCanvas = fabric.Canvas & { - isDragging: boolean; - lastPosX: number; - lastPosY: number; -}; - -class DringPlugin { +export class DringPlugin { public canvas: fabric.Canvas; public editor: IEditor; public defautOption = {}; @@ -122,4 +116,12 @@ class DringPlugin { } } +declare global { + export type ExtCanvas = fabric.Canvas & { + isDragging: boolean; + lastPosX: number; + lastPosY: number; + }; +} + export default DringPlugin; diff --git a/packages/core/vitest.config.ts b/packages/core/vitest.config.ts index b94fc225..7324fc6b 100644 --- a/packages/core/vitest.config.ts +++ b/packages/core/vitest.config.ts @@ -15,6 +15,11 @@ export default defineConfig((_configEnv) => deps: { interopDefault: true, }, + poolOptions: { + threads: { + singleThread: true, + }, + }, environment: 'jsdom', }, })