刘光辉
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
import { describe, expect, it, vi } from 'vitest';
 
import { bindPluginReady } from '../../src/onlyoffice/lifecycle';
 
describe('签批页标注 OnlyOffice 生命周期', () => {
  it('等待 OnlyOffice init 后再读取运行上下文', () => {
    const ready = vi.fn();
    const originalInit = vi.fn();
    const plugin: { info?: { options?: unknown }; init?: (...args: unknown[]) => void } = { init: originalInit };
 
    bindPluginReady({ plugin }, ready);
    expect(ready).not.toHaveBeenCalled();
 
    plugin.info = { options: { ticket: 'ticket' } };
    plugin.init?.('init-data');
 
    expect(originalInit).toHaveBeenCalledWith('init-data');
    expect(ready).toHaveBeenCalledTimes(1);
  });
 
  it('在 options 已存在时只初始化一次,并在清理时恢复 init', () => {
    const ready = vi.fn();
    const originalInit = vi.fn();
    const plugin: { info?: { options?: unknown }; init?: (...args: unknown[]) => void } = {
      info: { options: { ticket: 'ticket' } },
      init: originalInit,
    };
 
    const unbind = bindPluginReady({ plugin }, ready);
    const registeredInit = plugin.init;
    registeredInit?.();
    unbind();
 
    expect(ready).toHaveBeenCalledTimes(1);
    expect(plugin.init).toBe(originalInit);
  });
 
  it('将已存在的 JSON options 视为已初始化上下文', () => {
    const ready = vi.fn();
    const plugin: { info?: { options?: unknown }; init?: (...args: unknown[]) => void } = {
      info: { options: '{"ticket":"ticket"}' },
    };
 
    bindPluginReady({ plugin }, ready);
 
    expect(ready).toHaveBeenCalledTimes(1);
  });
});