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);
|
});
|
});
|