import { findVisibleSelectOption } from '../test-utils';

describe('findVisibleSelectOption', () => {
  afterEach(() => {
    document.body.innerHTML = '';
  });

  it('returns the matching option when a visible dropdown contains the option text', async () => {
    const dropdown = document.createElement('div');
    dropdown.className = 'ant-select-dropdown';

    const option1 = document.createElement('div');
    option1.className = 'ant-select-item-option';
    option1.textContent = 'Apple';

    const option2 = document.createElement('div');
    option2.className = 'ant-select-item-option';
    option2.textContent = 'Banana';

    dropdown.appendChild(option1);
    dropdown.appendChild(option2);
    document.body.appendChild(dropdown);

    const match = await findVisibleSelectOption('Banana');
    expect(match).toBe(option2);
  });

  it('skips dropdowns marked as hidden and returns the option from the visible one', async () => {
    const hiddenDropdown = document.createElement('div');
    hiddenDropdown.className = 'ant-select-dropdown ant-select-dropdown-hidden';
    const hiddenOption = document.createElement('div');
    hiddenOption.className = 'ant-select-item-option';
    hiddenOption.textContent = 'Cherry';
    hiddenDropdown.appendChild(hiddenOption);
    document.body.appendChild(hiddenDropdown);

    const visibleDropdown = document.createElement('div');
    visibleDropdown.className = 'ant-select-dropdown';
    const visibleOption = document.createElement('div');
    visibleOption.className = 'ant-select-item-option';
    visibleOption.textContent = 'Cherry';
    visibleDropdown.appendChild(visibleOption);
    document.body.appendChild(visibleDropdown);

    const match = await findVisibleSelectOption('Cherry');
    expect(match).toBe(visibleOption);
  });

  it('throws an error when the option text is not present in any visible dropdown', async () => {
    const dropdown = document.createElement('div');
    dropdown.className = 'ant-select-dropdown';
    const option = document.createElement('div');
    option.className = 'ant-select-item-option';
    option.textContent = 'Apple';
    dropdown.appendChild(option);
    document.body.appendChild(dropdown);

    await expect(findVisibleSelectOption('Mango')).rejects.toThrow(
      'Visible select option with text "Mango" not found',
    );
  });
});
