-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathxy-chart.d3.spec.ts
113 lines (93 loc) · 3.38 KB
/
xy-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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { XYChartD3, RenderOptions } from './xy-chart.d3';
import { Subject } from 'rxjs';
import { TimeSeriesPoint } from '../datasets/metas/types';
import { TimeSeriesDatum } from '../datasets/queries/time-series.query';
import { mockData } from '../utils/mocks.spec';
import { ElementRef } from '@angular/core';
import { LineChartDatum } from '../components/line-chart/line-chart.component';
type TestLegendItemStyle = {};
type TestDatum = TimeSeriesDatum<TestLegendItemStyle>;
interface SubjectRenderOptions extends RenderOptions<TimeSeriesPoint, TestDatum> {
data$: Subject<TestDatum[]>;
activePoint$: Subject<TimeSeriesPoint | null>;
}
describe('XYChartD3', () => {
// since XYChartD3 is an abstract class, make a concrete child class
class TestXYChartD3 extends XYChartD3<TimeSeriesPoint, TestDatum> {
// the flags below will be used to check if the methods have been called at the right time
axisFlag = 0;
dataFlag = 0;
activePointFlag = 0;
protected renderAxis() {
this.axisFlag = 1;
}
protected updateAxis() {
this.axisFlag = 2;
}
protected renderData() {
super.renderData();
this.dataFlag = 1;
}
protected updateData(data: TestDatum[]) {
this.dataFlag = 2;
}
protected renderActivePoint() {
this.activePointFlag = 1;
}
protected updateActivePoint(activePoint: TimeSeriesPoint | null) {
this.activePointFlag = 2;
}
protected appendLegendItemIcon(itemG: d3.Selection<SVGGElement, unknown, null, undefined>, datum: TestDatum) {
}
}
let containerElement: HTMLElement;
let svgElement: HTMLElement;
let renderOptions: SubjectRenderOptions;
let xyChartD3: TestXYChartD3;
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>(),
};
xyChartD3 = new TestXYChartD3(renderOptions);
});
afterEach(() => {
xyChartD3.clear();
});
it('should instantiate.', () => {
expect(xyChartD3).toBeInstanceOf(XYChartD3);
});
it('should render the data and update upon changes.', () => {
expect(xyChartD3.dataFlag).toBe(0);
xyChartD3.render();
expect(xyChartD3.dataFlag).toBe(1);
renderOptions.data$.next(mockData);
expect(xyChartD3.dataFlag).toBe(2);
});
it('should render the active datum and update upon changes.', () => {
expect(xyChartD3.activePointFlag).toBe(0);
xyChartD3.render();
expect(xyChartD3.activePointFlag).toBe(1);
renderOptions.activePoint$.next(null);
expect(xyChartD3.activePointFlag).toBe(2);
});
it('should render the axis.', () => {
expect(xyChartD3.axisFlag).toBe(0);
xyChartD3.render();
expect(xyChartD3.axisFlag).toBe(1);
renderOptions.data$.next(mockData);
expect(xyChartD3.axisFlag).toBe(2);
});
it('should render the legend.', () => {
xyChartD3.render();
renderOptions.data$.next(mockData);
const textElements = svgElement.querySelectorAll('.xy_chart-legend text');
expect(textElements.length).toBe(mockData.length);
});
});