mirror of
https://github.com/grafana/grafana.git
synced 2025-08-02 04:09:50 +08:00

* 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
121 lines
4.3 KiB
TypeScript
121 lines
4.3 KiB
TypeScript
import { DataSourceInstanceSettings } from '@grafana/data';
|
|
import { getTemplateSrv, TemplateSrv } from '@grafana/runtime'; // will use the version in __mocks__
|
|
|
|
import CloudMonitoringDataSource from '../datasource';
|
|
import { CloudMonitoringOptions, CustomVariableModel } from '../types/types';
|
|
|
|
let getTempVars = () => [] as CustomVariableModel[];
|
|
let replace = () => '';
|
|
|
|
jest.mock('@grafana/runtime', () => ({
|
|
__esModule: true,
|
|
...jest.requireActual('@grafana/runtime'),
|
|
getTemplateSrv: () => ({
|
|
replace: replace,
|
|
getVariables: getTempVars,
|
|
updateTimeRange: jest.fn(),
|
|
containsTemplate: jest.fn(),
|
|
}),
|
|
}));
|
|
|
|
type Args = { response?: unknown; throws?: boolean; templateSrv?: TemplateSrv };
|
|
|
|
function getTestcontext({ response = {}, throws = false, templateSrv = getTemplateSrv() }: Args = {}) {
|
|
jest.clearAllMocks();
|
|
|
|
const instanceSettings = {
|
|
jsonData: {
|
|
defaultProject: 'testproject',
|
|
},
|
|
} as unknown as DataSourceInstanceSettings<CloudMonitoringOptions>;
|
|
|
|
const ds = new CloudMonitoringDataSource(instanceSettings, templateSrv);
|
|
|
|
return { ds };
|
|
}
|
|
|
|
describe('CloudMonitoringDataSource', () => {
|
|
describe('when interpolating a template variable for the filter', () => {
|
|
beforeEach(() => {
|
|
getTempVars = () => [] as CustomVariableModel[];
|
|
replace = (target?: string) => target || '';
|
|
});
|
|
describe('and is single value variable', () => {
|
|
it('should replace the variable with the value', () => {
|
|
replace = () => 'filtervalue1';
|
|
const { ds } = getTestcontext();
|
|
const interpolated = ds.interpolateFilters(['resource.label.zone', '=~', '${test}'], {});
|
|
|
|
expect(interpolated.length).toBe(3);
|
|
expect(interpolated[2]).toBe('filtervalue1');
|
|
});
|
|
});
|
|
|
|
describe('and is single value variable for the label part', () => {
|
|
it('should replace the variable with the value and not with regex formatting', () => {
|
|
replace = () => 'resource.label.zone';
|
|
const { ds } = getTestcontext();
|
|
const interpolated = ds.interpolateFilters(['${test}', '=~', 'europe-north-1a'], {});
|
|
|
|
expect(interpolated.length).toBe(3);
|
|
expect(interpolated[0]).toBe('resource.label.zone');
|
|
});
|
|
});
|
|
|
|
describe('and is multi value variable', () => {
|
|
beforeEach(() => {
|
|
getTempVars = () => [] as CustomVariableModel[];
|
|
replace = (target?: string) => target || '';
|
|
});
|
|
it('should replace the variable with a regex expression', () => {
|
|
replace = () => '(filtervalue1|filtervalue2)';
|
|
const { ds } = getTestcontext();
|
|
const interpolated = ds.interpolateFilters(['resource.label.zone', '=~', '${test}'], {});
|
|
|
|
expect(interpolated[2]).toBe('(filtervalue1|filtervalue2)');
|
|
});
|
|
|
|
it('should not escape a regex', () => {
|
|
replace = () => '/[a-Z]*.html';
|
|
const { ds } = getTestcontext();
|
|
const interpolated = ds.interpolateFilters(['resource.label.zone', '=~', '${test}'], {});
|
|
|
|
expect(interpolated[2]).toBe('/[a-Z]*.html');
|
|
});
|
|
|
|
it('should not escape an array of regexes but join them as a regex', () => {
|
|
replace = () => '(/[a-Z]*.html|/foo.html)';
|
|
const { ds } = getTestcontext();
|
|
const interpolated = ds.interpolateFilters(['resource.label.zone', '=~', '${test}'], {});
|
|
|
|
expect(interpolated[2]).toBe('(/[a-Z]*.html|/foo.html)');
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('when interpolating a template variable for group bys', () => {
|
|
describe('and is single value variable', () => {
|
|
it('should replace the variable with the value', () => {
|
|
replace = () => 'groupby1';
|
|
const { ds } = getTestcontext();
|
|
const interpolated = ds.interpolateGroupBys(['${test}'], {});
|
|
|
|
expect(interpolated.length).toBe(1);
|
|
expect(interpolated[0]).toBe('groupby1');
|
|
});
|
|
});
|
|
|
|
describe('and is multi value variable', () => {
|
|
it('should replace the variable with an array of group bys', () => {
|
|
replace = () => 'groupby1,groupby2';
|
|
const { ds } = getTestcontext();
|
|
const interpolated = ds.interpolateGroupBys(['${test}'], {});
|
|
|
|
expect(interpolated.length).toBe(2);
|
|
expect(interpolated[0]).toBe('groupby1');
|
|
expect(interpolated[1]).toBe('groupby2');
|
|
});
|
|
});
|
|
});
|
|
});
|