-
Notifications
You must be signed in to change notification settings - Fork 4
/
pie-chart.d3.spec.ts
79 lines (69 loc) · 2.32 KB
/
pie-chart.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
import { ElementRef } from '@angular/core';
import { Subject } from 'rxjs';
import { PieChartD3 } from './pie-chart.d3';
import { CategoricalPoint } from '../datasets/metas/types';
import { PieChartDatum } from '../components/pie-chart/pie-chart.component';
import { RenderOptions } from './xy-chart.d3';
interface SubjectRenderOptions extends RenderOptions<CategoricalPoint, PieChartDatum> {
data$: Subject<PieChartDatum[]>;
}
const mockData = [
{
label: 'Datum 0',
points: [
{ x: 'A', y: 10 },
{ x: 'B', y: 20 },
{ x: 'C', y: 30 },
{ x: 'D', y: 40 },
],
},
{
label: 'Datum 1',
points: [
{ x: 'A', y: 100 },
{ x: 'B', y: 30 },
{ x: 'C', y: 70 },
{ x: 'D', y: 120 },
],
},
];
describe('PieChartD3', () => {
let containerElement: HTMLElement;
let svgElement: HTMLElement;
let renderOptions: SubjectRenderOptions;
let pieChartD3: PieChartD3;
const transitionDelay = 1000;
beforeEach(() => {
svgElement = document.createElement('svg');
containerElement = document.createElement('div');
containerElement.appendChild(svgElement);
renderOptions = {
elementRef: new ElementRef<HTMLElement>(containerElement),
width: 256,
height: 256,
data$: new Subject<PieChartDatum[]>(),
};
pieChartD3 = new PieChartD3(renderOptions);
});
afterEach(() => {
pieChartD3.clear();
});
it('should instantiate.', () => {
expect(pieChartD3).toBeInstanceOf(PieChartD3);
});
it('should update the data upon changes.', async () => {
pieChartD3.render();
renderOptions.data$.next([mockData[0]]);
await new Promise(resolve => setTimeout(resolve, transitionDelay));
const pathElements = Array.from(svgElement.querySelectorAll('path'));
expect(pathElements.length).toBeGreaterThan(0);
if (pathElements.length > 0) {
const dAttribute = pathElements.map(el => el.getAttribute('d'));
renderOptions.data$.next([mockData[1]]);
await new Promise(resolve => setTimeout(resolve, transitionDelay));
const newPathElements = Array.from(svgElement.querySelectorAll('path'));
const newDAttribute = newPathElements.map(el => el.getAttribute('d'));
expect(newDAttribute).not.toEqual(dAttribute);
}
});
});