刘光辉
2 小时以前 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import { mount, shallowMount } from '@vue/test-utils';
import { nextTick, reactive, watch } from 'vue';
 
import { Input as AInput } from 'ant-design-vue';
import { describe, expect, it, vi } from 'vitest';
 
import ParameterTable from '../ParameterTable.vue';
import tableSource from '../ParameterTable.vue?raw';
 
const tableState = vi.hoisted(() => ({ config: undefined as any }));
 
vi.mock('@jnpf/ui/vxeTable', () => ({
  BasicVxeTable: {
    name: 'BasicVxeTable',
    props: ['dataSource'],
    template:
      '<div><slot v-if="dataSource?.length" name="canshuMingcheng" :record="dataSource[0]" /><slot v-if="dataSource?.length" name="shujuLeixing" :record="dataSource[0]" /><slot v-if="dataSource?.length" name="danwei" :record="dataSource[0]" /></div>',
  },
  useVxeTable: (config: any) => {
    tableState.config = config;
    return [vi.fn()];
  },
}));
 
describe('eLN formula parameter table', () => {
  it('only allows draft slots to be edited or removed', () => {
    const wrapper = mount(ParameterTable, {
      props: { modelValue: [], readonly: true, status: 'PRE_RELEASE' },
      global: {
        stubs: {
          'a-alert': { props: ['message'], template: '<span>{{ message }}</span>' },
          'a-button': true,
          'a-input': true,
          'a-input-number': true,
          'a-select': true,
          'a-switch': true,
          'a-tag': true,
        },
      },
    });
    expect(wrapper.text()).toContain('集合参数仅支持映射重复区域的数值字段,不支持普通字段或逐行公式');
    expect(tableSource).toContain("status === 'DRAFT'");
    expect(tableSource).toContain('canshuBianma');
    expect(tableSource).toContain('DECIMAL_LIST');
  });
 
  it('adds only business properties and leaves the generated code to the backend', () => {
    expect(tableSource).toContain("canshuMingcheng: ''");
    expect(tableSource).not.toContain("canshuBianma: ''");
  });
 
  it('uses a stable client key for unsaved parameter rows', () => {
    expect(tableSource).toContain('row._clientKey || row.id');
    expect(tableSource).toContain('_clientKey:');
  });
 
  it('uses a stable height when embedded in a formula modal', () => {
    shallowMount(ParameterTable, {
      props: { modelValue: [], status: 'DRAFT' },
      global: { stubs: { 'a-alert': true, 'a-button': true, 'a-input': true, 'a-input-number': true, 'a-select': true, 'a-switch': true } },
    });
 
    expect(tableState.config.height).toBe(220);
  });
 
  it('uses its own stable height when it replaces the formula workspace', () => {
    const wrapper = shallowMount(ParameterTable, {
      props: { fullHeight: true, modelValue: [], status: 'DRAFT' },
      global: { stubs: { 'a-alert': true, 'a-button': true, 'a-input': true, 'a-input-number': true, 'a-select': true, 'a-switch': true } },
    });
 
    expect(typeof tableState.config.height).toBe('number');
    expect(tableState.config.height).toBeGreaterThanOrEqual(360);
    expect(wrapper.classes()).toContain('is-full-height');
  });
 
  it('maps parameter type option labels and values for the native select', () => {
    const wrapper = mount(ParameterTable, {
      props: {
        modelValue: [{ canshuBianma: 'amount', canshuMingcheng: '含量', paixu: 1, shifouBitian: 1, shujuLeixing: 'DECIMAL' }],
        status: 'DRAFT',
      },
      global: {
        stubs: {
          'a-alert': true,
          'a-button': true,
          'a-input': true,
          'a-input-number': true,
          'a-select': { name: 'ASelect', props: ['fieldNames', 'options', 'value'], template: '<div />' },
          'a-switch': true,
        },
      },
    });
 
    const typeSelect = wrapper.findComponent({ name: 'ASelect' });
    expect(typeSelect.props('fieldNames')).toEqual({ label: 'fullName', value: 'id' });
    expect(typeSelect.props('options')).toContainEqual({ fullName: '数值', id: 'DECIMAL' });
  });
 
  it('provides prompts for editable parameter fields and enough room for sorting', () => {
    shallowMount(ParameterTable, {
      props: { modelValue: [], status: 'DRAFT' },
      global: { stubs: { 'a-alert': true, 'a-button': true, 'a-input': true, 'a-input-number': true, 'a-select': true, 'a-switch': true } },
    });
 
    expect(tableSource).toContain('placeholder="请输入参数名称"');
    expect(tableSource).toContain('placeholder="请选择类型"');
    expect(tableSource).toContain('placeholder="请输入单位"');
    expect(tableSource).toContain('placeholder="请输入排序"');
    expect(tableState.config.columns.find((column: any) => column.dataIndex === 'paixu').width).toBe(110);
  });
 
  it('uses a compact action header without adding space below the notice', () => {
    expect(tableSource).not.toContain('<span>参数槽</span>');
    expect(tableSource).toContain('<a-button v-if="editable" type="default"');
    expect(tableSource).toContain('<a-alert banner class="parameter-table__notice"');
    expect(tableSource).toMatch(/\.parameter-table__notice\s*\{[^}]*margin-top: 8px/);
    expect(tableSource).not.toMatch(/\.parameter-table__notice\s*\{[^}]*margin-bottom:/);
  });
 
  it('updates a unit without triggering a reactive table refresh', async () => {
    const parameter = reactive({
      _clientKey: 'row-1',
      canshuMingcheng: '含量',
      danwei: undefined as string | undefined,
      paixu: 1,
      shifouBitian: 1 as const,
      shujuLeixing: 'DECIMAL' as const,
    });
    let reactiveUpdates = 0;
    watch(
      () => parameter.danwei,
      () => reactiveUpdates++,
    );
    const wrapper = mount(ParameterTable, {
      props: { modelValue: [parameter], status: 'DRAFT' },
      global: {
        stubs: {
          'a-alert': true,
          'a-button': true,
          'a-input': { name: 'AInput', props: ['value'], template: '<input />' },
          'a-input-number': true,
          'a-select': true,
          'a-switch': true,
        },
      },
    });
 
    const unitInput = wrapper.findAllComponents({ name: 'AInput' }).find((input) => input.props('value') === undefined)!;
    unitInput.vm.$emit('update:value', 'mg/mL');
    await nextTick();
 
    expect(unitInput.props('value')).toBe('mg/mL');
    expect(parameter.danwei).toBe('mg/mL');
    expect(reactiveUpdates).toBe(0);
    expect(wrapper.emitted('update:modelValue')).toBeUndefined();
  });
 
  it('updates a parameter name without triggering a reactive table refresh', async () => {
    const parameter = reactive({
      _clientKey: 'row-1',
      canshuMingcheng: '含量',
      danwei: undefined as string | undefined,
      paixu: 1,
      shifouBitian: 1 as const,
      shujuLeixing: 'DECIMAL' as const,
    });
    let reactiveUpdates = 0;
    watch(
      () => parameter.canshuMingcheng,
      () => reactiveUpdates++,
    );
    const wrapper = mount(ParameterTable, {
      props: { modelValue: [parameter], status: 'DRAFT' },
      global: {
        stubs: {
          'a-alert': true,
          'a-button': true,
          'a-input': { name: 'AInput', props: ['value'], template: '<input />' },
          'a-input-number': true,
          'a-select': true,
          'a-switch': true,
        },
      },
    });
 
    const nameInput = wrapper.findAllComponents({ name: 'AInput' }).find((input) => input.props('value') === '含量')!;
    nameInput.vm.$emit('update:value', '水分');
    await nextTick();
 
    expect(nameInput.props('value')).toBe('水分');
    expect(parameter.canshuMingcheng).toBe('水分');
    expect(reactiveUpdates).toBe(0);
    expect(wrapper.emitted('update:modelValue')).toBeUndefined();
  });
 
  it('keeps real Ant inputs interactive while synchronizing submitted text values', async () => {
    const parameter = reactive({
      _clientKey: 'row-1',
      canshuMingcheng: '',
      danwei: undefined as string | undefined,
      paixu: 1,
      shifouBitian: 1 as const,
      shujuLeixing: 'DECIMAL' as const,
    });
    const wrapper = mount(ParameterTable, {
      props: { modelValue: [parameter], status: 'DRAFT' },
      global: {
        components: { AInput },
        stubs: {
          'a-alert': true,
          'a-button': true,
          'a-input-number': true,
          'a-select': true,
          'a-switch': true,
        },
      },
    });
 
    const nameInput = wrapper.find('input[placeholder="请输入参数名称"]');
    const unitInput = wrapper.find('input[placeholder="请输入单位"]');
    await nameInput.setValue('水分');
    await unitInput.setValue('mg/mL');
 
    expect((nameInput.element as HTMLInputElement).value).toBe('水分');
    expect((unitInput.element as HTMLInputElement).value).toBe('mg/mL');
    expect(parameter.canshuMingcheng).toBe('水分');
    expect(parameter.danwei).toBe('mg/mL');
  });
});