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

import { describe, expect, it } from 'vitest';

import { EasyFlowPanelModal } from '..';

describe('easyFlowPanelModal', () => {
  it('renders title, description and body content when opened', async () => {
    const wrapper = mount(EasyFlowPanelModal, {
      global: {
        stubs: {
          teleport: true,
        },
      },
      props: {
        cancelText: '取消',
        confirmText: '确认',
        description: '描述信息',
        open: true,
        title: '面板弹窗',
      },
      slots: {
        default: '<div>弹窗内容</div>',
      },
    });

    await nextTick();
    await nextTick();

    expect(wrapper.text()).toContain('面板弹窗');
    expect(wrapper.text()).toContain('描述信息');
    expect(wrapper.text()).toContain('弹窗内容');
  });

  it('supports default footer actions and hide footer', async () => {
    const wrapper = mount(EasyFlowPanelModal, {
      global: {
        stubs: {
          teleport: true,
        },
      },
      props: {
        cancelText: '取消',
        confirmText: '确认',
        description: '描述信息',
        open: true,
        title: '面板弹窗',
      },
      slots: {
        default: '<div>弹窗内容</div>',
      },
    });

    await nextTick();
    await nextTick();

    const buttons = wrapper.findAll('button');
    const cancelButton = buttons.find((button) => button.text() === '取消');
    const confirmButton = buttons.find((button) => button.text() === '确认');

    await cancelButton?.trigger('click');
    await confirmButton?.trigger('click');

    expect(wrapper.emitted('cancel')).toHaveLength(1);
    expect(wrapper.emitted('confirm')).toHaveLength(1);
    expect(wrapper.emitted('update:open')?.at(-1)).toEqual([false]);

    await wrapper.setProps({ showFooter: false });
    await nextTick();

    expect(wrapper.text()).not.toContain('取消');
    expect(wrapper.text()).not.toContain('确认');
  });
});
