|
1 | 1 | import DataBuilder from '../test/data_builder';
|
2 | 2 | import {createBasicMap} from '../test/map_builder';
|
3 | 3 | import $ from 'jquery';
|
| 4 | +import PluginSettings from "./settings"; |
| 5 | +import { TemplateSrv } from 'grafana/app/features/templating/template_srv'; |
4 | 6 |
|
5 | 7 |
|
6 | 8 | describe('Worldmap', () => {
|
@@ -461,3 +463,108 @@ describe('WorldmapFoundation', () => {
|
461 | 463 | });
|
462 | 464 |
|
463 | 465 | });
|
| 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 | +}); |
0 commit comments