Files
SupplierSystem/src/utils/pickingLabelPrint.test.ts
T
雨天 657c5fbc51
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
feat(分拣打印): 标签纸张尺寸可配(不写死)
打印弹窗新增「纸张宽/高(mm)」(默认 50x30),改后记住 localStorage,@page size 与标签宽高随之,非法值回落默认。换纸/换驱动只改这两个数,不用改代码。另:浏览器不会告知纸张尺寸,CSS 只能跟随不能猜,纸张尺寸必须填成实际标签规格。
2026-10-13 19:28:54 +08:00

123 lines
5.3 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,
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/);
expect(html).toMatch(/\.pick-label \{[^}]*height: 59\.5mm/);
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: '斤' }]);
expect(html).toMatch(/\.pick-label \{[^}]*page-break-inside: avoid/);
// 回归①:强制分页在「浏览器页高=标签高整数倍」时,每页剩下一整张空白标签(打 N 张耗 2N 张)
expect(html).not.toContain('page-break-after: always');
// 回归②:百分比高会把每张标签撑成两页
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('有 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('订单号');
});
});