Loki: Fix fetching of second label set for the streams selector (#46898)

* Fix fetching of labels

* Add test
This commit is contained in:
Ivana Huckova 2022-03-23 17:59:48 +00:00 committed by GitHub
parent 9f16644476
commit 7839fadf00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 1 deletions

View File

@ -0,0 +1,45 @@
import React from 'react';
import { render, screen, getAllByRole, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { LokiQueryBuilder } from './LokiQueryBuilder';
import { LokiDatasource } from '../../datasource';
import { LokiVisualQuery } from '../types';
import { PanelData } from '@grafana/data';
const defaultQuery: LokiVisualQuery = {
labels: [{ op: '=', label: 'baz', value: 'bar' }],
operations: [],
};
describe('LokiQueryBuilder', () => {
it('tries to load labels when no labels are selected', async () => {
const { datasource } = setup();
datasource.languageProvider.fetchSeriesLabels = jest.fn().mockReturnValue({ job: ['a'], instance: ['b'] });
userEvent.click(screen.getByLabelText('Add'));
const labels = screen.getByText(/Labels/);
const selects = getAllByRole(labels.parentElement!, 'combobox');
userEvent.click(selects[3]);
await waitFor(() => expect(screen.getByText('job')).toBeInTheDocument());
});
});
function setup(query: LokiVisualQuery = defaultQuery, data?: PanelData) {
const datasource = new LokiDatasource(
{
url: '',
jsonData: {},
meta: {} as any,
} as any,
undefined,
undefined
);
const props = {
datasource,
onRunQuery: () => {},
onChange: () => {},
data,
};
const { container } = render(<LokiQueryBuilder {...props} query={query} />);
return { datasource, container };
}

View File

@ -36,7 +36,8 @@ export const LokiQueryBuilder = React.memo<Props>(({ datasource, query, nested,
} }
const expr = lokiQueryModeller.renderLabels(labelsToConsider); const expr = lokiQueryModeller.renderLabels(labelsToConsider);
return await datasource.languageProvider.fetchSeriesLabels(expr); const series = await datasource.languageProvider.fetchSeriesLabels(expr);
return Object.keys(series).sort();
}; };
const onGetLabelValues = async (forLabel: Partial<QueryBuilderLabelFilter>) => { const onGetLabelValues = async (forLabel: Partial<QueryBuilderLabelFilter>) => {