Files
grafana/public/app/plugins/datasource/prometheus/querybuilder/components/PromQueryBuilderOptions.test.tsx
T
Torkel ÖdegaardandIvana Huckova cfa24a3cfb Prometheus: Auto legend handling (#45367)
* Legend editor is working

* It's working

* Progress on auto legend mode

* Fixes

* added unit tests

* Added go tests

* Fixing tests

* Fix issue with timing and internal state

* Update public/app/plugins/datasource/prometheus/querybuilder/components/PromQueryCodeEditor.tsx

Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com>
2022-02-16 14:06:33 +01:00

77 lines
2.1 KiB
TypeScript

import React from 'react';
import { render, screen } from '@testing-library/react';
import { PromQuery } from '../../types';
import { getQueryWithDefaults } from '../types';
import { CoreApp } from '@grafana/data';
import { PromQueryBuilderOptions } from './PromQueryBuilderOptions';
import { selectOptionInTest } from '@grafana/ui';
describe('PromQueryBuilderOptions', () => {
it('Can change query type', async () => {
const { props } = setup();
screen.getByTitle('Click to edit options').click();
expect(screen.getByLabelText('Range')).toBeChecked();
screen.getByLabelText('Instant').click();
expect(props.onChange).toHaveBeenCalledWith({
...props.query,
instant: true,
range: false,
exemplar: false,
});
});
it('Legend format default to Auto', async () => {
setup();
expect(screen.getByText('Legend: Auto')).toBeInTheDocument();
});
it('Can change legend format to verbose', async () => {
const { props } = setup();
screen.getByTitle('Click to edit options').click();
let legendModeSelect = screen.getByText('Auto').parentElement!;
legendModeSelect.click();
await selectOptionInTest(legendModeSelect as HTMLElement, 'Verbose');
expect(props.onChange).toHaveBeenCalledWith({
...props.query,
legendFormat: '',
});
});
it('Can change legend format to custom', async () => {
const { props } = setup();
screen.getByTitle('Click to edit options').click();
let legendModeSelect = screen.getByText('Auto').parentElement!;
legendModeSelect.click();
await selectOptionInTest(legendModeSelect as HTMLElement, 'Custom');
expect(props.onChange).toHaveBeenCalledWith({
...props.query,
legendFormat: '{{label_name}}',
});
});
});
function setup(queryOverrides: Partial<PromQuery> = {}) {
const props = {
query: {
...getQueryWithDefaults({ refId: 'A' } as PromQuery, CoreApp.PanelEditor),
queryOverrides,
},
onRunQuery: jest.fn(),
onChange: jest.fn(),
};
const { container } = render(<PromQueryBuilderOptions {...props} />);
return { container, props };
}