刘光辉
3 小时以前 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
/* @vitest-environment happy-dom */
 
import { flushPromises, mount } from '@vue/test-utils';
 
import { beforeEach, describe, expect, it, vi } from 'vitest';
 
import ReportElnRecordsPane from '../ReportElnRecordsPane.vue';
 
const apiMocks = vi.hoisted(() => ({
  getOnlyOfficeConfig: vi.fn(),
  getRecordByTestWorkItem: vi.fn(),
  postInspectionProgressDetail: vi.fn(),
}));
 
vi.mock('#/api/x/onlyoffice', () => ({ getOnlyOfficeConfig: apiMocks.getOnlyOfficeConfig }));
vi.mock('#/api/x/eln/records', () => ({ getRecordByTestWorkItem: apiMocks.getRecordByTestWorkItem }));
vi.mock('#/api/x/lims/jianyanguanli/jianyan_jindu', () => ({
  postInspectionProgressDetail: apiMocks.postInspectionProgressDetail,
}));
vi.mock('#/components/OnlyOfficeEditor/src/Editor.vue', async () => {
  const { defineComponent, h } = await import('vue');
  return {
    default: defineComponent({
      name: 'OnlyOfficeEditor',
      props: { config: { required: true, type: Object } },
      setup() {
        return () => h('div', { 'data-test': 'eln-onlyoffice-editor' });
      },
    }),
  };
});
 
const record = (id: string) => ({
  data: {
    document: { fileExtension: 'docx', fileId: `file-${id}`, name: `${id}.docx` },
    existing: true,
    recordId: `record-${id}`,
    revisionId: `revision-${id}`,
  },
});
 
describe('report ELN records pane', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    apiMocks.postInspectionProgressDetail.mockResolvedValue({
      data: {
        project_detail: [
          { id: 'item-1', name: '性状', stages: [] },
          { id: 'item-2', name: '鉴别', stages: [] },
        ],
      },
    });
    apiMocks.getRecordByTestWorkItem.mockImplementation((id: string) => Promise.resolve(record(id)));
    apiMocks.getOnlyOfficeConfig.mockResolvedValue({
      data: { document: { key: 'key-1' }, editorConfig: { mode: 'view' }, token: 'token-1' },
    });
  });
 
  it('loads inspection items and opens the selected current ELN revision readonly', async () => {
    const wrapper = mount(ReportElnRecordsPane, {
      props: { tab: { key: 'elnRecords', qingyandanId: 'request-1' } },
    });
    await flushPromises();
 
    expect(apiMocks.postInspectionProgressDetail).toHaveBeenCalledWith({ qingyandan_id: 'request-1' });
    expect(apiMocks.getRecordByTestWorkItem).toHaveBeenCalledWith('item-1');
    expect(apiMocks.getOnlyOfficeConfig).toHaveBeenCalledWith(expect.objectContaining({ bizDataId: 'record-item-1', mode: 'view' }));
    expect(wrapper.find('[data-test="eln-onlyoffice-editor"]').exists()).toBe(true);
 
    await wrapper.get('[data-test="eln-project-item-2"]').trigger('click');
    await flushPromises();
 
    expect(apiMocks.getRecordByTestWorkItem).toHaveBeenCalledWith('item-2');
  });
 
  it('shows a non-mutating empty state when the selected item has no ELN record', async () => {
    apiMocks.getRecordByTestWorkItem.mockRejectedValue(new Error('检验项电子记录不存在'));
    const wrapper = mount(ReportElnRecordsPane, {
      props: { tab: { key: 'elnRecords', qingyandanId: 'request-1' } },
    });
    await flushPromises();
 
    expect(wrapper.get('[data-test="eln-record-error"]').text()).toContain('检验项电子记录不存在');
    expect(apiMocks.getOnlyOfficeConfig).not.toHaveBeenCalled();
  });
});