Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v10.0.x] Explore: Run remaining queries when one is removed from a pane #69670

Merged
merged 3 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 35 additions & 4 deletions public/app/features/explore/state/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from '@grafana/data';
import { config } from '@grafana/runtime';
import { DataQuery, DataSourceRef } from '@grafana/schema';
import { ExploreId, ExploreItemState, StoreState, ThunkDispatch } from 'app/types';
import { createAsyncThunk, ExploreId, ExploreItemState, StoreState, ThunkDispatch } from 'app/types';

import { reducerTester } from '../../../../test/core/redux/reducerTester';
import { configureStore } from '../../../store/configureStore';
Expand Down Expand Up @@ -242,12 +242,12 @@ describe('running queries', () => {
});

describe('changeQueries', () => {
afterEach(() => {
jest.restoreAllMocks();
});
// Due to how spyOn works (it removes `type`, `match` and `toString` from the spied function, on which we rely on in the reducer),
// we are repeating the following tests twice, once to chck the resulting state and once to check that the correct actions are dispatched.
describe('calls the correct actions', () => {
afterEach(() => {
jest.restoreAllMocks();
});
it('should import queries when datasource is changed', async () => {
jest.spyOn(actions, 'importQueries');
jest.spyOn(actions, 'changeQueriesAction');
Expand Down Expand Up @@ -363,6 +363,37 @@ describe('changeQueries', () => {
});
});
});

it('runs remaining queries when one query is removed', async () => {
jest.spyOn(actions, 'runQueries').mockImplementation(createAsyncThunk('@explore/runQueries', () => {}));

const originalQueries = [
{ refId: 'A', datasource: datasources[0].getRef() },
{ refId: 'B', datasource: datasources[0].getRef() },
];

const { dispatch } = configureStore({
...defaultInitialState,
explore: {
panes: {
left: {
...defaultInitialState.explore.panes.left,
datasourceInstance: datasources[0],
queries: originalQueries,
},
},
},
} as unknown as Partial<StoreState>);

await dispatch(
changeQueries({
queries: [originalQueries[0]],
exploreId: ExploreId.left,
})
);

expect(actions.runQueries).toHaveBeenCalled();
});
});

describe('importing queries', () => {
Expand Down
2 changes: 1 addition & 1 deletion public/app/features/explore/state/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ export const changeQueries = createAsyncThunk<void, ChangeQueriesPayload>(
}

// if we are removing a query we want to run the remaining ones
if (queries.length < queries.length) {
if (queries.length < oldQueries.length) {
dispatch(runQueries(exploreId));
}
}
Expand Down