import { mount } from '@vue/test-utils';
import { beforeEach, describe, it, vi } from 'vitest';
import { nextTick } from 'vue';
import UiTooltip from './UiTooltip.vue';

let spy = vi.fn();
vi.mock('@/components/feedback/tooltip.helper', () => ({
  useTooltipHelper: vi.fn(() => ({
    getTooltipTranslatePosition: spy.mockImplementation(() => {
      return { translateX: 0, translateY: 0 };
    }),
  })),
}));

let props = {
  visible: false,
  tooltipText: 'this is a longer tooltip',
  direction: 'bottom',
  getAnchorElementValues: () => ({
    top: 50,
    left: 50,
    width: 100,
    height: 100,
  }),
} as any;
const factory = () =>
  mount(UiTooltip, {
    props: props,
  });

describe('UiTooltip', () => {
  beforeEach(() => {
    vi.resetAllMocks();
  });
  it('should not calculate position of tooltip if tooltip not visible', async () => {
    factory();
    await nextTick();
    expect(spy).not.toHaveBeenCalled();
  });
  it('should calculate position of tooltip', async () => {
    const wrapper = factory();
    await nextTick();
    wrapper.setProps({ ...props, visible: true });
    await nextTick();
    await nextTick();
    expect(spy).toHaveBeenCalled();
  });
  it('should calculate position of tooltip only once', async () => {
    const wrapper = factory();
    await nextTick();
    wrapper.setProps({ ...props, visible: true });
    await nextTick();
    await nextTick();
    wrapper.setProps({ ...props, visible: false });
    await nextTick();
    await nextTick();
    wrapper.setProps({ ...props, visible: true });
    await nextTick();
    await nextTick();
    expect(spy).toHaveBeenCalledOnce();
  });
});
