Skip to content

Commit 513eeae

Browse files
LdsystemShenglong.Liu
andauthored
replace getTemplateSrv with PanelProps#replaceVariables() so that interpolations will be replaced by repeated variables, instead of the original variable. (#6)
Co-authored-by: Shenglong.Liu <shenglong.liu@keytech-engeering.com>
1 parent 53bfaf8 commit 513eeae

File tree

2 files changed

+3
-15
lines changed

2 files changed

+3
-15
lines changed

src/buttonPanel.test.tsx

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
import {
99
getBackendSrv,
1010
getDataSourceSrv,
11-
getTemplateSrv,
1211
SystemJS,
1312
} from '@grafana/runtime';
1413
import { Button, HorizontalGroup, VerticalGroup } from '@grafana/ui';
@@ -40,7 +39,7 @@ describe('button panel', () => {
4039
onFieldConfigChange: () => {},
4140
onOptionsChange: () => {},
4241
renderCounter: 1,
43-
replaceVariables: () => '',
42+
replaceVariables: () => '{}',
4443
timeRange: DefaultTimeRange,
4544
timeZone: DefaultTimeZone,
4645
options: {
@@ -68,11 +67,6 @@ describe('button panel', () => {
6867
{ text: 'b', variant: 'primary', datasource: 'b' },
6968
];
7069
wrapper.setProps({ options: { buttons: buttons } });
71-
72-
const mockTemplate = jest.fn().mockReturnValue('{}');
73-
(getTemplateSrv as jest.Mock<any>).mockImplementation(() => ({
74-
replace: mockTemplate,
75-
}));
7670
const mockGet = jest.fn().mockReturnValue({ id: 1 });
7771
(getDataSourceSrv as jest.Mock<any>).mockImplementation(() => ({
7872
get: mockGet,
@@ -97,7 +91,6 @@ describe('button panel', () => {
9791
expect(b.text()).toBe(buttons[i].text);
9892
b.simulate('click');
9993
setImmediate(() => {
100-
expect(mockTemplate).toHaveBeenCalled();
10194
expect(mockGet).toHaveBeenCalledWith(buttons[i].datasource);
10295
expect(mockDataSourceRequest).toHaveBeenCalled();
10396
expect(mockEmit).toHaveBeenCalledWith(AppEvents.alertSuccess, [
@@ -118,10 +111,6 @@ describe('button panel', () => {
118111
(getDataSourceSrv as jest.Mock<any>).mockImplementation(() => ({
119112
get: mockGet,
120113
}));
121-
const mockTemplate = jest.fn().mockReturnValue('{}');
122-
(getTemplateSrv as jest.Mock<any>).mockImplementation(() => ({
123-
replace: mockTemplate,
124-
}));
125114
const msg = 'msg';
126115
const mockDataSourceRequest = jest.fn().mockRejectedValue({
127116
status: statusError,

src/buttonPanel.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { AppEvents, PanelProps } from '@grafana/data';
22
import {
33
getBackendSrv,
44
getDataSourceSrv,
5-
getTemplateSrv,
65
SystemJS,
76
} from '@grafana/runtime';
87
import { Button, HorizontalGroup, VerticalGroup } from '@grafana/ui';
@@ -11,7 +10,7 @@ import { ButtonOptions, Options } from 'types';
1110

1211
interface Props extends PanelProps<Options> {}
1312

14-
export const ButtonPanel: React.FC<Props> = ({ options }) => {
13+
export const ButtonPanel: React.FC<Props> = ({ options, replaceVariables }) => {
1514
const renderButtons = (buttons: ButtonOptions[]) => {
1615
return buttons.map((b: ButtonOptions, index: number) => {
1716
const text = b.text || 'Button';
@@ -21,7 +20,7 @@ export const ButtonPanel: React.FC<Props> = ({ options }) => {
2120
variant={b.variant}
2221
onClick={async () => {
2322
const payload = JSON.parse(
24-
await getTemplateSrv().replace(b.query || '{}')
23+
replaceVariables(b.query || '{}')
2524
);
2625
const ds = await getDataSourceSrv().get(b.datasource);
2726
try {

0 commit comments

Comments
 (0)