刘光辉
9 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
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
102
103
104
105
106
107
108
109
110
111
112
113
114
import { describe, expect, it } from 'vitest';
 
import {
  getDefaultActiveKey,
  getReportViewHeaderMode,
  hasCustomPrintTab,
  normalizeReportViewConfig,
  shouldEnablePrintAction,
  validateReportViewConfig,
} from '../helpers/reportView';
import type { ReportViewConfig } from '../types';
 
const formDetailTab = {
  id: 'form-id',
  key: 'formDetail',
  modelId: 'model-id',
} as const;
 
const customPrintTab = {
  key: 'customPrint',
  print: {
    data: { id: 'record-id' },
    template: 'template-id',
  },
} as const;
 
const attachmentsTab = {
  key: 'attachments',
} as const;
 
describe('reportView helpers', () => {
  it('rejects config without tabs', () => {
    expect(() => validateReportViewConfig({ tabs: [] })).toThrow('查看报告至少需要一个 tab');
    expect(() => validateReportViewConfig({} as any)).toThrow('查看报告至少需要一个 tab');
  });
 
  it('uses first tab key as default active key', () => {
    expect(getDefaultActiveKey({ tabs: [formDetailTab, customPrintTab] })).toBe('formDetail');
  });
 
  it('keeps configured active key when it exists in tabs', () => {
    expect(getDefaultActiveKey({ activeKey: 'customPrint', tabs: [formDetailTab, customPrintTab] })).toBe('customPrint');
  });
 
  it('normalizes default title and tab titles', () => {
    const normalized = normalizeReportViewConfig({
      tabs: [formDetailTab, customPrintTab, attachmentsTab],
    });
 
    expect(normalized.title).toBe('查看报告');
    expect(normalized.tabs).toEqual([
      { ...formDetailTab, internalKey: 'formDetail:0', title: '表单详情' },
      { ...customPrintTab, internalKey: 'customPrint:1', title: '报告' },
      { ...attachmentsTab, internalKey: 'attachments:2', title: '附件' },
    ]);
  });
 
  it('normalizes duplicate form detail tabs with unique internal keys', () => {
    const normalized = normalizeReportViewConfig({
      activeKey: 'formDetail',
      tabs: [
        { ...formDetailTab, id: 'form-id-1', title: '委托信息' },
        { ...formDetailTab, id: 'form-id-2', title: '检验记录' },
      ],
    });
 
    expect(normalized.activeKey).toBe('formDetail:0');
    expect(normalized.tabs).toEqual([
      { ...formDetailTab, id: 'form-id-1', internalKey: 'formDetail:0', title: '委托信息' },
      { ...formDetailTab, id: 'form-id-2', internalKey: 'formDetail:1', title: '检验记录' },
    ]);
  });
 
  it('uses form detail id as active key when selecting a duplicate form detail tab', () => {
    const normalized = normalizeReportViewConfig({
      activeKey: 'form-id-2',
      tabs: [
        { ...formDetailTab, id: 'form-id-1', title: '委托信息' },
        { ...formDetailTab, id: 'form-id-2', title: '检验记录' },
      ],
    });
 
    expect(normalized.activeKey).toBe('formDetail:1');
  });
 
  it('selects print header mode only when actions is not provided', () => {
    expect(getReportViewHeaderMode({ tabs: [customPrintTab] })).toBe('print');
    expect(getReportViewHeaderMode({ actions: [], tabs: [customPrintTab] })).toBe('actions');
 
    const config: ReportViewConfig = {
      actions: [
        {
          key: 'approve',
          label: '批准',
          onClick: () => undefined,
        },
      ],
      tabs: [customPrintTab],
    };
 
    expect(getReportViewHeaderMode(config)).toBe('actions');
  });
 
  it('detects custom print tab', () => {
    expect(hasCustomPrintTab({ tabs: [formDetailTab] })).toBe(false);
    expect(hasCustomPrintTab({ tabs: [formDetailTab, customPrintTab] })).toBe(true);
  });
 
  it('enables print action only when current active key is customPrint and customPrint tab exists', () => {
    expect(shouldEnablePrintAction({ tabs: [formDetailTab, customPrintTab] }, 'formDetail')).toBe(false);
    expect(shouldEnablePrintAction({ tabs: [customPrintTab] }, 'customPrint')).toBe(true);
    expect(shouldEnablePrintAction({ tabs: [formDetailTab] }, 'customPrint')).toBe(false);
  });
});