-
Notifications
You must be signed in to change notification settings - Fork 4
/
line-chart.d3.spec.ts
95 lines (82 loc) · 3.53 KB
/
line-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
89
90
91
92
93
94
95
import { ElementRef } from '@angular/core';
import { Subject } from 'rxjs';
import { LineChartD3 } from './line-chart.d3';
import { mockData, mockPoint } from '../utils/mocks.spec';
import { TimeSeriesPoint } from '../datasets/metas/types';
import { LineChartDatum } from '../components/line-chart/line-chart.component';
import { RenderOptions } from './xy-chart.d3';
interface SubjectRenderOptions extends RenderOptions<TimeSeriesPoint, LineChartDatum> {
data$: Subject<LineChartDatum[]>;
activePoint$: Subject<TimeSeriesPoint | null>;
}
describe('LineChartD3', () => {
let containerElement: HTMLElement;
let svgElement: HTMLElement;
let renderOptions: SubjectRenderOptions;
let lineChartD3: LineChartD3;
const transitionDelay = 350;
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<LineChartDatum[]>(),
activePoint$: new Subject<TimeSeriesPoint | null>(),
};
lineChartD3 = new LineChartD3(renderOptions);
});
afterEach(() => {
lineChartD3.clear();
});
it('should instantiate.', () => {
expect(lineChartD3).toBeInstanceOf(LineChartD3);
});
it('should update the data upon changes.', async () => {
lineChartD3.render();
renderOptions.data$.next([mockData[0]]);
await new Promise(resolve => setTimeout(resolve, transitionDelay));
const pathElement = svgElement.querySelector('.xy_chart-data path');
expect(pathElement).not.toBe(null);
if (pathElement !== null) {
const dAttribute = pathElement.getAttribute('d');
renderOptions.data$.next([mockData[1]]);
await new Promise(resolve => setTimeout(resolve, transitionDelay));
const newDAttribute = pathElement.getAttribute('d');
expect(newDAttribute).not.toBe(dAttribute);
}
});
it('should render the active point.', async () => {
lineChartD3.render();
const activePointElement = svgElement.querySelector('.line_chart-active_point');
expect(activePointElement).toBeTruthy();
});
it('should update the active point upon changes.', async () => {
lineChartD3.render();
const activePointElement = svgElement.querySelector('.line_chart-active_point')!;
const transformAttribute = activePointElement.getAttribute('transform');
renderOptions.activePoint$.next(mockPoint);
await new Promise(resolve => setTimeout(resolve, transitionDelay));
const newTransformAttribute = activePointElement.getAttribute('transform');
expect(newTransformAttribute).not.toBe(transformAttribute);
});
it('should only show the active point when non-null.', async () => {
lineChartD3.render();
const circleElement = svgElement.querySelector('.line_chart-active_point')!;
renderOptions.activePoint$.next(mockPoint);
await new Promise(resolve => setTimeout(resolve, transitionDelay));
expect(circleElement.getAttribute('display')).toBe('inherit');
renderOptions.activePoint$.next(null);
await new Promise(resolve => setTimeout(resolve, transitionDelay));
expect(circleElement.getAttribute('display')).toBe('none');
});
it('should render the x and y axis.', () => {
lineChartD3.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);
});
});