@@ -3,6 +3,7 @@ import { createBasicMap } from '../test/map_builder';
3
3
import $ from 'jquery' ;
4
4
import PluginSettings from './settings' ;
5
5
import { TemplateSrv } from 'grafana/app/features/templating/template_srv' ;
6
+ import DataFormatter from "./data_formatter" ;
6
7
7
8
describe ( 'Worldmap' , ( ) => {
8
9
let worldMap ;
@@ -488,9 +489,21 @@ describe('ClickthroughLinks', () => {
488
489
let ctrl ;
489
490
490
491
// 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;
491
494
const templateSrvMock = ( {
492
495
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
+ } ,
494
507
} as unknown ) as TemplateSrv ;
495
508
496
509
beforeEach ( ( ) => {
@@ -562,4 +575,74 @@ describe('ClickthroughLinks', () => {
562
575
} ) ;
563
576
564
577
} ) ;
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
+
565
648
} ) ;
0 commit comments