刘光辉
5 小时以前 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
/* @vitest-environment happy-dom */
 
import { flushPromises, mount } from '@vue/test-utils';
 
import { beforeEach, describe, expect, it, vi } from 'vitest';
 
import routesSource from '../../../../../router/routes/basic.ts?raw';
import SourcePathPage from '../index.vue';
import pageSource from '../index.vue?raw';
 
const apiMocks = vi.hoisted(() => ({ getSourcePaths: vi.fn() }));
const drawerMocks = vi.hoisted(() => ({ openDrawer: vi.fn() }));
 
vi.mock('#/api/x/eln/sources', () => apiMocks);
vi.mock('@jnpf/ui/drawer', () => ({ useDrawer: () => [vi.fn(), { openDrawer: drawerMocks.openDrawer }] }));
vi.mock('@jnpf/ui/form', () => ({
  BasicForm: { name: 'BasicForm', template: '<form />' },
  useForm: () => [vi.fn(), { getFieldsValue: vi.fn(() => ({})), resetFields: vi.fn() }],
}));
vi.mock('@jnpf/ui/vxeTable', () => ({
  BasicVxeTable: {
    name: 'BasicVxeTable',
    props: { dataSource: { default: () => [], type: Array } },
    template:
      '<div><slot name="tableTitle" /><div v-for="record in dataSource" :key="record.id"><span>{{ record.displayName }}</span><span>{{ record.path }}</span><slot name="status" :record="record" /></div></div>',
  },
  useVxeTable: () => [vi.fn(), { setLoading: vi.fn() }],
}));
vi.mock('../SourcePathDrawer.vue', () => ({
  default: { emits: ['success'], name: 'SourcePathDrawer', template: '<aside data-test="source-path-drawer" />' },
}));
 
const path = {
  code: 'request',
  displayName: '所属请验单',
  editVersion: 1,
  id: 'path-1',
  outputs: [],
  path: '检验项 > 请验单 > 生产日期',
  revision: 1,
  scope: '全部检验项',
  status: 'PUBLISHED',
};
 
function mountPage() {
  return mount(SourcePathPage, {
    global: {
      stubs: {
        'a-alert': { template: '<div><slot /><slot name="action" /></div>' },
        'a-button': { emits: ['click'], template: '<button @click="$emit(\'click\')"><slot /></button>' },
        'a-tag': { template: '<span><slot /></span>' },
      },
    },
  });
}
 
describe('来源路径管理页面', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    apiMocks.getSourcePaths.mockResolvedValue({ data: [path] });
  });
 
  it('使用标准列表结构独立加载来源路径', async () => {
    const wrapper = mountPage();
    await flushPromises();
 
    expect(apiMocks.getSourcePaths).toHaveBeenCalledTimes(1);
    expect(wrapper.text()).toContain('所属请验单');
    expect(pageSource).toContain('jnpf-content-wrapper-search-box');
    expect(pageSource).toContain('jnpf-content-wrapper-content');
    expect(pageSource).toContain('<BasicVxeTable');
  });
 
  it('新增成功后刷新列表', async () => {
    const wrapper = mountPage();
    await flushPromises();
 
    await wrapper.get('[data-test="new-source-path"]').trigger('click');
    expect(drawerMocks.openDrawer).toHaveBeenCalledWith(true);
 
    wrapper.findComponent({ name: 'SourcePathDrawer' }).vm.$emit('success');
    await flushPromises();
    expect(apiMocks.getSourcePaths).toHaveBeenCalledTimes(2);
  });
 
  it('注册独立隐藏路由', () => {
    expect(routesSource).toContain("path: '/eln/source-paths'");
    expect(routesSource).toContain("name: 'elnSourcePaths'");
    expect(routesSource).toContain("title: '来源路径管理'");
  });
});