import { render, screen } from '@testing-library/react';
import { openMenu, select } from 'react-select-event';

import { QueryType } from '../dataquery.gen';
import { createMockQuery } from '../mocks/cloudMonitoringQuery';

import { QueryHeader } from './QueryHeader';

describe('QueryHeader', () => {
  it('can change query types to SLO', async () => {
    const query = createMockQuery();
    const onChange = jest.fn();
    const onRunQuery = jest.fn();

    render(<QueryHeader query={query} onChange={onChange} onRunQuery={onRunQuery} />);

    const queryType = screen.getByLabelText(/Query type/);
    await openMenu(queryType);
    await select(screen.getByLabelText('Select options menu'), 'Service Level Objectives (SLO)');
    expect(onChange).toBeCalledWith(expect.objectContaining({ queryType: QueryType.SLO }));
  });

  it('can change query types to MQL', async () => {
    const query = createMockQuery();
    const onChange = jest.fn();
    const onRunQuery = jest.fn();

    render(<QueryHeader query={query} onChange={onChange} onRunQuery={onRunQuery} />);

    const queryType = screen.getByLabelText(/Query type/);
    await openMenu(queryType);
    await select(screen.getByLabelText('Select options menu'), 'MQL');
    expect(onChange).toBeCalledWith(expect.objectContaining({ queryType: QueryType.TIME_SERIES_QUERY }));
  });

  it('can change query types to PromQL', async () => {
    const query = createMockQuery();
    const onChange = jest.fn();
    const onRunQuery = jest.fn();

    render(<QueryHeader query={query} onChange={onChange} onRunQuery={onRunQuery} />);

    const queryType = screen.getByLabelText(/Query type/);
    await openMenu(queryType);
    await select(screen.getByLabelText('Select options menu'), 'PromQL');
    expect(onChange).toBeCalledWith(expect.objectContaining({ queryType: QueryType.PROMQL }));
  });
});
