Files
label-tools/tests/labels/render.test.ts
T
2026-09-01 13:05:57 +02:00

71 lines
2.0 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { renderLabelPages } from "../../src/labels/render";
import {
STOCKS,
fromMillimetres,
toMillimetres,
} from "../../src/labels/stocks";
import type { RenderOptions } from "../../src/labels/types";
function options(): RenderOptions {
return {
stock: STOCKS[0]!,
calibration: { offsetXmm: 0, offsetYmm: 0, scaleX: 1, scaleY: 1 },
layout: "asset",
mapping: {
title: "name",
subtitle: "serial",
body: "body",
barcode: "code",
image: "image",
},
barcodeFormat: "qrcode",
includeBarcodeText: false,
fontFamily: "Arial, Helvetica, sans-serif",
records: [
{
name: "<script>alert(1)</script>",
serial: "A-1",
body: "local",
code: "https://example.test/inert",
image: "",
},
],
images: [],
defaultImage: "",
cutMarks: true,
registrationMarks: true,
startPosition: 2,
};
}
describe("label SVG renderer", () => {
it("creates a physical-size inert SVG and escapes merged text", () => {
const result = renderLabelPages(options());
expect(result.pages).toHaveLength(1);
expect(result.pages[0]).toContain('width="210mm"');
expect(result.pages[0]).toContain("&lt;script&gt;alert");
expect(result.pages[0]).not.toContain("<script>");
expect(result.pages[0]).toContain("clip-path");
});
it("paginates with a start position and caps preview rendering", () => {
const input = options();
input.records = Array.from({ length: 60 }, (_, index) => ({
name: String(index),
code: String(index),
}));
expect(renderLabelPages(input, 2)).toMatchObject({
totalPages: 3,
totalLabels: 60,
});
expect(renderLabelPages(input, 2).pages).toHaveLength(2);
});
it("converts mm, inches and points round-trip", () => {
expect(toMillimetres(1, "in")).toBeCloseTo(25.4);
expect(fromMillimetres(25.4, "pt")).toBeCloseTo(72);
expect(toMillimetres(fromMillimetres(42, "in"), "in")).toBeCloseTo(42);
});
});