刘光辉
4 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { describe, expect, it } from 'vitest';
 
import { buildLabelLayoutHtml, defaultLabelLayoutConfig, getLabelSize, normalizeLabelLayoutConfig, validateLabelLayout } from './labelLayout';
 
describe('label layout configuration', () => {
  it('normalizes persisted values without mutating the defaults', () => {
    expect(normalizeLabelLayoutConfig({ columns: 2, enabled: true, itemsPerPage: 5, paperHeight: 210, paperWidth: 297 })).toMatchObject({
      columns: 2,
      enabled: true,
      itemsPerPage: 5,
      paperHeight: 210,
      paperWidth: 297,
    });
    expect(defaultLabelLayoutConfig.enabled).toBe(false);
    expect(defaultLabelLayoutConfig).toMatchObject({ paperHeight: 210, paperWidth: 297 });
  });
 
  it('reads a fixed label size from a single panel template', () => {
    expect(getLabelSize({ panels: [{ height: 50, width: 75 }] })).toEqual({ height: 50, width: 75 });
    expect(getLabelSize({ panels: [{ height: 297, width: 210 }] }, { labelHeight: 50, labelWidth: 75 })).toEqual({ height: 50, width: 75 });
    expect(() =>
      getLabelSize({
        panels: [
          { height: 50, width: 75 },
          { height: 50, width: 75 },
        ],
      }),
    ).toThrow('仅支持单面板');
  });
 
  it('rejects a layout that exceeds the physical paper', () => {
    const config = normalizeLabelLayoutConfig({
      columns: 3,
      enabled: true,
      itemsPerPage: 6,
      marginLeft: 10,
      marginRight: 10,
      paperHeight: 210,
      paperWidth: 210,
    });
    expect(validateLabelLayout(config, { height: 50, width: 75 })).toContain('超过纸张宽度');
  });
 
  it('validates the height against the labels in the current preview', () => {
    const config = normalizeLabelLayoutConfig({ columns: 1, enabled: true, itemsPerPage: 5, paperHeight: 297, paperWidth: 210 });
    expect(validateLabelLayout(config, { height: 297, width: 210 }, 1)).toBe('');
    expect(validateLabelLayout(config, { height: 297, width: 210 }, 2)).toContain('超过纸张高度');
  });
});
 
describe('buildLabelLayoutHtml', () => {
  it('places five labels after one empty slot in a two-column grid', () => {
    const renderedHtml = `<div class="hiprint-printPanel">${Array.from(
      { length: 5 },
      (_, index) => `<div class="hiprint-printPaper"><span>标签${index + 1}</span></div>`,
    ).join('')}</div>`;
    const result = buildLabelLayoutHtml(
      renderedHtml,
      { panels: [{ height: 50, width: 75 }] },
      {
        columns: 2,
        enabled: true,
        itemsPerPage: 5,
        paperHeight: 210,
        paperWidth: 297,
        startOffset: 1,
      },
    );
 
    const root = document.createElement('div');
    root.innerHTML = result.html;
    expect(result.pageCount).toBe(1);
    expect(root.querySelectorAll('.jnpf-label-sheet')).toHaveLength(1);
    expect(root.querySelectorAll('.jnpf-label-slot')).toHaveLength(6);
    expect(root.querySelector('.jnpf-label-slot')?.classList.contains('is-empty')).toBe(true);
    expect(root.textContent).toContain('标签5');
  });
 
  it('starts subsequent pages without the first-page offset', () => {
    const renderedHtml = `<div>${Array.from({ length: 6 }, () => '<div class="hiprint-printPaper">标签</div>').join('')}</div>`;
    const result = buildLabelLayoutHtml(
      renderedHtml,
      { panels: [{ height: 20, width: 30 }] },
      {
        columns: 2,
        enabled: true,
        itemsPerPage: 5,
        paperHeight: 100,
        paperWidth: 100,
        startOffset: 1,
      },
    );
 
    const root = document.createElement('div');
    root.innerHTML = result.html;
    const sheets = root.querySelectorAll('.jnpf-label-sheet');
    expect(sheets).toHaveLength(2);
    expect(sheets[0]?.querySelectorAll('.jnpf-label-slot')).toHaveLength(6);
    expect(sheets[1]?.querySelectorAll('.jnpf-label-slot')).toHaveLength(1);
  });
});