mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Pyroscope: Decouple from templateSrv (#79068)
* Decouple templateSrv * Update test
This commit is contained in:
parent
e3d14307ed
commit
4751013a91
@ -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);
|
||||||
|
@ -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') {
|
||||||
|
@ -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);
|
||||||
|
Loading…
Reference in New Issue
Block a user