/* @vitest-environment happy-dom */

import { mount } from '@vue/test-utils';
import { nextTick } from 'vue';

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import Editor from '../Editor.vue';

const mocks = vi.hoisted(() => ({
  destroyEditor: vi.fn(),
  requestClose: vi.fn(),
}));

vi.mock('../loadDocsApi', () => ({ loadDocsApi: vi.fn().mockResolvedValue(undefined) }));

describe('OnlyOfficeEditor', () => {
  beforeEach(() => {
    mocks.destroyEditor.mockClear();
    mocks.requestClose.mockClear();
    (window as any).DocsAPI = {
      DocEditor: vi.fn(() => ({ destroyEditor: mocks.destroyEditor, 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();
  });
});
