DatasourcePicker: Call onChange only if data source changes (#71630)

Call onChange only when data source changes
This commit is contained in:
Piotr Jamróz
2023-07-19 08:39:52 +02:00
committed by GitHub
parent ad272b7984
commit 173c87beb5
2 changed files with 14 additions and 4 deletions

View File

@@ -142,7 +142,7 @@ describe('DataSourceDropdown', () => {
);
});
it('should dispaly the current selected DS in the selector', async () => {
it('should display the current selected DS in the selector', async () => {
getInstanceSettingsMock.mockReturnValue(mockDS2);
render(<DataSourceDropdown onChange={jest.fn()} current={mockDS2}></DataSourceDropdown>);
expect(screen.getByTestId('Select a data source')).toHaveAttribute('placeholder', mockDS2.name);
@@ -163,7 +163,7 @@ describe('DataSourceDropdown', () => {
expect(await findByText(cards[0], mockDS2.name, { selector: 'span' })).toBeInTheDocument();
});
it('should dispaly the default DS as selected when `current` is not set', async () => {
it('should display the default DS as selected when `current` is not set', async () => {
getInstanceSettingsMock.mockReturnValue(mockDS2);
render(<DataSourceDropdown onChange={jest.fn()} current={undefined}></DataSourceDropdown>);
expect(screen.getByTestId('Select a data source')).toHaveAttribute('placeholder', mockDS2.name);
@@ -216,6 +216,14 @@ describe('DataSourceDropdown', () => {
expect(screen.queryByText(mockDS1.name, { selector: 'span' })).toBeNull();
});
it('should not call onChange when the currently selected data source is clicked', async () => {
const onChange = jest.fn();
await setupOpenDropdown(user, { onChange });
await user.click(await screen.findByText(mockDS1.name, { selector: 'span' }));
expect(onChange).not.toBeCalled();
});
it('should push recently used datasources when a data source is clicked', async () => {
const onChange = jest.fn();
await setupOpenDropdown(user, { onChange });

View File

@@ -202,7 +202,10 @@ export function DataSourceDropdown(props: DataSourceDropdownProps) {
filterTerm={filterTerm}
onChange={(ds: DataSourceInstanceSettings, defaultQueries?: DataQuery[] | GrafanaQuery[]) => {
onClose();
onChange(ds, defaultQueries);
if (ds.uid !== currentValue?.uid) {
onChange(ds, defaultQueries);
reportInteraction(INTERACTION_EVENT_NAME, { item: INTERACTION_ITEM.SELECT_DS, ds_type: ds.type });
}
}}
onClose={onClose}
current={currentValue}
@@ -253,7 +256,6 @@ const PickerContent = React.forwardRef<HTMLDivElement, PickerContentProps>((prop
const changeCallback = useCallback(
(ds: DataSourceInstanceSettings) => {
onChange(ds);
reportInteraction(INTERACTION_EVENT_NAME, { item: INTERACTION_ITEM.SELECT_DS, ds_type: ds.type });
},
[onChange]
);