Files
grafana/public/app/features/annotations/executeAnnotationQuery.test.ts
Erik Sundell 29d4f6a217 Datasource: Change query filtering (#84656)
* call filterQuery from queryrunner

* test query hide filtering

* fix more broken tests

* lint errrors

* remove redundant filterQuery call

* skip filter in variable queries

* fix broken cypress test

* change tooltip text

* fix translations

* fix comments

* do not execute query is targets are empty

* add more tests

* remove unsued import

* update translations

* revert id change

* change header text

* update comment for hide prop

* rename hide query prop

* change tooltip and introduce different toggle state text

* update tests

* update comment and regenerate types

* run extract again

* fix broken e2e test

* track event

* fix build issues

* revert changes in wire file
2024-03-21 13:39:39 +01:00

60 lines
1.9 KiB
TypeScript

import { DataSourceApi, dateTime, DataQuery } from '@grafana/data';
import { PanelModel } from '../dashboard/state';
import { createDashboardModelFixture } from '../dashboard/state/__fixtures__/dashboardFixtures';
import { TestQuery, getMockDataSource } from '../query/state/__mocks__/mockDataSource';
import { executeAnnotationQuery } from './executeAnnotationQuery';
import { AnnotationQueryOptions } from './types';
describe('executeAnnotationQuery', () => {
let filterQuerySpy: jest.SpyInstance;
let querySpy: jest.SpyInstance;
let ds: DataSourceApi;
const setup = ({ query, filterQuery }: { query: TestQuery; filterQuery?: typeof ds.filterQuery }) => {
const options: AnnotationQueryOptions = {
range: { from: dateTime(), to: dateTime(), raw: { from: '1h', to: 'now' } },
dashboard: createDashboardModelFixture({
panels: [{ id: 1, type: 'graph' }],
}),
panel: {} as PanelModel,
};
const ds = getMockDataSource();
if (filterQuery) {
ds.filterQuery = filterQuery;
filterQuerySpy = jest.spyOn(ds, 'filterQuery');
}
querySpy = jest.spyOn(ds, 'query');
executeAnnotationQuery(options, ds, {
name: '',
enable: false,
iconColor: '',
target: query,
});
};
beforeEach(() => {
jest.clearAllMocks();
});
it('Should not call query method in case query is filtered out', async () => {
setup({
query: { q: 'SUM(foo)', refId: 'A' },
filterQuery: (query: TestQuery) => query.q !== 'SUM(foo)',
});
expect(filterQuerySpy).toHaveBeenCalledTimes(1);
expect(querySpy).not.toHaveBeenCalled();
});
it('Should call backend in case query is not filtered out', async () => {
setup({
filterQuery: (_: DataQuery) => true,
query: { q: 'SUM(foo)', refId: 'A' },
});
expect(filterQuerySpy).toHaveBeenCalledTimes(1);
expect(querySpy).toHaveBeenCalledTimes(1);
});
});