刘光辉
6 小时以前 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
import { config, flushPromises, mount } from '@vue/test-utils';
 
import { beforeEach, describe, expect, it, vi } from 'vitest';
 
import OnlyOfficeCompareEditor from '../OnlyOfficeCompareEditor.vue';
 
const mocks = vi.hoisted(() => ({ load: vi.fn(), destroy: vi.fn(), compare: vi.fn(), config: undefined as any }));
vi.mock('#/components/OnlyOfficeEditor/src/loadDocsApi', () => ({ loadDocsApi: mocks.load }));
 
const session = () => ({
  config: { token: 'signed-config', editorConfig: { mode: 'view' }, document: { key: 'isolated-key', permissions: { edit: false, review: false } } },
  expiresAt: Date.now() + 60_000,
  title: 'v1 → v3',
});
 
beforeEach(() => {
  config.global.stubs = { 'a-alert': true, 'a-spin': true };
  vi.clearAllMocks();
  mocks.load.mockResolvedValue(undefined);
  (window as any).DocsAPI = {
    // eslint-disable-next-line prefer-arrow-callback -- DocsAPI is invoked as a constructor.
    DocEditor: vi.fn(function (_id: string, config: any) {
      mocks.config = config;
      return { destroyEditor: mocks.destroy, setRequestedDocument: mocks.compare };
    }),
  };
});
 
describe('isolated comparison editor', () => {
  it('opens only the readonly result and destroys the session on close', async () => {
    const data = session();
    const wrapper = mount(OnlyOfficeCompareEditor, { props: { session: data } });
    await flushPromises();
    expect(mocks.compare).not.toHaveBeenCalled();
    expect(mocks.config.token).toBe('signed-config');
    mocks.config.events.onDocumentReady();
    mocks.config.events.onDocumentReady();
    expect(mocks.config.events.onRequestSelectDocument).toBeUndefined();
    expect(mocks.config.editorConfig.mode).toBe('view');
    expect(mocks.config.document.permissions).toEqual({ edit: false, review: false });
    expect(mocks.compare).not.toHaveBeenCalled();
    wrapper.unmount();
    expect(mocks.destroy).toHaveBeenCalledOnce();
    mocks.config.events.onDocumentReady();
    expect(mocks.compare).not.toHaveBeenCalled();
  });
  it('does not open an expired result ticket', async () => {
    const wrapper = mount(OnlyOfficeCompareEditor, { props: { session: { ...session(), expiresAt: 0 } } });
    await flushPromises();
    expect((window as any).DocsAPI.DocEditor).not.toHaveBeenCalled();
    await flushPromises();
    expect(wrapper.html()).toContain('比较会话已过期');
    wrapper.unmount();
  });
  it('rejects old editable comparison configs instead of showing an editor', async () => {
    const data = session();
    data.config.editorConfig.mode = 'edit';
    const wrapper = mount(OnlyOfficeCompareEditor, { props: { session: data } });
    await flushPromises();
    expect((window as any).DocsAPI.DocEditor).not.toHaveBeenCalled();
    expect(wrapper.html()).toContain('比较窗口仅允许只读查看');
    wrapper.unmount();
  });
  it('does not mount an editor after the user closes during API loading', async () => {
    let release!: () => void;
    mocks.load.mockReturnValue(
      new Promise<void>((resolve) => {
        release = resolve;
      }),
    );
    const wrapper = mount(OnlyOfficeCompareEditor, { props: { session: session() } });
    wrapper.unmount();
    release();
    await flushPromises();
    expect((window as any).DocsAPI.DocEditor).not.toHaveBeenCalled();
  });
});