刘光辉
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
/* @vitest-environment happy-dom */
/* eslint-disable vue/one-component-per-file */
 
import type { DmsFileCenterItem } from '#/api/x/dms/fileCenter';
 
import { flushPromises, mount } from '@vue/test-utils';
 
import { beforeEach, describe, expect, it, vi } from 'vitest';
 
import FileOperationLogModal from '../FileOperationLogModal.vue';
 
const apiMocks = vi.hoisted(() => ({ getFileOperationLogs: vi.fn() }));
const messageMocks = vi.hoisted(() => ({ error: vi.fn() }));
const modalState = vi.hoisted(() => ({ init: undefined as ((data: { item: DmsFileCenterItem }) => void) | undefined }));
const tableState = vi.hoisted(() => ({ config: undefined as any, pagination: { current: 1, pageSize: 10 } }));
 
vi.mock('#/api/x/dms/fileCenter', () => ({ getFileOperationLogs: apiMocks.getFileOperationLogs }));
vi.mock('@jnpf/hooks', () => ({ useMessage: () => ({ createMessage: messageMocks }) }));
vi.mock('@jnpf/utils', () => ({ formatToDateTime: (value: string) => value }));
 
vi.mock('@jnpf/ui/modal', async () => {
  const { defineComponent, h } = await import('vue');
 
  return {
    BasicModal: defineComponent({
      name: 'BasicModal',
      setup:
        (_, { slots }) =>
        () =>
          h('section', slots.default?.()),
    }),
    useModalInner: (init: (data: { item: DmsFileCenterItem }) => void) => {
      modalState.init = init;
      return [vi.fn(), {}];
    },
  };
});
 
vi.mock('@jnpf/ui/vxeTable', async () => {
  const { defineComponent, h } = await import('vue');
 
  return {
    BasicVxeTable: defineComponent({
      name: 'BasicVxeTable',
      props: {
        dataSource: { default: () => [], type: Array },
        loading: Boolean,
        pagination: { default: undefined, type: [Boolean, Object] },
      },
      emits: ['change'],
      setup(props, { emit }) {
        return () =>
          h(
            'button',
            {
              'data-current-page': (props.pagination as any)?.current,
              'data-page-size': (props.pagination as any)?.pageSize,
              'data-record-count': props.dataSource.length,
              'data-test': 'operation-log-table',
              onClick: () => {
                tableState.pagination = { current: 2, pageSize: 20 };
                emit('change', { action: 'paginate' });
              },
              type: 'button',
            },
            '操作记录',
          );
      },
    }),
    useVxeTable: (config: any) => {
      tableState.config = config;
      return [vi.fn(), { getPaginationRef: () => tableState.pagination }];
    },
  };
});
 
const fileItem: DmsFileCenterItem = {
  createdAt: '2026-09-01T00:00:00Z',
  documentId: 'document-1',
  fileName: '质量手册.docx',
  id: 'version-1',
  name: '质量手册.docx',
  resourceType: 'FILE_VERSION',
  versionNo: 1,
};
 
describe('file operation log modal', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    tableState.pagination = { current: 1, pageSize: 10 };
    apiMocks.getFileOperationLogs.mockResolvedValue({
      data: {
        currentPage: 1,
        list: [{ id: 'log-1', occurredAt: '2026-09-20T08:00:00Z', operationContent: '查看文件' }],
        pageSize: 10,
        total: 21,
      },
    });
  });
 
  it('uses the standard table as a controlled paginated view', async () => {
    const wrapper = mount(FileOperationLogModal, {
      global: {
        stubs: {
          'a-alert': true,
          'a-button': true,
          'a-tooltip': true,
          'jnpf-input': true,
        },
      },
    });
 
    modalState.init?.({ item: fileItem });
    await flushPromises();
 
    expect(tableState.config).toMatchObject({ rowKey: 'id', showIndexColumn: false, showTableSetting: false });
    expect(apiMocks.getFileOperationLogs).toHaveBeenLastCalledWith(fileItem.id, {
      currentPage: 1,
      keyword: undefined,
      pageSize: 10,
    });
 
    const table = wrapper.get('[data-test="operation-log-table"]');
    expect(table.attributes('data-record-count')).toBe('1');
    expect(table.attributes('data-current-page')).toBe('1');
    expect(table.attributes('data-page-size')).toBe('10');
 
    apiMocks.getFileOperationLogs.mockResolvedValueOnce({
      data: { currentPage: 2, list: [], pageSize: 20, total: 21 },
    });
    await table.trigger('click');
    await flushPromises();
 
    expect(apiMocks.getFileOperationLogs).toHaveBeenLastCalledWith(fileItem.id, {
      currentPage: 2,
      keyword: undefined,
      pageSize: 20,
    });
 
    wrapper.unmount();
  });
});