Files
grafana/public/app/plugins/datasource/prometheus/querybuilder/components/MetricSelect.test.tsx
Ivana Huckova a1a96a7324 Prometheus: Fix removing of labels when removed outside of component (#45424)
* Prometheus: Don't remove lables when switching metrics

* Always update labels when labels from prop change

* Add test

* Update variable name

* Revert "Update variable name"

This reverts commit ca186f8b0b.
2022-02-16 09:40:04 +01:00

97 lines
3.6 KiB
TypeScript

import React from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { MetricSelect } from './MetricSelect';
const props = {
query: {
metric: '',
labels: [],
operations: [],
},
onChange: jest.fn(),
onGetMetrics: jest
.fn()
.mockResolvedValue([{ label: 'random_metric' }, { label: 'unique_metric' }, { label: 'more_unique_metric' }]),
};
describe('MetricSelect', () => {
it('shows all metric options', async () => {
render(<MetricSelect {...props} />);
await openMetricSelect();
await waitFor(() => expect(screen.getByText('random_metric')).toBeInTheDocument());
await waitFor(() => expect(screen.getByText('unique_metric')).toBeInTheDocument());
await waitFor(() => expect(screen.getByText('more_unique_metric')).toBeInTheDocument());
await waitFor(() => expect(screen.getAllByLabelText('Select option')).toHaveLength(3));
});
it('shows option to create metric when typing', async () => {
render(<MetricSelect {...props} />);
await openMetricSelect();
const input = screen.getByRole('combobox');
userEvent.type(input, 'new');
await waitFor(() => expect(screen.getByText('Create: new')).toBeInTheDocument());
});
it('shows searched options when typing', async () => {
render(<MetricSelect {...props} />);
await openMetricSelect();
const input = screen.getByRole('combobox');
userEvent.type(input, 'unique');
await waitFor(() => expect(screen.getAllByLabelText('Select option')).toHaveLength(3));
});
it('searches on split words', async () => {
render(<MetricSelect {...props} />);
await openMetricSelect();
const input = screen.getByRole('combobox');
userEvent.type(input, 'more unique');
await waitFor(() => expect(screen.getAllByLabelText('Select option')).toHaveLength(2));
});
it('searches on multiple split words', async () => {
render(<MetricSelect {...props} />);
await openMetricSelect();
const input = screen.getByRole('combobox');
userEvent.type(input, 'more unique metric');
await waitFor(() => expect(screen.getAllByLabelText('Select option')).toHaveLength(2));
});
it('highlights matching string', async () => {
const { container } = render(<MetricSelect {...props} />);
await openMetricSelect();
const input = screen.getByRole('combobox');
userEvent.type(input, 'more');
await waitFor(() => expect(container.querySelectorAll('mark')).toHaveLength(1));
});
it('highlights multiple matching strings in 1 input row', async () => {
const { container } = render(<MetricSelect {...props} />);
await openMetricSelect();
const input = screen.getByRole('combobox');
userEvent.type(input, 'more metric');
await waitFor(() => expect(container.querySelectorAll('mark')).toHaveLength(2));
});
it('highlights multiple matching strings in multiple input rows', async () => {
const { container } = render(<MetricSelect {...props} />);
await openMetricSelect();
const input = screen.getByRole('combobox');
userEvent.type(input, 'unique metric');
await waitFor(() => expect(container.querySelectorAll('mark')).toHaveLength(4));
});
it('does not highlight matching string in create option', async () => {
const { container } = render(<MetricSelect {...props} />);
await openMetricSelect();
const input = screen.getByRole('combobox');
userEvent.type(input, 'new');
await waitFor(() => expect(container.querySelector('mark')).not.toBeInTheDocument());
});
});
async function openMetricSelect() {
const select = await screen.getByText('Select metric').parentElement!;
userEvent.click(select);
}