Skip to content

Commit ddb4f2a

Browse files
committed
fix tests
1 parent 990562a commit ddb4f2a

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

projects/ngx-simple-charts/src/lib/sc-line-chart/sc-line-chart.component.spec.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,58 @@
1010
See the License for the specific language governing permissions and
1111
limitations under the License.
1212
*/
13+
import { SimpleChange, SimpleChanges } from '@angular/core';
1314
import { ComponentFixture, TestBed } from '@angular/core/testing';
15+
import { ChartPoint, ChartPoints } from '../model/chart-points';
1416

1517
import { ScLineChartComponent } from './sc-line-chart.component';
1618

17-
describe('NgxSimpleChartsComponent', () => {
19+
describe('ScLineChartComponent', () => {
1820
let component: ScLineChartComponent;
1921
let fixture: ComponentFixture<ScLineChartComponent>;
22+
const chartPoints : ChartPoints = {name: 'myChartPoints', xScaleHeight: 250, yScaleWidth: 250,
23+
chartPointList: []};
24+
const myChartPointList : ChartPoint[] = [{x: new Date(2021, 0,1), y: 50},{x: new Date(2021, 0,2), y: 75}];
25+
2026
beforeEach(async () => {
2127
await TestBed.configureTestingModule({
2228
declarations: [ ScLineChartComponent ]
2329
})
2430
.compileComponents();
2531
});
32+
2633
beforeEach(() => {
2734
fixture = TestBed.createComponent(ScLineChartComponent);
28-
component = fixture.componentInstance;
35+
component = fixture.componentInstance;
2936
fixture.detectChanges();
3037
});
38+
3139
it('should create', () => {
3240
expect(component).toBeTruthy();
3341
});
42+
43+
it('should create a chart', () => {
44+
const newChartPoints = JSON.parse(JSON.stringify(chartPoints));
45+
newChartPoints.chartPointList.push(...JSON.parse(JSON.stringify(myChartPointList)));
46+
component.chartPoints = [newChartPoints];
47+
component.ngAfterViewInit();
48+
fixture.detectChanges();
49+
const myElement = component.d3Svg.selectAll('path').nodes()
50+
.filter(myNode => (myNode as Element).classList.contains('line'))[0] as Element;
51+
expect(myElement.getAttribute('d')?.split(',').length).toBeGreaterThan(1);
52+
});
53+
54+
it('should update a chart', () => {
55+
const newChartPoints = JSON.parse(JSON.stringify(chartPoints));
56+
newChartPoints.chartPointList.push(...JSON.parse(JSON.stringify(myChartPointList)));
57+
newChartPoints.chartPointList.push({x: new Date(2021, 0,3), y: 100});
58+
const myChanges: SimpleChanges = {chartPoints: {currentValue: [newChartPoints], firstChange: false,
59+
previousValue: [chartPoints], isFirstChange: () => false} as SimpleChange};
60+
component.chartPoints = [newChartPoints];
61+
component.ngOnChanges(myChanges);
62+
fixture.detectChanges();
63+
const myElement = component.d3Svg.selectAll('path').nodes()
64+
.filter(myNode => (myNode as Element).classList.contains('line'))[0] as Element;
65+
expect(myElement.getAttribute('d')?.split(',').length).toBeGreaterThan(2);
66+
});
3467
});

projects/ngx-simple-charts/src/lib/sc-line-chart/sc-line-chart.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ import { ChartPoints, ChartPoint } from '../model/chart-points';
2727
export class ScLineChartComponent implements AfterViewInit, OnChanges {
2828
@ViewChild("svgchart", {static: true})
2929
private chartContainer!: ElementRef;
30-
private d3Svg!: Selection<ContainerElement, ChartPoint, HTMLElement, any>;
30+
d3Svg!: Selection<ContainerElement, ChartPoint, HTMLElement, any>;
3131
@Input()
32-
private chartPoints: ChartPoints[] = [];
32+
chartPoints: ChartPoints[] = [];
3333
private gAttribute!: Selection<SVGGElement, ChartPoint, HTMLElement, any>;
3434
private gxAttribute!: Selection<SVGGElement, ChartPoint, HTMLElement, any>;
3535
private gyAttribute!: Selection<SVGGElement, ChartPoint, HTMLElement, any>;

0 commit comments

Comments
 (0)