刘光辉
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import { flushPromises, mount } from '@vue/test-utils';
import { defineComponent, h } from 'vue';
 
import { beforeEach, describe, expect, it, vi } from 'vitest';
 
import StepSettingsDrawer from '../StepSettingsDrawer.vue';
 
const apiMocks = vi.hoisted(() => ({
  getTemplateSteps: vi.fn(),
  saveTemplateSteps: vi.fn(),
}));
const drawerState = vi.hoisted(() => ({ init: undefined as any }));
const drawerActions = vi.hoisted(() => ({
  changeLoading: vi.fn(),
  changeOkLoading: vi.fn(),
  close: vi.fn(),
}));
const messageMocks = vi.hoisted(() => ({ error: vi.fn(), success: vi.fn(), warning: vi.fn() }));
const modalActions = vi.hoisted(() => ({ close: vi.fn(), open: vi.fn() }));
 
const configuration = {
  singleStep: false,
  steps: [
    { id: 'step-2', name: '记录结果', order: 2 },
    { id: 'step-1', name: '准备样品', order: 1 },
  ],
};
 
const InputStub = defineComponent({
  name: 'AInput',
  inheritAttrs: false,
  props: { value: String },
  emits: ['update:value'],
  setup(props, { attrs, emit }) {
    return () =>
      h('input', {
        ...attrs,
        value: props.value,
        onInput: (event: Event) => emit('update:value', (event.target as HTMLInputElement).value),
      });
  },
});
 
const SelectStub = defineComponent({
  name: 'ASelect',
  inheritAttrs: false,
  props: { options: { type: Array, default: () => [] }, value: String },
  emits: ['update:value'],
  setup(props, { attrs, emit }) {
    return () =>
      h(
        'select',
        {
          ...attrs,
          value: props.value,
          onChange: (event: Event) => emit('update:value', (event.target as HTMLSelectElement).value || undefined),
        },
        [h('option', { value: '' }), ...props.options.map((item: any) => h('option', { value: item.value }, item.label))],
      );
  },
});
 
const ButtonStub = defineComponent({
  name: 'AButton',
  inheritAttrs: false,
  props: { disabled: Boolean },
  emits: ['click'],
  setup(props, { attrs, emit, slots }) {
    return () => h('button', { ...attrs, disabled: props.disabled, onClick: () => emit('click') }, [slots.icon?.(), slots.default?.()]);
  },
});
 
vi.mock('#/api/x/eln/templates', () => ({
  getTemplateSteps: apiMocks.getTemplateSteps,
  saveTemplateSteps: apiMocks.saveTemplateSteps,
}));
 
vi.mock('@jnpf/hooks', () => ({
  useMessage: () => ({ createMessage: messageMocks }),
}));
 
vi.mock('@jnpf/ui/drawer', async () => {
  const { defineComponent, h } = await import('vue');
  return {
    BasicDrawer: defineComponent({
      name: 'BasicDrawer',
      inheritAttrs: false,
      props: { showFooter: Boolean, title: String },
      emits: ['ok', 'register'],
      setup(_, { slots }) {
        return () => h('div', { 'data-test': 'basic-drawer' }, slots.default?.());
      },
    }),
    useDrawerInner: (init: any) => {
      drawerState.init = init;
      return [
        vi.fn(),
        {
          changeLoading: drawerActions.changeLoading,
          changeOkLoading: drawerActions.changeOkLoading,
          closeDrawer: drawerActions.close,
        },
      ];
    },
  };
});
 
vi.mock('@jnpf/ui/modal', async () => {
  const { defineComponent, h } = await import('vue');
  return {
    BasicModal: defineComponent({
      name: 'BasicModal',
      emits: ['ok', 'register'],
      setup(_, { slots }) {
        return () => h('div', { 'data-test': 'basic-modal' }, slots.default?.());
      },
    }),
    useModal: () => [vi.fn(), { closeModal: modalActions.close, openModal: modalActions.open }],
  };
});
 
vi.mock('vuedraggable', async () => {
  const { defineComponent, h } = await import('vue');
  return {
    default: defineComponent({
      name: 'DraggableStub',
      inheritAttrs: false,
      props: { disabled: Boolean, modelValue: { type: Array, default: () => [] } },
      emits: ['update:modelValue'],
      setup(props, { attrs, slots }) {
        return () =>
          h(
            'div',
            { ...attrs, 'data-disabled': String(props.disabled) },
            props.modelValue.map((element, index) => slots.item?.({ element, index })),
          );
      },
    }),
  };
});
 
function mountDrawer() {
  return mount(StepSettingsDrawer, {
    global: {
      stubs: {
        'a-button': ButtonStub,
        'a-form-item': defineComponent({ name: 'AFormItem', template: '<div><slot /></div>' }),
        'a-input': InputStub,
        'a-select': SelectStub,
        'a-tooltip': defineComponent({ name: 'ATooltip', template: '<span><slot /></span>' }),
      },
    },
  });
}
 
