Skip to content

Commit 1bd2776

Browse files
committed
Add tests for clicktrough links
1 parent acb676b commit 1bd2776

File tree

3 files changed

+109
-1
lines changed

3 files changed

+109
-1
lines changed

src/worldmap.test.ts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import DataBuilder from '../test/data_builder';
22
import {createBasicMap} from '../test/map_builder';
33
import $ from 'jquery';
4+
import PluginSettings from "./settings";
5+
import { TemplateSrv } from 'grafana/app/features/templating/template_srv';
46

57

68
describe('Worldmap', () => {
@@ -461,3 +463,108 @@ describe('WorldmapFoundation', () => {
461463
});
462464

463465
});
466+
467+
468+
describe('ClickthroughLinks', () => {
469+
/*
470+
* These tests proof the clickthrough link works.
471+
*
472+
* See also https://community.hiveeyes.org/t/developing-grafana-worldmap-ng/1824/13
473+
*/
474+
475+
let worldMap;
476+
let ctrl;
477+
478+
// https://github.com/grafana/grafana/blob/v6.5.2/public/app/plugins/datasource/loki/datasource.test.ts#L28-L31
479+
const templateSrvMock = ({
480+
getAdhocFilters: (): any[] => [],
481+
replace: (a: string) => a,
482+
} as unknown) as TemplateSrv;
483+
484+
beforeEach(() => {
485+
worldMap = createBasicMap();
486+
ctrl = worldMap.ctrl;
487+
//ctrl.loadSettings();
488+
});
489+
490+
afterEach(() => {
491+
const fixture: HTMLElement = document.getElementById('fixture')!;
492+
document.body.removeChild(fixture);
493+
});
494+
495+
describe('when a Worldmap is created with clickthrough-links enabled', () => {
496+
beforeEach(() => {
497+
498+
// Create map.
499+
ctrl.panel.clickthroughUrl = 'http://foo.bar';
500+
ctrl.settings = new PluginSettings(ctrl.panel, templateSrvMock, {});
501+
worldMap.createMap();
502+
503+
// Load data and draw circles.
504+
ctrl.data = new DataBuilder()
505+
.withCountryAndValue('SE', 1)
506+
.build();
507+
worldMap.drawCircles();
508+
509+
});
510+
511+
it('should have registered a second click event', () => {
512+
expect(worldMap.circles.length).toBe(1);
513+
expect(worldMap.circles[0]._events.click.length).toBe(2);
514+
});
515+
516+
it('should do its job when actually clicked', () => {
517+
518+
// Setup interaction mock for "window.location.assign".
519+
// https://remarkablemark.org/blog/2018/11/17/mock-window-location/
520+
Object.defineProperty(window.location, 'assign', {
521+
configurable: true,
522+
});
523+
window.location.assign = jest.fn();
524+
525+
// Capture interaction.
526+
worldMap.circles[0].fire('click');
527+
expect(window.location.assign).toHaveBeenCalledWith('http://foo.bar');
528+
});
529+
530+
});
531+
532+
describe('when a Worldmap is created with clickthrough-links enabled to another window', () => {
533+
beforeEach(() => {
534+
535+
// Create map.
536+
ctrl.panel.clickthroughUrl = 'http://foo.bar';
537+
ctrl.panel.clickthroughOptions = {windowName: 'test' };
538+
ctrl.settings = new PluginSettings(ctrl.panel, templateSrvMock, {});
539+
worldMap.createMap();
540+
541+
// Load data and draw circles.
542+
ctrl.data = new DataBuilder()
543+
.withCountryAndValue('SE', 1)
544+
.build();
545+
worldMap.drawCircles();
546+
547+
});
548+
549+
it('should have registered a second click event', () => {
550+
expect(worldMap.circles.length).toBe(1);
551+
expect(worldMap.circles[0]._events.click.length).toBe(2);
552+
});
553+
554+
it('should do its job when actually clicked', () => {
555+
556+
// Setup interaction mock for "window.open".
557+
// https://remarkablemark.org/blog/2018/11/17/mock-window-location/
558+
Object.defineProperty(window, 'open', {
559+
configurable: true,
560+
});
561+
window.open = jest.fn();
562+
563+
// Capture interaction.
564+
worldMap.circles[0].fire('click');
565+
expect(window.open).toHaveBeenCalledWith('http://foo.bar', 'test');
566+
});
567+
568+
});
569+
570+
});

src/worldmap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ export default class WorldMap {
325325
if (linkUrl) {
326326
const clickthroughOptions = this.ctrl.settings.clickthroughOptions;
327327
circle.on('click', evt => {
328-
if (clickthroughOptions.windowName) {
328+
if (clickthroughOptions && clickthroughOptions.windowName) {
329329
window.open(linkUrl, clickthroughOptions.windowName);
330330
} else {
331331
window.location.assign(linkUrl);

src/worldmap_ctrl.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export default class WorldmapCtrl extends MetricsPanelCtrl {
111111
this.errors = new ErrorManager();
112112
this.errors.registerDomains('data', 'location');
113113

114+
this.settings = undefined;
114115
this.loadSettings();
115116

116117
this.core = new WorldmapCore(this);

0 commit comments

Comments
 (0)