2020-10-14 13:08:35 +03:00
|
|
|
import { fireEvent, render, screen } from '@testing-library/react';
|
|
|
|
|
import userEvent from '@testing-library/user-event';
|
2022-04-22 14:33:13 +01:00
|
|
|
import React from 'react';
|
|
|
|
|
|
2020-10-14 13:08:35 +03:00
|
|
|
import { QueryEditor, Props } from './QueryEditor';
|
|
|
|
|
import { scenarios } from './__mocks__/scenarios';
|
2022-04-22 14:33:13 +01:00
|
|
|
import { defaultQuery } from './constants';
|
2023-01-26 16:31:34 +01:00
|
|
|
import { TestDataQueryType } from './dataquery.gen';
|
2023-02-09 09:03:13 +00:00
|
|
|
import { TestDataDataSource } from './datasource';
|
2021-03-29 07:51:15 +02:00
|
|
|
import { defaultStreamQuery } from './runStreams';
|
2020-10-14 13:08:35 +03:00
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
jest.clearAllMocks();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const mockOnChange = jest.fn();
|
|
|
|
|
const props = {
|
|
|
|
|
onRunQuery: jest.fn(),
|
|
|
|
|
query: defaultQuery,
|
|
|
|
|
onChange: mockOnChange,
|
|
|
|
|
datasource: {
|
|
|
|
|
getScenarios: () => Promise.resolve(scenarios),
|
2023-02-09 09:03:13 +00:00
|
|
|
} as TestDataDataSource,
|
2020-10-14 13:08:35 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const setup = (testProps?: Partial<Props>) => {
|
|
|
|
|
const editorProps = { ...props, ...testProps };
|
|
|
|
|
return render(<QueryEditor {...editorProps} />);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
describe('Test Datasource Query Editor', () => {
|
|
|
|
|
it('should render with default scenario', async () => {
|
|
|
|
|
setup();
|
|
|
|
|
|
|
|
|
|
expect(await screen.findByText(/random walk/i)).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByRole('textbox', { name: 'Alias' })).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByRole('textbox', { name: 'Labels' })).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should switch scenario and display its default values', async () => {
|
|
|
|
|
const { rerender } = setup();
|
|
|
|
|
|
2022-01-12 09:19:10 +00:00
|
|
|
let select = (await screen.findByText('Scenario')).nextSibling!.firstChild!;
|
2020-10-14 13:08:35 +03:00
|
|
|
await fireEvent.keyDown(select, { keyCode: 40 });
|
|
|
|
|
const scs = screen.getAllByLabelText('Select option');
|
|
|
|
|
|
|
|
|
|
expect(scs).toHaveLength(scenarios.length);
|
|
|
|
|
|
|
|
|
|
await userEvent.click(screen.getByText('CSV Metric Values'));
|
2023-01-26 16:31:34 +01:00
|
|
|
expect(mockOnChange).toHaveBeenCalledWith(
|
|
|
|
|
expect.objectContaining({ scenarioId: TestDataQueryType.CSVMetricValues })
|
|
|
|
|
);
|
2020-10-14 13:08:35 +03:00
|
|
|
await rerender(
|
|
|
|
|
<QueryEditor
|
|
|
|
|
{...props}
|
2023-01-26 16:31:34 +01:00
|
|
|
query={{ ...defaultQuery, scenarioId: TestDataQueryType.CSVMetricValues, stringInput: '1,20,90,30,5,0' }}
|
2020-10-14 13:08:35 +03:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
expect(await screen.findByRole('textbox', { name: /string input/i })).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByRole('textbox', { name: /string input/i })).toHaveValue('1,20,90,30,5,0');
|
|
|
|
|
|
|
|
|
|
await fireEvent.keyDown(select, { keyCode: 40 });
|
|
|
|
|
await userEvent.click(screen.getByText('Grafana API'));
|
|
|
|
|
expect(mockOnChange).toHaveBeenCalledWith(
|
|
|
|
|
expect.objectContaining({ scenarioId: 'grafana_api', stringInput: 'datasources' })
|
|
|
|
|
);
|
|
|
|
|
rerender(
|
2023-01-26 16:31:34 +01:00
|
|
|
<QueryEditor
|
|
|
|
|
{...props}
|
|
|
|
|
query={{ ...defaultQuery, scenarioId: TestDataQueryType.GrafanaAPI, stringInput: 'datasources' }}
|
|
|
|
|
/>
|
2020-10-14 13:08:35 +03:00
|
|
|
);
|
|
|
|
|
expect(await screen.findByText('Grafana API')).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByText('Data Sources')).toBeInTheDocument();
|
|
|
|
|
|
|
|
|
|
await fireEvent.keyDown(select, { keyCode: 40 });
|
|
|
|
|
await userEvent.click(screen.getByText('Streaming Client'));
|
|
|
|
|
expect(mockOnChange).toHaveBeenCalledWith(
|
2021-03-29 07:51:15 +02:00
|
|
|
expect.objectContaining({ scenarioId: 'streaming_client', stream: defaultStreamQuery })
|
2020-10-14 13:08:35 +03:00
|
|
|
);
|
2021-03-29 07:51:15 +02:00
|
|
|
|
2023-01-26 16:31:34 +01:00
|
|
|
const streamQuery = { ...defaultQuery, stream: defaultStreamQuery, scenarioId: TestDataQueryType.StreamingClient };
|
2021-03-29 07:51:15 +02:00
|
|
|
|
|
|
|
|
rerender(<QueryEditor {...props} query={streamQuery} />);
|
|
|
|
|
|
2020-10-14 13:08:35 +03:00
|
|
|
expect(await screen.findByText('Streaming Client')).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByText('Type')).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByLabelText('Noise')).toHaveValue(2.2);
|
|
|
|
|
expect(screen.getByLabelText('Speed (ms)')).toHaveValue(250);
|
|
|
|
|
expect(screen.getByLabelText('Spread')).toHaveValue(3.5);
|
|
|
|
|
expect(screen.getByLabelText('Bands')).toHaveValue(1);
|
|
|
|
|
});
|
2022-01-19 13:04:35 +00:00
|
|
|
|
|
|
|
|
it('persists the datasource from the query when switching scenario', async () => {
|
|
|
|
|
const mockDatasource = {
|
|
|
|
|
type: 'test',
|
|
|
|
|
uid: 'foo',
|
|
|
|
|
};
|
|
|
|
|
setup({
|
|
|
|
|
query: {
|
|
|
|
|
...defaultQuery,
|
|
|
|
|
datasource: mockDatasource,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
let select = (await screen.findByText('Scenario')).nextSibling!.firstChild!;
|
|
|
|
|
await fireEvent.keyDown(select, { keyCode: 40 });
|
|
|
|
|
await userEvent.click(screen.getByText('Grafana API'));
|
|
|
|
|
expect(mockOnChange).toHaveBeenCalledWith(expect.objectContaining({ datasource: mockDatasource }));
|
|
|
|
|
});
|
2020-10-14 13:08:35 +03:00
|
|
|
});
|