From 27277505ff022cc5dc6fa725eb31f46aba3e1915 Mon Sep 17 00:00:00 2001 From: hikerpig Date: Fri, 29 Dec 2023 23:33:25 +0800 Subject: [PATCH] fix: cli test --- packages/pintora-cli/package.json | 2 +- packages/pintora-cli/src/render.ts | 12 +++++++++++- .../pintora-renderer/src/renderers/CanvasRenderer.ts | 5 +++++ .../pintora-renderer/src/renderers/SvgRenderer.ts | 5 +++++ packages/pintora-renderer/src/renderers/base.ts | 5 +---- 5 files changed, 23 insertions(+), 6 deletions(-) diff --git a/packages/pintora-cli/package.json b/packages/pintora-cli/package.json index 44edae7a..0811d213 100644 --- a/packages/pintora-cli/package.json +++ b/packages/pintora-cli/package.json @@ -31,7 +31,7 @@ "scripts": { "compile": "tsc", "watch": "tsc -w", - "test": "jest" + "test": "jest ---forceExit" }, "bugs": { "url": "https://github.com/hikerpig/pintora/issues" diff --git a/packages/pintora-cli/src/render.ts b/packages/pintora-cli/src/render.ts index 891a69b0..f472106e 100644 --- a/packages/pintora-cli/src/render.ts +++ b/packages/pintora-cli/src/render.ts @@ -35,8 +35,18 @@ function renderPrepare(opts: CLIRenderOptions) { // setup the env for renderer global.window = dom.window as any global.document = document + + const noop = () => {} + Object.assign(global as any, { + navigator: {}, + addEventListener: noop, + removeEventListener: noop, + CanvasPattern, + requestAnimationFrame(cb: () => void) { + cb() + } + }) ;(dom.window as any).devicePixelRatio = devicePixelRatio - ;(global as any).CanvasPattern = CanvasPattern return { container, diff --git a/packages/pintora-renderer/src/renderers/CanvasRenderer.ts b/packages/pintora-renderer/src/renderers/CanvasRenderer.ts index cd76d617..c8ad5917 100644 --- a/packages/pintora-renderer/src/renderers/CanvasRenderer.ts +++ b/packages/pintora-renderer/src/renderers/CanvasRenderer.ts @@ -5,4 +5,9 @@ export class CanvasRenderer extends BaseRenderer { getGRenderer() { return new Renderer() } + + override getRootElement() { + if (!this.gcvs) return + return (this.gcvs.context.contextService as any).$canvas + } } diff --git a/packages/pintora-renderer/src/renderers/SvgRenderer.ts b/packages/pintora-renderer/src/renderers/SvgRenderer.ts index fdad2b5b..64e0c2a3 100644 --- a/packages/pintora-renderer/src/renderers/SvgRenderer.ts +++ b/packages/pintora-renderer/src/renderers/SvgRenderer.ts @@ -8,6 +8,11 @@ export class SvgRenderer extends BaseRenderer { return new Renderer() } + override getRootElement() { + if (!this.gcvs) return + return (this.gcvs.context.contextService as any).$namespace + } + onShapeAdd(shape: GElement, mark: Mark) { super.onShapeAdd(shape, mark) diff --git a/packages/pintora-renderer/src/renderers/base.ts b/packages/pintora-renderer/src/renderers/base.ts index 92699cbc..abf5bd6a 100644 --- a/packages/pintora-renderer/src/renderers/base.ts +++ b/packages/pintora-renderer/src/renderers/base.ts @@ -92,10 +92,7 @@ export abstract class BaseRenderer implements IRenderer { return this } - getRootElement() { - if (!this.gcvs) return - return this.gcvs.getConfig().el - } + abstract getRootElement(): Element protected addBgShape() { if (this.ir.bgColor && this.gcvs) {