刘光辉
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* @vitest-environment happy-dom */
 
import { mount } from '@vue/test-utils';
import { defineComponent, h, KeepAlive, nextTick, ref } from 'vue';
 
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
 
import Editor from '../Editor.vue';
 
const mocks = vi.hoisted(() => ({
  destroyEditor: vi.fn(),
  editorConfig: undefined as Record<string, any> | undefined,
  grabFocus: vi.fn(),
  requestClose: vi.fn(),
}));
 
vi.mock('../loadDocsApi', () => ({ loadDocsApi: vi.fn().mockResolvedValue(undefined) }));
 
describe('onlyOfficeEditor', () => {
  beforeEach(() => {
    mocks.destroyEditor.mockClear();
    mocks.grabFocus.mockClear();
    mocks.requestClose.mockClear();
    mocks.editorConfig = undefined;
    (window as any).DocsAPI = {
      DocEditor: vi.fn((_containerId: string, config: Record<string, any>) => {
        mocks.editorConfig = config;
        return { destroyEditor: mocks.destroyEditor, grabFocus: mocks.grabFocus, requestClose: mocks.requestClose };
      }),
    };
  });
 
  afterEach(() => {
    delete (window as any).DocsAPI;
  });
 
  it('将宿主关闭请求转发给 ONLYOFFICE 编辑器', async () => {
    const wrapper = mount(Editor, { props: { config: { document: { key: 'key-1' } } } });
    await nextTick();
    await nextTick();
 
    wrapper.vm.requestClose();
 
    expect(mocks.requestClose).toHaveBeenCalledTimes(1);
    wrapper.unmount();
  });
 
  it('恢复焦点使用文档API,销毁后不再操作旧编辑器', async () => {
    const wrapper = mount(Editor, { props: { config: { document: { key: 'key-1' } } } });
    await nextTick();
    await nextTick();
    const grabFocus = wrapper.vm.grabFocus;
 
    grabFocus();
    expect(mocks.grabFocus).toHaveBeenCalledTimes(1);
    expect(mocks.destroyEditor).not.toHaveBeenCalled();
 
    wrapper.unmount();
    grabFocus();
    expect(mocks.grabFocus).toHaveBeenCalledTimes(1);
  });
 
  it('文档由已修改恢复为未修改时只报告编辑状态', async () => {
    const wrapper = mount(Editor, { props: { config: { document: { key: 'key-1' } } } });
    await nextTick();
    await nextTick();
 
    mocks.editorConfig?.events.onDocumentStateChange({ data: true });
    mocks.editorConfig?.events.onDocumentStateChange({ data: false });
 
    expect(wrapper.emitted('modified')).toEqual([[true], [false]]);
    expect(wrapper.emitted('save')).toBeUndefined();
    expect(mocks.editorConfig?.events.onSaveDocument).toBeUndefined();
    wrapper.unmount();
  });
 
  it('切换 KeepAlive 缓存页时销毁 ONLYOFFICE 编辑器', async () => {
    const editorActive = ref(true);
    const OtherPage = defineComponent({ name: 'OtherPage', setup: () => () => h('div', '其他页面') });
    const Host = defineComponent({
      setup: () => () =>
        h(KeepAlive, null, {
          default: () => (editorActive.value ? h(Editor, { config: { document: { key: 'key-1' } } }) : h(OtherPage)),
        }),
    });
    const wrapper = mount(Host);
    await nextTick();
    await nextTick();
 
    expect((window as any).DocsAPI.DocEditor).toHaveBeenCalledTimes(1);
    expect(mocks.destroyEditor).not.toHaveBeenCalled();
 
    editorActive.value = false;
    await nextTick();
 
    expect(mocks.destroyEditor).toHaveBeenCalledTimes(1);
 
    editorActive.value = true;
    await nextTick();
    await nextTick();
 
    expect((window as any).DocsAPI.DocEditor).toHaveBeenCalledTimes(2);
    expect(mocks.destroyEditor).toHaveBeenCalledTimes(1);
    wrapper.unmount();
  });
});