Skip to content

Commit acb676b

Browse files
committed
tests: Refactor map builder fixture
1 parent f24b93e commit acb676b

File tree

2 files changed

+53
-69
lines changed

2 files changed

+53
-69
lines changed

src/worldmap.test.ts

Lines changed: 22 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
import WorldMap from './worldmap';
21
import DataBuilder from '../test/data_builder';
2+
import {createBasicMap} from '../test/map_builder';
33
import $ from 'jquery';
44

5+
56
describe('Worldmap', () => {
7+
68
let worldMap;
79
let ctrl;
810

911
beforeEach(() => {
10-
setupWorldmapFixture();
12+
worldMap = createBasicMap();
13+
ctrl = worldMap.ctrl;
14+
worldMap.createMap();
15+
});
16+
17+
afterEach(() => {
18+
const fixture: HTMLElement = document.getElementById('fixture')!;
19+
document.body.removeChild(fixture);
1120
});
1221

1322
describe('when a Worldmap is created', () => {
@@ -388,49 +397,18 @@ describe('Worldmap', () => {
388397
});
389398
});
390399

391-
afterEach(() => {
392-
const fixture: HTMLElement = document.getElementById('fixture')!;
393-
document.body.removeChild(fixture);
394-
});
395-
396-
function setupWorldmapFixture() {
397-
const fixture = '<div id="fixture" class="mapcontainer"></div>';
398-
document.body.insertAdjacentHTML('afterbegin', fixture);
399-
400-
ctrl = {
401-
panel: {
402-
center: {
403-
mapCenterLatitude: 0,
404-
mapCenterLongitude: 0,
405-
initialZoom: 1,
406-
},
407-
colors: ['red', 'blue', 'green'],
408-
circleOptions: {},
409-
},
410-
tileServer: 'CartoDB Positron',
411-
};
412-
413-
// This mimics the `ctrl.panel` proxying established
414-
// by `PluginSettings` to make the tests happy.
415-
// Todo: Don't worry, this will go away.
416-
ctrl.settings = ctrl.panel;
417-
418-
worldMap = new WorldMap(ctrl, document.getElementsByClassName('mapcontainer')[0]);
419-
worldMap.createMap();
420-
}
421400
});
422401

423402
describe('WorldmapFoundation', () => {
424403
/*
425-
* Optimizations for small maps
426-
*
427-
* In order to test the `createMap()` method,
428-
* we need to pass a half-configured `WorldMap`
404+
* In order to individually configure the map before the `createMap()`
405+
* method will be invoked, we need to pass a half-configured `WorldMap`
429406
* instance into the test cases.
430407
*
431408
* We are testing the "showZoomControl" and "showAttribution"
432409
* options here to proof they actually toggle the visibility
433-
* of the respective control elements.
410+
* of the respective control elements. These have been introduced
411+
* to optimize Worldmap for small maps.
434412
*
435413
* See also https://community.hiveeyes.org/t/grafana-worldmap-panel-ng/1824/3
436414
*/
@@ -439,7 +417,13 @@ describe('WorldmapFoundation', () => {
439417
let ctrl;
440418

441419
beforeEach(() => {
442-
setupWorldmapHalfFixture();
420+
worldMap = createBasicMap();
421+
ctrl = worldMap.ctrl;
422+
});
423+
424+
afterEach(() => {
425+
const fixture: HTMLElement = document.getElementById('fixture')!;
426+
document.body.removeChild(fixture);
443427
});
444428

445429
describe('when a Worldmap is created with default parameters', () => {
@@ -476,35 +460,4 @@ describe('WorldmapFoundation', () => {
476460
});
477461
});
478462

479-
afterEach(() => {
480-
const fixture: HTMLElement = document.getElementById('fixture')!;
481-
document.body.removeChild(fixture);
482-
});
483-
484-
function setupWorldmapHalfFixture() {
485-
const fixture = '<div id="fixture" class="mapcontainer"></div>';
486-
document.body.insertAdjacentHTML('afterbegin', fixture);
487-
488-
ctrl = {
489-
panel: {
490-
center: {
491-
mapCenterLatitude: 0,
492-
mapCenterLongitude: 0,
493-
initialZoom: 1,
494-
},
495-
colors: ['red', 'blue', 'green'],
496-
circleOptions: {},
497-
showZoomControl: true,
498-
showAttribution: true,
499-
},
500-
tileServer: 'CartoDB Positron',
501-
};
502-
503-
// This mimics the `ctrl.panel` proxying established
504-
// by `PluginSettings` to make the tests happy.
505-
// Todo: Don't worry, this will go away.
506-
ctrl.settings = ctrl.panel;
507-
508-
worldMap = new WorldMap(ctrl, document.getElementsByClassName('mapcontainer')[0]);
509-
}
510463
});

test/map_builder.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import WorldMap from "../src/worldmap";
2+
3+
export function createBasicMap() {
4+
const fixture = '<div id="fixture" class="mapcontainer"></div>';
5+
document.body.insertAdjacentHTML('afterbegin', fixture);
6+
7+
let ctrl = {
8+
panel: {
9+
center: {
10+
mapCenterLatitude: 0,
11+
mapCenterLongitude: 0,
12+
initialZoom: 1,
13+
},
14+
colors: ['red', 'blue', 'green'],
15+
circleOptions: {},
16+
showZoomControl: true,
17+
showAttribution: true,
18+
},
19+
tileServer: 'CartoDB Positron',
20+
};
21+
22+
// This mimics the `ctrl.panel` proxying established
23+
// by `PluginSettings` to make the tests happy.
24+
// Todo: Don't worry, this will go away.
25+
ctrl.settings = ctrl.panel;
26+
27+
const worldMap = new WorldMap(ctrl, document.getElementsByClassName('mapcontainer')[0]);
28+
29+
return worldMap;
30+
31+
}

0 commit comments

Comments
 (0)