Skip to content

Commit 78882ec

Browse files
committed
Improve tests for checking "clickthroughUrl" interpolation
1 parent 27e042a commit 78882ec

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changelog
22

33
## development
4+
- Improve tests for checking "clickthroughUrl" interpolation
45

56

67
## v0.9.0

src/data_formatter.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,46 @@ describe('DataFormatter', () => {
163163
});
164164
});
165165

166+
describe('when some fields are given in table data', () => {
167+
168+
const data: any[] = [];
169+
170+
beforeEach(() => {
171+
172+
jQuery.extend(ctrl, {
173+
panel: {
174+
tableQueryOptions: {
175+
},
176+
},
177+
});
178+
179+
const tableData = [
180+
[
181+
{
182+
foo: "42.42",
183+
bar: 42.42,
184+
},
185+
{
186+
foo: "43.43",
187+
bar: 43.43,
188+
},
189+
],
190+
];
191+
192+
const dataFormatter = new DataFormatter(ctrl);
193+
dataFormatter.setTableValues(tableData, data);
194+
195+
});
196+
197+
it('the fields should be available within transformed data', () => {
198+
expect(data[0].__field_foo).toEqual("42.42");
199+
expect(data[0].__field_bar).toEqual(42.42);
200+
expect(data[1].__field_foo).toEqual("43.43");
201+
expect(data[1].__field_bar).toEqual(43.43);
202+
});
203+
204+
});
205+
166206
describe('when the time series data matches the location', () => {
167207
beforeEach(() => {
168208
jQuery.extend(ctrl, {

src/worldmap.test.ts

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { createBasicMap } from '../test/map_builder';
33
import $ from 'jquery';
44
import PluginSettings from './settings';
55
import { TemplateSrv } from 'grafana/app/features/templating/template_srv';
6+
import DataFormatter from "./data_formatter";
67

78
describe('Worldmap', () => {
89
let worldMap;
@@ -488,9 +489,21 @@ describe('ClickthroughLinks', () => {
488489
let ctrl;
489490

490491
// https://github.com/grafana/grafana/blob/v6.5.2/public/app/plugins/datasource/loki/datasource.test.ts#L28-L31
492+
// https://github.com/grafana/grafana/blob/v6.5.2/public/app/features/templating/template_srv.ts#L261
493+
const variableRegex = /\$(\w+)|\[\[([\s\S]+?)(?::(\w+))?\]\]|\${(\w+)(?:\.([^:^\}]+))?(?::(\w+))?}/g;
491494
const templateSrvMock = ({
492495
getAdhocFilters: (): any[] => [],
493-
replace: (a: string) => a,
496+
replace: (target: any, scopedVars?: any, format?: any) => {
497+
return target.replace(variableRegex, (match, var1, var2, fmt2, var3, fieldPath, fmt3) => {
498+
const variableName = var1 || var2 || var3;
499+
if (scopedVars[variableName]) {
500+
const value = scopedVars[variableName].value;
501+
if (value !== null && value !== undefined) {
502+
return value;
503+
}
504+
}
505+
})
506+
},
494507
} as unknown) as TemplateSrv;
495508

496509
beforeEach(() => {
@@ -562,4 +575,74 @@ describe('ClickthroughLinks', () => {
562575
});
563576

564577
});
578+
579+
describe('when using fields with clickthrough-links on table data', () => {
580+
beforeEach(() => {
581+
582+
// Create map.
583+
ctrl.panel.clickthroughUrl = 'http://foo.bar/?foo=$__field_foo&value=$value';
584+
585+
ctrl.panel.tableQueryOptions = {
586+
queryType: 'coordinates',
587+
latitudeField: 'latitude',
588+
longitudeField: 'longitude',
589+
metricField: 'metric',
590+
labelField: 'name',
591+
labelLocationKeyField: 'key',
592+
linkField: null,
593+
};
594+
595+
// Load settings and create map.
596+
ctrl.settings = new PluginSettings(ctrl.panel, templateSrvMock, {});
597+
worldMap.createMap();
598+
599+
// Define fixture.
600+
const tableData = [
601+
[
602+
{
603+
key: 'SE',
604+
name: 'Sweden',
605+
latitude: 60,
606+
longitude: 18,
607+
metric: 123.456,
608+
609+
foo: "42.42",
610+
bar: 42.42,
611+
},
612+
{
613+
key: 'IE',
614+
name: 'Ireland',
615+
latitude: 53,
616+
longitude: 8,
617+
metric: 45.678,
618+
619+
foo: "43.43",
620+
bar: 43.43,
621+
},
622+
],
623+
];
624+
625+
// Apply data as table format.
626+
const dataFormatter = new DataFormatter(ctrl);
627+
const data: any[] = [];
628+
dataFormatter.setTableValues(tableData, data);
629+
data.thresholds = [];
630+
631+
// Draw circles.
632+
ctrl.data = data;
633+
worldMap.drawCircles();
634+
635+
});
636+
637+
it('the fields within transformed data should interpolate well into clickthrough links', () => {
638+
// Prepare interaction with window object.
639+
setupInteractionMocks();
640+
641+
// Capture interaction.
642+
worldMap.circles[0].fire('click');
643+
expect(window.location.assign).toHaveBeenCalledWith('http://foo.bar/?foo=42.42&value=123.456');
644+
});
645+
646+
});
647+
565648
});

0 commit comments

Comments
 (0)