import { fireEvent, render } from '@testing-library/react';
import React from 'react';
import { describe, expect, it, vi } from 'vitest';
import { ScrollColumn } from './ScrollColumn';

describe('ScrollColumn', () => {
  it('renders the component without crashing', () => {
    const { container } = render(
      <ScrollColumn items={[]} selectedItem={null} />,
    );

    expect(container).toBeTruthy();
  });

  it('activates when the isActive prop is set to true', () => {
    const { container } = render(
      <ScrollColumn items={[]} selectedItem={null} isActive={true} />,
    );

    const scroller = container.querySelector('.scroller');

    expect(scroller).toBeTruthy();
  });

  it('displays the inactive title when isActive is false', () => {
    const titleContent = 'test';
    const { container } = render(
      <ScrollColumn
        items={[]}
        selectedItem={null}
        isActive={false}
        inActiveTitle={titleContent}
      />,
    );

    const inActiveTitle = container.querySelector('.inactive-title');

    expect(inActiveTitle).toBeTruthy();
    expect(inActiveTitle?.textContent).toBe(titleContent);
  });

  it('fires onSelected event when an item is selected', () => {
    const itemContent = 'test';
    const selectedSpy = vi.fn();
    const { container } = render(
      <ScrollColumn
        items={[itemContent]}
        selectedItem={null}
        isActive={true}
        onSelected={selectedSpy}
      />,
    );

    const item = container.querySelector('.item') as HTMLElement;

    fireEvent.click(item);

    expect(selectedSpy).toHaveBeenCalled();
  });

  it('fires onSelected event when an item is selected through the wheel', () => {
    const itemContent = 'test';
    const selectedSpy = vi.fn();
    const { container } = render(
      <ScrollColumn
        items={[itemContent]}
        selectedItem={null}
        isActive={true}
        onSelected={selectedSpy}
      />,
    );

    const item = container.querySelector('.item') as HTMLElement;

    fireEvent.wheel(item, { deltaY: 1 });

    expect(selectedSpy).toHaveBeenCalled();
  });
});
