Files
SupplierSystem/src/utils/pickingLabelPrint.test.ts
T
雨天 65f07c2495
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
分拣标签改为块高只包内容+强制分页
打印对话框的「边距」会盖掉 @page{margin:0}(默认约 10mm),块高=纸高时\n溢出被拆到下一页:实测打两张耗三张、中间夹一张空白。\n\n旋转 0° 的块高改为 min(纸高-0.5, 内容下沿+insetY),一签一页改由\nbreak-after:page 保证(末张复位,防尾部空白);旋转 90/180/270 仍用整页高。
2026-10-14 13:53:20 +08:00

217 lines
10 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, it, expect } from 'vitest';
import {
buildPickingLabelHtml,
buildPickingLabelRulerHtml,
formatLabelWeightLine,
sortItemsForLabelPrint,
} from './pickingLabelPrint';
describe('formatLabelWeightLine(与旧秤端逐字一致)', () => {
it('重量单位统一换算为「斤」', () => {
expect(formatLabelWeightLine(12, '斤')).toBe('重量:12.00 斤');
expect(formatLabelWeightLine(6, 'kg')).toBe('重量:12.00 斤');
expect(formatLabelWeightLine(6, '公斤')).toBe('重量:12.00 斤');
expect(formatLabelWeightLine(1000, '克')).toBe('重量:2.00 斤');
});
it('计数单位展示数量', () => {
expect(formatLabelWeightLine(5, '瓶')).toBe('数量:5.00 瓶');
expect(formatLabelWeightLine(3, '')).toBe('数量:3.00 斤');
});
});
describe('sortItemsForLabelPrint(批量打印排队)', () => {
// 分组值用 ASCII,避免断言依赖中文拼音排序
const items = [
{ item_name: 'C-白菜', order_code: 'O2', category_name: 'CAT-B', storage_zone: 'Z-B', sku_code: 'SKU-3', sort_order: 2 },
{ item_name: 'A-苹果', order_code: 'O1', category_name: 'CAT-A', storage_zone: 'Z-A', sku_code: 'SKU-1', sort_order: 1 },
{ item_name: 'B-菠菜', order_code: 'O1', category_name: 'CAT-A', storage_zone: 'Z-A', sku_code: 'SKU-2', sort_order: 1 },
];
it('按订单分组,组内按 sort_order 再按品名', () => {
const out = sortItemsForLabelPrint(items, 'order').map((i) => i.item_name);
expect(out).toEqual(['A-苹果', 'B-菠菜', 'C-白菜']);
});
it('按分类分组', () => {
const out = sortItemsForLabelPrint(items, 'category').map((i) => i.item_name);
expect(out).toEqual(['A-苹果', 'B-菠菜', 'C-白菜']);
});
it('按商品(SKU)分组', () => {
const out = sortItemsForLabelPrint(items, 'sku').map((i) => i.item_name);
expect(out).toEqual(['A-苹果', 'B-菠菜', 'C-白菜']);
});
it('按区域分组,且尊重分公司区域顺序', () => {
const ordered = sortItemsForLabelPrint(items, 'zone', { zoneOrder: ['Z-B', 'Z-A'] }).map((i) => i.storage_zone);
expect(ordered).toEqual(['Z-B', 'Z-A', 'Z-A']);
});
it('未配置区域顺序时按名称排', () => {
const fallback = sortItemsForLabelPrint(items, 'zone').map((i) => i.storage_zone);
expect(fallback).toEqual(['Z-A', 'Z-A', 'Z-B']);
});
it('不改动原数组', () => {
const before = items.map((i) => i.item_name);
sortItemsForLabelPrint(items, 'sku');
expect(items.map((i) => i.item_name)).toEqual(before);
});
});
describe('buildPickingLabelHtml(分拣标签 · 浏览器打印)', () => {
it('默认纸张 50×30mm(可被传入覆盖)', () => {
const html = buildPickingLabelHtml([{ itemName: '大葱', qty: 10, unit: '斤' }]);
expect(html).toContain('@page { size: 50mm 30mm; margin: 0; }');
expect(html).toContain('class="pick-label"');
expect(html).toContain('分拣标签');
});
it('纸张尺寸动态传入(不写死);非法值回落默认', () => {
const html = buildPickingLabelHtml([{ itemName: 'a', qty: 1, unit: '斤' }], {
paper: { widthMm: 80, heightMm: 60 },
});
expect(html).toContain('@page { size: 80mm 60mm; margin: 0; }');
expect(html).toMatch(/\.pick-label \{[^}]*width: 80mm/);
// 块高**只包住内容**(不是 59.5mm = 纸高):insetY 3.57 + 内容下沿 20.27 + 下边距 3.57 ≈ 23.84
expect(html).toMatch(/\.pick-label \{[^}]*height: 23\.84mm/);
const bad = buildPickingLabelHtml([{ itemName: 'a', qty: 1, unit: '斤' }], {
paper: { widthMm: 0, heightMm: -3 },
});
expect(bad).toContain('@page { size: 50mm 30mm; margin: 0; }');
});
it('一签一页:块高只包住内容 + 强制分页(防对话框边距把块挤到下一页)', () => {
const html = buildPickingLabelHtml([{ itemName: '大葱', qty: 1, unit: '斤' }]);
// 回归①(2026-10-14 实测):块高 = 纸高 时,打印对话框的「边距」会盖掉 @page{margin:0}、
// 把可用高度压小 ⇒ 块溢出被浏览器拆到下一页 ⇒ 打两张耗三张、中间夹一张空白。
// 块高必须**小于**纸高,且 50×30 时块高 ≤ 20.24mm(远小于任何纸型的可用高)。
expect(html).not.toMatch(/\.pick-label \{[^}]*height: 29\.5mm/);
expect(html).toMatch(/\.pick-label \{[^}]*height: 20\.24mm/);
expect(html).toMatch(/\.pick-label \{[^}]*page-break-inside: avoid/);
// 一签一页改靠**强制分页**;末张复位,防尾部多出一张空白页
expect(html).toMatch(/\.pick-label \{[^}]*page-break-after: always/);
expect(html).toMatch(/\.pick-label:last-child \{[^}]*page-break-after: auto/);
// 回归②:百分比高会把每张标签撑成两页(打印宿主 @media print 把 html/body 高度置 auto)
expect(html).not.toContain('padding: 0; height: 100%');
expect(html).not.toContain('min-height: 100%');
expect(html).not.toContain('pick-page');
});
it('多张标签 = 多页(一签一页)', () => {
const html = buildPickingLabelHtml([
{ itemName: 'a', qty: 1, unit: '斤' },
{ itemName: 'b', qty: 2, unit: '斤' },
]);
expect((html.match(/class="pick-label"/g) || []).length).toBe(2);
});
it('旋转 0°(默认):不包页面框、无 transform', () => {
const html = buildPickingLabelHtml([{ itemName: 'a', qty: 1, unit: '斤' }]);
expect(html).not.toContain('class="pick-page"');
expect(html).not.toContain('rotate(');
});
it('旋转 90°/270°:页面尺寸转置 + 标签块旋转(一页一签)', () => {
const html90 = buildPickingLabelHtml([{ itemName: 'a', qty: 1, unit: '斤' }], {
paper: { widthMm: 80, heightMm: 30 },
rotation: 90,
});
// 页面转置为 30×80,标签块仍是 80×29.5 但整体转 90°(旋转模式仍用整页高,块高压缩只作用于 0°)
expect(html90).toContain('@page { size: 30mm 80mm; margin: 0; }');
expect(html90).toMatch(/\.pick-label \{[^}]*width: 80mm/);
expect(html90).toMatch(/\.pick-label \{[^}]*height: 29\.5mm/);
expect(html90).toContain('rotate(90deg)');
expect(html90).toContain('class="pick-page"');
expect(html90).toContain('break-after: page');
const html270 = buildPickingLabelHtml([{ itemName: 'a', qty: 1, unit: '斤' }], {
paper: { widthMm: 80, heightMm: 30 },
rotation: 270,
});
expect(html270).toContain('@page { size: 30mm 80mm; margin: 0; }');
expect(html270).toContain('rotate(-90deg)');
});
it('旋转 180°:页面尺寸不变、标签块翻转', () => {
const html = buildPickingLabelHtml([{ itemName: 'a', qty: 1, unit: '斤' }], {
paper: { widthMm: 80, heightMm: 30 },
rotation: 180,
});
expect(html).toContain('@page { size: 80mm 30mm; margin: 0; }');
expect(html).toContain('rotate(180deg)');
});
it('非法旋转值回落 0°', () => {
const html = buildPickingLabelHtml([{ itemName: 'a', qty: 1, unit: '斤' }], {
rotation: 45 as unknown as number,
});
expect(html).not.toContain('rotate(');
});
it('测试打印(标尺):整页刻度 + 页码数跟随 pages,纸张/旋转与标签一致', () => {
const html = buildPickingLabelRulerHtml({
paper: { widthMm: 80, heightMm: 30 },
rotation: 90,
pages: 3,
});
// 页面尺寸跟随旋转(转置)
expect(html).toContain('@page { size: 30mm 80mm; margin: 0; }');
// 每 5mm 一个刻度:横向 0..80 ⇒ 17 个;纵向 0..29.5 ⇒ 6 个;共 3 页
expect((html.match(/class="tk-v/g) || []).length).toBe(17 * 3);
expect((html.match(/class="tk-h/g) || []).length).toBe(6 * 3);
// 标注当前纸张与旋转,便于拍照回报
expect(html).toContain('80 × 30 mm');
expect(html).toContain('旋转 90');
// 页码数
expect((html.match(/class="pick-label"/g) || []).length).toBe(3);
// 旋转模式下一页一签
expect((html.match(/class="pick-page"/g) || []).length).toBe(3);
});
it('窄纸(50×30)四周留比例边距(≥1mm),且不再贴边', () => {
const html = buildPickingLabelHtml([{ itemName: 'a', qty: 1, unit: '斤', barcode: 'X' }], {
paper: { widthMm: 50, heightMm: 30 },
});
// 50 × 6% = 3mm ⇒ 二维码 right 3mm;文本块右边界 = 3 + 13.75 + 1 = 17.750mm
expect(html).toMatch(/\.t-qr \{[^}]*right: 3\.000mm/);
expect(html).toMatch(/\.pick-label > div \{[^}]*left: 3\.000mm/);
expect(html).toMatch(/\.pick-label > div \{[^}]*right: 17\.750mm/);
});
it('宽纸(80×30)受可打印宽度约束:左右各让出 10mm,二维码落在可打印区内', () => {
const html = buildPickingLabelHtml([{ itemName: 'a', qty: 1, unit: '斤', barcode: 'X' }], {
paper: { widthMm: 80, heightMm: 30 },
});
// inset = max(1, 80×6% = 4.8, 80 − 70 = 10) = 10mm
// ⇒ 内容落在 10–70mm(居中铺/左对齐铺两种映射都在可打印区内)
expect(html).toMatch(/\.t-qr \{[^}]*right: 10\.000mm/);
expect(html).toMatch(/\.pick-label > div \{[^}]*left: 10\.000mm/);
expect(html).toMatch(/\.pick-label > div \{[^}]*right: 24\.750mm/);
// @page 仍是实物签尺寸(不被内缩改写)
expect(html).toContain('@page { size: 80mm 30mm; margin: 0; }');
});
it('有 barcode 时内联二维码 SVG;无则不输出', () => {
const withQr = buildPickingLabelHtml([{ itemName: '大葱', qty: 1, unit: '斤', barcode: 'SKU-1' }]);
expect(withQr).toContain('class="t-qr"');
expect(withQr).toContain('<svg');
const withoutQr = buildPickingLabelHtml([{ itemName: '大葱', qty: 1, unit: '斤' }]);
expect(withoutQr).not.toContain('class="t-qr"');
expect(withoutQr).not.toContain('<svg');
});
it('字段带前缀、默认值与 HTML 转义;不出现订单号', () => {
const html = buildPickingLabelHtml([
{ itemName: '<b>大葱</b>', qty: 1, unit: '斤', companyName: '', barcode: 'a&b' },
]);
expect(html).toContain('客户:客户');
expect(html).toContain('品名:&lt;b&gt;大葱&lt;/b&gt;');
expect(html).toContain('a&amp;b');
expect(html).not.toContain('订单号');
});
});