-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbar-chart.d3.spec.ts
88 lines (76 loc) · 2.64 KB
/
bar-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
80
81
82
83
84
85
86
87
88
import { ElementRef } from '@angular/core';
import { Subject } from 'rxjs';
import { BarChartD3 } from './bar-chart.d3';
import { CategoricalPoint } from '../datasets/metas/types';
import { BarChartDatum } from '../components/bar-chart/bar-chart.component';
import { RenderOptions } from './xy-chart.d3';
interface SubjectRenderOptions extends RenderOptions<CategoricalPoint, BarChartDatum> {
data$: Subject<BarChartDatum[]>;
}
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('BarChartD3', () => {
let containerElement: HTMLElement;
let svgElement: HTMLElement;
let renderOptions: SubjectRenderOptions;
let barChartD3: BarChartD3;
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<BarChartDatum[]>(),
};
barChartD3 = new BarChartD3(renderOptions);
});
afterEach(() => {
barChartD3.clear();
});
it('should instantiate.', () => {
expect(barChartD3).toBeInstanceOf(BarChartD3);
});
it('should update the data upon changes.', async () => {
barChartD3.render();
renderOptions.data$.next([mockData[0]]);
await new Promise(resolve => setTimeout(resolve, transitionDelay));
const barElements = Array.from(svgElement.querySelectorAll('.bar'));
expect(barElements.length).toBeGreaterThan(0);
if (barElements.length > 0) {
const widthAttributes = barElements.map(el => el.getAttribute('width'));
renderOptions.data$.next([mockData[1]]);
await new Promise(resolve => setTimeout(resolve, transitionDelay));
const newBarElements = Array.from(svgElement.querySelectorAll('.bar'));
const newWidthAttributes = newBarElements.map(el => el.getAttribute('width'));
expect(newWidthAttributes).not.toEqual(widthAttributes);
}
});
it('should render the x and y axis.', () => {
barChartD3.render();
const xAxisElement = svgElement.querySelector('.xy_chart-x_axis');
const yAxisElement = svgElement.querySelector('.xy_chart-y_axis');
expect(xAxisElement).not.toBe(null);
expect(yAxisElement).not.toBe(null);
});
});