describe('eLN template step settings drawer', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    apiMocks.getTemplateSteps.mockResolvedValue({ data: structuredClone(configuration) });
    apiMocks.saveTemplateSteps.mockResolvedValue({ data: structuredClone(configuration) });
  });
 
  it('标题展示模板编号和模板版本', async () => {
    const wrapper = mountDrawer();
 
    await drawerState.init({
      version: { bizEnabled: 'enabled', bizStatus: 'DRAFT', id: 'template-1', mubanBanben: 'R01', mubanBianhao: 'TMP-001' },
    });
    await wrapper.vm.$nextTick();
 
    expect(wrapper.findComponent({ name: 'BasicDrawer' }).props('title')).toBe('步骤设置 - TMP-001 - R01');
  });
 
  it('加载已发布版本时按顺序只读展示', async () => {
    const wrapper = mountDrawer();
 
    await drawerState.init({
      version: { bizEnabled: 'enabled', bizStatus: 'PUBLISHED', id: 'template-1', mubanBanben: 'R01', mubanBianhao: 'TMP-001' },
    });
    await wrapper.vm.$nextTick();
 
    expect(apiMocks.getTemplateSteps).toHaveBeenCalledWith('template-1');
    expect(drawerActions.changeLoading.mock.calls).toEqual([[true], [false]]);
    expect(wrapper.findComponent({ name: 'BasicDrawer' }).props('showFooter')).toBe(false);
    expect(wrapper.findComponent('[data-test="step-list"]').attributes('data-disabled')).toBe('true');
    expect(wrapper.find('[data-test="step-list"]').text().indexOf('准备样品')).toBeLessThan(wrapper.find('[data-test="step-list"]').text().indexOf('记录结果'));
  });
 
  it('按拖动顺序保存步骤', async () => {
    const wrapper = mountDrawer();
    await drawerState.init({
      version: { bizEnabled: 'enabled', bizStatus: 'DRAFT', id: 'template-1', mubanBanben: 'R01', mubanBianhao: 'TMP-001' },
    });
    await wrapper.vm.$nextTick();
 
    const stepList = wrapper.findComponent({ name: 'DraggableStub' });
    stepList.vm.$emit('update:modelValue', [...(stepList.props('modelValue') as any[])].reverse());
    await wrapper.vm.$nextTick();
 
    wrapper.findComponent({ name: 'BasicDrawer' }).vm.$emit('ok');
    await flushPromises();
 
    expect(apiMocks.saveTemplateSteps).toHaveBeenCalledWith('template-1', {
      stepMigrations: {},
      steps: [
        { id: 'step-2', name: '记录结果' },
        { id: 'step-1', name: '准备样品' },
      ],
    });
    expect(drawerActions.changeOkLoading.mock.calls).toEqual([[true], [false]]);
    expect(drawerActions.close).toHaveBeenCalledOnce();
    expect(wrapper.emitted('saved')).toEqual([['template-1']]);
  });
 
  it('删除已有步骤时记录迁移目标', async () => {
    const wrapper = mountDrawer();
    await drawerState.init({
      version: { bizEnabled: 'enabled', bizStatus: 'DRAFT', id: 'template-1', mubanBanben: 'R01', mubanBianhao: 'TMP-001' },
    });
    await wrapper.vm.$nextTick();
 
    await wrapper.find('[data-test="add-step"]').trigger('click');
    await wrapper.find('[data-test="delete-step-step-1"]').trigger('click');
    expect(modalActions.open).toHaveBeenCalledWith(true);
    const migrationTarget = wrapper.findAllComponents(SelectStub).find((item) => item.attributes('data-test') === 'migration-target')!;
    expect(migrationTarget.props('options')).toEqual([{ label: '记录结果', value: 'step-2' }]);
    migrationTarget.vm.$emit('update:value', 'step-2');
    await wrapper.vm.$nextTick();
    wrapper.findComponent({ name: 'BasicModal' }).vm.$emit('ok');
    await wrapper.vm.$nextTick();
    await wrapper.find('[data-test="delete-step-local-step-1"]').trigger('click');
    wrapper.findComponent({ name: 'BasicDrawer' }).vm.$emit('ok');
    await flushPromises();
 
    expect(apiMocks.saveTemplateSteps).toHaveBeenCalledWith(
      'template-1',
      expect.objectContaining({
        stepMigrations: { 'step-1': 'step-2' },
        steps: [{ id: 'step-2', name: '记录结果' }],
      }),
    );
  });
 
  it('保存失败后恢复按钮 loading 并保持抽屉打开', async () => {
    apiMocks.saveTemplateSteps.mockRejectedValueOnce(new Error('保存失败'));
    const wrapper = mountDrawer();
    await drawerState.init({
      version: { bizEnabled: 'enabled', bizStatus: 'DRAFT', id: 'template-1', mubanBanben: 'R01', mubanBianhao: 'TMP-001' },
    });
    await wrapper.vm.$nextTick();
 
    wrapper.findComponent({ name: 'BasicDrawer' }).vm.$emit('ok');
    await flushPromises();
 
    expect(messageMocks.error).toHaveBeenCalledWith('保存失败');
    expect(drawerActions.changeOkLoading.mock.calls).toEqual([[true], [false]]);
    expect(drawerActions.close).not.toHaveBeenCalled();
  });
});