|
| 1 | +import { Page, TestInfo, expect, test } from '@playwright/test'; |
| 2 | +import { Canvas } from 'canvas'; |
| 3 | +import * as fabric from 'fabric/node'; |
| 4 | +import { createWriteStream, writeFileSync } from 'fs'; |
| 5 | +import { ensureDirSync } from 'fs-extra'; |
| 6 | +import path from 'path'; |
| 7 | + |
| 8 | +import '../../setup/setupCoverage'; |
| 9 | + |
| 10 | +test.describe('Exporting node canvas', () => { |
| 11 | + const size = { |
| 12 | + width: 460, |
| 13 | + height: 450, |
| 14 | + }; |
| 15 | + |
| 16 | + const createCanvas = () => { |
| 17 | + const canvas = new fabric.StaticCanvas(null, { |
| 18 | + width: 1000, |
| 19 | + height: 1000, |
| 20 | + enableRetinaScaling: false, |
| 21 | + }); |
| 22 | + const rect = new fabric.Rect({ width: 50, height: 50, fill: 'red' }); |
| 23 | + const path = fabric.util.getSmoothPathFromPoints([ |
| 24 | + new fabric.Point(50, 50), |
| 25 | + new fabric.Point(100, 100), |
| 26 | + new fabric.Point(50, 200), |
| 27 | + new fabric.Point(400, 150), |
| 28 | + new fabric.Point(500, 500), |
| 29 | + ]); |
| 30 | + const text = new fabric.Text(new Array(9).fill('fabric.js').join(' '), { |
| 31 | + fill: 'blue', |
| 32 | + fontSize: 24, |
| 33 | + path: new fabric.Path(path), |
| 34 | + }); |
| 35 | + canvas.add(rect, text); |
| 36 | + return canvas; |
| 37 | + }; |
| 38 | + |
| 39 | + const createCtxForExport = (type: 'pdf' | 'svg') => { |
| 40 | + const ctx = new Canvas(0, 0, type).getContext('2d'); |
| 41 | + ctx.textDrawingMode = 'glyph'; |
| 42 | + return ctx; |
| 43 | + }; |
| 44 | + |
| 45 | + test.describe('SVG', () => { |
| 46 | + const attachSVG = async (result: Canvas, testInfo: TestInfo) => { |
| 47 | + ensureDirSync(testInfo.outputDir); |
| 48 | + const pathTo = path.resolve(testInfo.outputDir, 'output.svg'); |
| 49 | + writeFileSync(pathTo, result.toBuffer()); |
| 50 | + await testInfo.attach('output', { |
| 51 | + path: pathTo, |
| 52 | + }); |
| 53 | + }; |
| 54 | + |
| 55 | + const testSVG = async (page: Page, result: Canvas) => { |
| 56 | + await page.goto( |
| 57 | + `data:image/svg+xml,${encodeURIComponent(result.toBuffer().toString()) |
| 58 | + .replace(/'/g, '%27') |
| 59 | + .replace(/"/g, '%22')}`, |
| 60 | + { waitUntil: 'load' } |
| 61 | + ); |
| 62 | + expect( |
| 63 | + await page.screenshot({ |
| 64 | + clip: { x: 0, y: 0, width: 460, height: 450 }, |
| 65 | + }) |
| 66 | + ).toMatchSnapshot(); |
| 67 | + }; |
| 68 | + |
| 69 | + test('canvas', async ({ page }, testInfo) => { |
| 70 | + const ctx = createCtxForExport('svg'); |
| 71 | + const result = createCanvas().toCanvasElement(1, size, ctx); |
| 72 | + expect(result).toMatchObject(size); |
| 73 | + await attachSVG(result, testInfo); |
| 74 | + await testSVG(page, result); |
| 75 | + }); |
| 76 | + |
| 77 | + test('object', async ({ page }, testInfo) => { |
| 78 | + const ctx = createCtxForExport('svg'); |
| 79 | + const result = createCanvas() |
| 80 | + .item(1) |
| 81 | + .toCanvasElement({ ...size, ctx }); |
| 82 | + expect(result).toMatchObject(size); |
| 83 | + await attachSVG(result, testInfo); |
| 84 | + await testSVG(page, result); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + test.describe('PDF', () => { |
| 89 | + const attachPDF = async (result: Canvas, testInfo: TestInfo) => { |
| 90 | + ensureDirSync(testInfo.outputDir); |
| 91 | + const pathTo = path.resolve(testInfo.outputDir, 'output.pdf'); |
| 92 | + const out = createWriteStream(pathTo); |
| 93 | + await new Promise((resolve) => { |
| 94 | + out.on('finish', resolve); |
| 95 | + result.createPDFStream().pipe(out); |
| 96 | + }); |
| 97 | + await testInfo.attach('output', { |
| 98 | + path: pathTo, |
| 99 | + contentType: 'application/pdf', |
| 100 | + }); |
| 101 | + }; |
| 102 | + |
| 103 | + const testPDF = async (page: Page, result: Canvas) => { |
| 104 | + await page.goto( |
| 105 | + `data:application/pdf;base64,${Buffer.from(result.toBuffer()).toString( |
| 106 | + 'base64' |
| 107 | + )}`, |
| 108 | + { waitUntil: 'load' } |
| 109 | + ); |
| 110 | + await page.waitForTimeout(5000); |
| 111 | + expect( |
| 112 | + await page.screenshot({ |
| 113 | + clip: { x: 80, y: 25, width: 500, height: 500 }, |
| 114 | + }) |
| 115 | + ).toMatchSnapshot(); |
| 116 | + }; |
| 117 | + |
| 118 | + test('canvas', async ({ page }, testInfo) => { |
| 119 | + const ctx = createCtxForExport('pdf'); |
| 120 | + const result = createCanvas().toCanvasElement(1, size, ctx); |
| 121 | + expect(result).toMatchObject(size); |
| 122 | + await attachPDF(result, testInfo); |
| 123 | + await testPDF(page, result); |
| 124 | + }); |
| 125 | + |
| 126 | + test('object', async ({ page }, testInfo) => { |
| 127 | + const ctx = createCtxForExport('pdf'); |
| 128 | + const result = createCanvas() |
| 129 | + .item(1) |
| 130 | + .toCanvasElement({ ...size, ctx }); |
| 131 | + expect(result).toMatchObject(size); |
| 132 | + await attachPDF(result, testInfo); |
| 133 | + await testPDF(page, result); |
| 134 | + }); |
| 135 | + }); |
| 136 | +}); |
0 commit comments