-
Notifications
You must be signed in to change notification settings - Fork 4
/
geo-map.d3.spec.ts
150 lines (117 loc) · 4.21 KB
/
geo-map.d3.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { ElementRef } from '@angular/core';
import { Subject } from 'rxjs';
import { GeoMapD3, RenderOptions } from './geo-map.d3';
import { GeoDatum } from '../datasets/queries/geo.query';
import { fetchWorld } from '../datasets/geo.dataset';
import { Territory, TerritoryLevel, World } from '../datasets/geo.types';
import { atlantaCityId, southKoreaCountryId } from '../utils/mocks.spec';
import * as d3 from 'd3';
import { waitFor } from '../utils/misc';
interface SubjectRenderOptions extends RenderOptions {
data$: Subject<GeoDatum[]>;
filteringTerritory$: Subject<Territory | null>;
}
const { CONTINENT, SUBCONTINENT, COUNTRY, CITY } = TerritoryLevel;
describe('GeoMapD3', () => {
class MockGeoMapD3 extends GeoMapD3 {
public projection: d3.GeoProjection;
scaleTo(scale) {
this.zoom.scaleTo(this.svg, scale);
}
translateBy(x, y) {
this.zoom.translateBy(this.svg, x, y);
}
}
let containerElement: HTMLElement;
let svgElement: HTMLElement;
let renderOptions: SubjectRenderOptions;
let geoMapD3: MockGeoMapD3;
let world: World;
beforeAll(async () => {
world = await fetchWorld();
});
beforeEach(() => {
svgElement = document.createElement('svg');
containerElement = document.createElement('div');
containerElement.appendChild(svgElement);
renderOptions = {
elementRef: new ElementRef<HTMLElement>(containerElement),
width: 256,
height: 256,
world,
data$: new Subject<GeoDatum[]>(),
activeDatumIndex$: new Subject<number>(),
filteringTerritory$: new Subject<Territory | null>(),
};
geoMapD3 = new MockGeoMapD3(renderOptions);
});
afterEach(() => {
geoMapD3.clear();
});
it('should instantiate.', () => {
expect(geoMapD3).toBeInstanceOf(GeoMapD3);
});
it('should render the map.', () => {
geoMapD3.render();
const landElement = svgElement.querySelector('.geo_map-land');
const boundaryElement = svgElement.querySelector('.geo_map-boundary');
expect(landElement).toBeTruthy();
expect(boundaryElement).toBeTruthy();
});
it('should rerender the map on zooming.', () => {
geoMapD3.render();
geoMapD3.scaleTo(50);
const landElement = svgElement.querySelector('.geo_map-land')!;
const dAttribute = landElement.getAttribute('d');
geoMapD3.scaleTo(100);
const newDAttribute = landElement.getAttribute('d');
expect(newDAttribute).not.toBe(dAttribute);
});
it('should rerender the map on panning.', () => {
geoMapD3.render();
geoMapD3.translateBy(0, 0);
const landElement = svgElement.querySelector('.geo_map-land')!;
const dAttribute = landElement.getAttribute('d');
geoMapD3.translateBy(10, 10);
const newDAttribute = landElement.getAttribute('d');
expect(newDAttribute).not.toBe(dAttribute);
});
it('should update territory paths upon data change.', () => {
geoMapD3.render();
const data = [{
territory: world[COUNTRY][southKoreaCountryId],
values: { activeUser: 123 },
}];
renderOptions.data$.next(data);
const territoryCount = svgElement.querySelectorAll('path.geo_map-territory').length;
expect(territoryCount).toBe(data.length);
});
it('should update city circles upon data change.', () => {
geoMapD3.render();
const data = [{
territory: world[CITY][atlantaCityId],
values: { activeUser: 234 },
}];
renderOptions.data$.next(data);
const cityCount = svgElement.querySelectorAll('circle.geo_map-territory').length;
expect(cityCount).toBe(data.length);
});
it('should update projection upon filtering territory changes.', async () => {
geoMapD3.render();
const translate = geoMapD3.projection.translate();
const scale = geoMapD3.projection.scale();
const city = world[CITY][atlantaCityId];
const data = [{
territory: city,
values: { activeUser: 234 },
}];
renderOptions.data$.next(data);
renderOptions.filteringTerritory$.next(city);
// wait for the first frame of animation to happen
await waitFor(50);
const newTranslate = geoMapD3.projection.translate();
const newScale = geoMapD3.projection.scale();
expect(translate).not.toEqual(newTranslate);
expect(scale).not.toEqual(newScale);
});
});