mirror of
https://github.com/grafana/grafana.git
synced 2025-01-11 00:22:06 -06:00
CloudMonitoring: Only run query if filters are complete (#85004)
* Only run query if filters are complete - Update tests * Fix tests
This commit is contained in:
parent
e138ae3eb9
commit
4855751d0d
@ -1,5 +1,7 @@
|
||||
import { render, screen, waitFor } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import React from 'react';
|
||||
import { openMenu } from 'react-select-event';
|
||||
|
||||
import { getDefaultTimeRange } from '@grafana/data';
|
||||
|
||||
@ -80,4 +82,33 @@ describe('MetricQueryEditor', () => {
|
||||
render(<MetricQueryEditor {...defaultProps} query={query} />);
|
||||
await waitFor(() => expect(screen.getByLabelText('Alias by').closest('input')!.value).toEqual('AliasTest'));
|
||||
});
|
||||
|
||||
it('runs a timeSeriesList query if there are no filters', async () => {
|
||||
const onRunQuery = jest.fn();
|
||||
const onChange = jest.fn();
|
||||
const query = createMockQuery();
|
||||
|
||||
render(<MetricQueryEditor {...defaultProps} onRunQuery={onRunQuery} onChange={onChange} query={query} />);
|
||||
|
||||
const groupBy = screen.getByLabelText('Group by');
|
||||
openMenu(groupBy);
|
||||
const option = 'metadata.system_labels.cloud_account';
|
||||
await userEvent.click(screen.getByText(option));
|
||||
|
||||
expect(onRunQuery).toHaveBeenCalledTimes(1);
|
||||
expect(onChange).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('does not run a timeSeriesList query when filter is added', async () => {
|
||||
const onRunQuery = jest.fn();
|
||||
const onChange = jest.fn();
|
||||
const query = createMockQuery();
|
||||
|
||||
render(<MetricQueryEditor {...defaultProps} onRunQuery={onRunQuery} onChange={onChange} query={query} />);
|
||||
|
||||
const addFilter = screen.getByLabelText('Add');
|
||||
await userEvent.click(addFilter);
|
||||
expect(onRunQuery).toHaveBeenCalledTimes(0);
|
||||
expect(onChange).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
@ -50,8 +50,19 @@ function Editor({
|
||||
}: React.PropsWithChildren<Props>) {
|
||||
const onChangeTimeSeriesList = useCallback(
|
||||
(timeSeriesList: TimeSeriesList) => {
|
||||
let filtersComplete = true;
|
||||
if (timeSeriesList?.filters && timeSeriesList.filters.length > 0) {
|
||||
for (const filter of timeSeriesList.filters) {
|
||||
if (filter === '') {
|
||||
filtersComplete = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
onQueryChange({ ...query, timeSeriesList });
|
||||
onRunQuery();
|
||||
if (filtersComplete) {
|
||||
onRunQuery();
|
||||
}
|
||||
},
|
||||
[onQueryChange, onRunQuery, query]
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user