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:
Andreas Christou 2024-03-22 17:06:51 +00:00 committed by GitHub
parent e138ae3eb9
commit 4855751d0d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 43 additions and 1 deletions

View File

@ -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);
});
});

View File

@ -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]
);