Pyroscope: Decouple from templateSrv (#79068)

* Decouple templateSrv

* Update test
This commit is contained in:
Joey 2023-12-11 13:14:22 +00:00 committed by GitHub
parent e3d14307ed
commit 4751013a91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 8 deletions

View File

@ -17,6 +17,13 @@ jest.mock('@grafana/runtime', () => ({
...jest.requireActual('@grafana/runtime'), ...jest.requireActual('@grafana/runtime'),
setPluginExtensionGetter: jest.fn(), setPluginExtensionGetter: jest.fn(),
getPluginLinkExtensions: jest.fn(), getPluginLinkExtensions: jest.fn(),
getTemplateSrv: () => {
return {
replace: (query: string): string => {
return query.replace(/\$var/g, 'interpolated');
},
};
},
})); }));
const getPluginLinkExtensionsMock = jest.mocked(getPluginLinkExtensions); const getPluginLinkExtensionsMock = jest.mocked(getPluginLinkExtensions);

View File

@ -2,12 +2,24 @@ import { render, screen } from '@testing-library/react';
import React from 'react'; import React from 'react';
import { DataSourceInstanceSettings } from '@grafana/data'; import { DataSourceInstanceSettings } from '@grafana/data';
import { TemplateSrv } from 'app/features/templating/template_srv'; import { getTemplateSrv } from '@grafana/runtime';
import { VariableQueryEditor } from './VariableQueryEditor'; import { VariableQueryEditor } from './VariableQueryEditor';
import { PyroscopeDataSource } from './datasource'; import { PyroscopeDataSource } from './datasource';
import { PyroscopeDataSourceOptions } from './types'; import { PyroscopeDataSourceOptions } from './types';
jest.mock('@grafana/runtime', () => {
const actual = jest.requireActual('@grafana/runtime');
return {
...actual,
getTemplateSrv: () => {
return {
replace: jest.fn(),
};
},
};
});
describe('VariableQueryEditor', () => { describe('VariableQueryEditor', () => {
it('renders correctly with type profileType', () => { it('renders correctly with type profileType', () => {
render( render(
@ -69,7 +81,7 @@ describe('VariableQueryEditor', () => {
}); });
function getMockDatasource() { function getMockDatasource() {
const ds = new PyroscopeDataSource({} as DataSourceInstanceSettings<PyroscopeDataSourceOptions>, new TemplateSrv()); const ds = new PyroscopeDataSource({} as DataSourceInstanceSettings<PyroscopeDataSourceOptions>, getTemplateSrv());
ds.getResource = jest.fn(); ds.getResource = jest.fn();
(ds.getResource as jest.Mock).mockImplementation(async (type: string) => { (ds.getResource as jest.Mock).mockImplementation(async (type: string) => {
if (type === 'profileTypes') { if (type === 'profileTypes') {

View File

@ -6,13 +6,26 @@ import {
PluginType, PluginType,
DataSourceJsonData, DataSourceJsonData,
} from '@grafana/data'; } from '@grafana/data';
import { setPluginExtensionGetter, getBackendSrv, setBackendSrv } from '@grafana/runtime'; import { setPluginExtensionGetter, getBackendSrv, setBackendSrv, getTemplateSrv } from '@grafana/runtime';
import { TemplateSrv } from 'app/features/templating/template_srv';
import { defaultPyroscopeQueryType } from './dataquery.gen'; import { defaultPyroscopeQueryType } from './dataquery.gen';
import { normalizeQuery, PyroscopeDataSource } from './datasource'; import { normalizeQuery, PyroscopeDataSource } from './datasource';
import { Query } from './types'; import { Query } from './types';
jest.mock('@grafana/runtime', () => {
const actual = jest.requireActual('@grafana/runtime');
return {
...actual,
getTemplateSrv: () => {
return {
replace: (query: string): string => {
return query.replace(/\$var/g, 'interpolated');
},
};
},
};
});
/** The datasource QueryEditor fetches datasource settings to send to the extension's `configure` method */ /** The datasource QueryEditor fetches datasource settings to send to the extension's `configure` method */
export function mockFetchPyroscopeDatasourceSettings( export function mockFetchPyroscopeDatasourceSettings(
datasourceSettings?: Partial<DataSourceInstanceSettings<DataSourceJsonData>> datasourceSettings?: Partial<DataSourceInstanceSettings<DataSourceJsonData>>
@ -80,10 +93,7 @@ describe('Pyroscope data source', () => {
}); });
describe('applyTemplateVariables', () => { describe('applyTemplateVariables', () => {
const templateSrv = new TemplateSrv(); const templateSrv = getTemplateSrv();
templateSrv.replace = jest.fn((query: string): string => {
return query.replace(/\$var/g, 'interpolated');
});
it('should not update labelSelector if there are no template variables', () => { it('should not update labelSelector if there are no template variables', () => {
ds = new PyroscopeDataSource(defaultSettings, templateSrv); ds = new PyroscopeDataSource(defaultSettings, templateSrv);