mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Cloudwatch: Fix issue where selected log groups clear from dashboards if there are more than 50 results (#57196)
This commit is contained in:
parent
9c954d06ab
commit
b2e2879b07
@ -1,4 +1,4 @@
|
||||
import { act, render, screen, waitFor } from '@testing-library/react';
|
||||
import { act, render, screen } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import lodash from 'lodash'; // eslint-disable-line lodash/import-scope
|
||||
import React from 'react';
|
||||
@ -26,7 +26,7 @@ describe('LogGroupSelector', () => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
it('updates upstream query log groups on region change', async () => {
|
||||
it('does not clear previously selected log groups after region change', async () => {
|
||||
ds.datasource.api.describeLogGroups = jest.fn().mockImplementation(async (params: DescribeLogGroupsRequest) => {
|
||||
if (params.region === 'region1') {
|
||||
return Promise.resolve(['log_group_1'].map(toOption));
|
||||
@ -40,36 +40,10 @@ describe('LogGroupSelector', () => {
|
||||
};
|
||||
|
||||
const { rerender } = render(<LogGroupSelector {...props} />);
|
||||
await waitFor(() => expect(onChange).toHaveBeenCalledTimes(1));
|
||||
expect(onChange).toHaveBeenLastCalledWith(['log_group_1']);
|
||||
expect(await screen.findByText('log_group_1')).toBeInTheDocument();
|
||||
|
||||
act(() => rerender(<LogGroupSelector {...props} region="region2" />));
|
||||
await waitFor(() => expect(onChange).toHaveBeenCalledTimes(1));
|
||||
expect(onChange).toHaveBeenLastCalledWith([]);
|
||||
});
|
||||
|
||||
it('does not update upstream query log groups if saved is false', async () => {
|
||||
ds.datasource.api.describeLogGroups = jest.fn().mockImplementation(async (params: DescribeLogGroupsRequest) => {
|
||||
if (params.region === 'region1') {
|
||||
return Promise.resolve(['log_group_1'].map(toOption));
|
||||
} else {
|
||||
return Promise.resolve(['log_group_2'].map(toOption));
|
||||
}
|
||||
});
|
||||
const props = {
|
||||
...defaultProps,
|
||||
selectedLogGroups: ['log_group_1'],
|
||||
};
|
||||
|
||||
const { rerender } = render(<LogGroupSelector {...props} />);
|
||||
await waitFor(() => expect(onChange).toHaveBeenCalledTimes(1));
|
||||
expect(onChange).toHaveBeenLastCalledWith(['log_group_1']);
|
||||
expect(await screen.findByText('log_group_1')).toBeInTheDocument();
|
||||
|
||||
act(() => rerender(<LogGroupSelector {...props} region="region2" saved={false} />));
|
||||
await waitFor(() => expect(onChange).toHaveBeenCalledTimes(1));
|
||||
expect(onChange).toHaveBeenLastCalledWith(['log_group_1']);
|
||||
});
|
||||
|
||||
it('should merge results of remote log groups search with existing results', async () => {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { debounce, intersection, unionBy } from 'lodash';
|
||||
import { debounce, unionBy } from 'lodash';
|
||||
import React, { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
|
||||
import { SelectableValue, toOption } from '@grafana/data';
|
||||
@ -25,7 +25,7 @@ export interface LogGroupSelectorProps {
|
||||
onOpenMenu?: () => Promise<void>;
|
||||
refId?: string;
|
||||
width?: number | 'auto';
|
||||
saved?: boolean;
|
||||
saved?: boolean; // is only used in the config editor
|
||||
}
|
||||
|
||||
export const LogGroupSelector: React.FC<LogGroupSelectorProps> = ({
|
||||
@ -90,7 +90,7 @@ export const LogGroupSelector: React.FC<LogGroupSelectorProps> = ({
|
||||
|
||||
// Reset the log group options if the datasource or region change and are saved
|
||||
useEffect(() => {
|
||||
async function resetLogGroups() {
|
||||
async function getAvailableLogGroupOptions() {
|
||||
// Don't call describeLogGroups if datasource or region is undefined
|
||||
if (!datasource || !datasource.getActualRegion(region)) {
|
||||
setAvailableLogGroups([]);
|
||||
@ -100,19 +100,15 @@ export const LogGroupSelector: React.FC<LogGroupSelectorProps> = ({
|
||||
setLoadingLogGroups(true);
|
||||
return fetchLogGroupOptions(datasource.getActualRegion(region))
|
||||
.then((logGroups) => {
|
||||
const newSelectedLogGroups = intersection(
|
||||
selectedLogGroups,
|
||||
logGroups.map((l) => l.value || '')
|
||||
);
|
||||
onChange(newSelectedLogGroups);
|
||||
setAvailableLogGroups(logGroups);
|
||||
})
|
||||
.finally(() => {
|
||||
setLoadingLogGroups(false);
|
||||
});
|
||||
}
|
||||
// Only reset if the current datasource is saved
|
||||
saved && resetLogGroups();
|
||||
|
||||
// Config editor does not fetch new log group options unless changes have been saved
|
||||
saved && getAvailableLogGroupOptions();
|
||||
// this hook shouldn't get called every time selectedLogGroups or onChange updates
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [datasource, region, saved]);
|
||||
|
Loading…
Reference in New Issue
Block a user