刘光辉
11 小时以前 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
115
116
117
118
119
120
121
122
import { describe, expect, it } from 'vitest';
 
import { convertControlType, replaceControlData, sortControlTypeOptionsByUsage } from './controlType';
 
describe('convertControlType', () => {
  const input = {
    __config__: {
      defaultValue: 'old value',
      formId: 'formItem1',
      isSubTable: true,
      jnpfKey: 'input',
      label: '版本',
      parentVModel: 'versions',
      regList: [{ pattern: '/.+/' }],
      span: 12,
      tableName: 'version_record',
    },
    __vModel__: 'version_name',
    addonBefore: 'V',
    disabled: true,
    on: { blur: 'old blur', change: 'old change' },
    placeholder: '请输入版本',
    style: { width: '60%' },
  };
  const select = {
    __config__: {
      defaultValue: undefined,
      formId: '',
      jnpfKey: 'select',
      label: '下拉选择',
      regList: [],
      span: 24,
      tableName: '',
    },
    disabled: false,
    filterable: false,
    on: { change: 'default change' },
    options: [],
    placeholder: '请选择',
    style: { width: '100%' },
  };
 
  it('keeps field identity and shared settings while adopting the target schema', () => {
    const converted = convertControlType(input, select);
 
    expect(converted.__config__).toMatchObject({
      defaultValue: undefined,
      formId: 'formItem1',
      isSubTable: true,
      jnpfKey: 'select',
      label: '版本',
      parentVModel: 'versions',
      span: 12,
      tableName: 'version_record',
    });
    expect(converted).toMatchObject({
      __vModel__: 'version_name',
      disabled: true,
      filterable: false,
      on: { change: 'old change' },
      options: [],
      placeholder: '请输入版本',
      style: { width: '60%' },
    });
    expect(converted.__config__.regList).toEqual([]);
    expect(converted).not.toHaveProperty('addonBefore');
    expect(converted.on).not.toHaveProperty('blur');
  });
 
  it('replaces an existing reactive-shaped object without changing its reference', () => {
    const activeData = { ...input };
    const originalReference = activeData;
    const converted = convertControlType(activeData, select);
 
    replaceControlData(activeData, converted);
 
    expect(activeData).toBe(originalReference);
    expect(activeData.__config__.jnpfKey).toBe('select');
    expect(activeData).not.toHaveProperty('addonBefore');
  });
});
 
describe('sortControlTypeOptionsByUsage', () => {
  it('uses the preferred order before the fixed all-form usage snapshot', () => {
    const options = [
      { id: 'textarea', fullName: '多行输入' },
      { id: 'inputNumber', fullName: '数字输入' },
      { id: 'checkbox', fullName: '多选框组' },
      { id: 'radio', fullName: '单选框组' },
      { id: 'popupSelect', fullName: '弹窗选择' },
      { id: 'select', fullName: '下拉选择' },
      { id: 'organizeSelect', fullName: '组织选择' },
      { id: 'userSelect', fullName: '用户选择' },
      { id: 'input', fullName: '单行输入' },
      { id: 'datePicker', fullName: '日期选择' },
    ];
 
    expect(sortControlTypeOptionsByUsage(options).map((item) => item.id)).toEqual([
      'input',
      'datePicker',
      'userSelect',
      'organizeSelect',
      'select',
      'popupSelect',
      'radio',
      'checkbox',
      'inputNumber',
      'textarea',
    ]);
  });
 
  it('preserves fixed usage ordering for controls outside the preferred list', () => {
    const options = [
      { id: 'colorPicker', fullName: '颜色选择' },
      { id: 'relationForm', fullName: '关联表单' },
      { id: 'signature', fullName: '电子签章' },
      { id: 'uploadFile', fullName: '文件上传' },
    ];
 
    expect(sortControlTypeOptionsByUsage(options).map((item) => item.id)).toEqual(['uploadFile', 'relationForm', 'colorPicker', 'signature']);
  });
});