-import { NO_ERRORS_SCHEMA } from '@angular/core';
+import { NO_ERRORS_SCHEMA, SimpleChange } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { CssHelper } from '~/app/shared/classes/css-helper';
beforeEach(() => {
fixture = TestBed.createComponent(DashboardAreaChartComponent);
component = fixture.componentInstance;
+ component.data = [
+ [1, '110'],
+ [3, '130']
+ ];
});
it('should create', () => {
expect(component).toBeTruthy();
});
+
+ it('should have a chart', () => {
+ const chartElement = fixture.debugElement.nativeElement.querySelector('canvas');
+ expect(chartElement).toBeTruthy();
+ });
+
+ it('should have two datasets', () => {
+ component.data2 = [
+ [2, '120'],
+ [4, '140']
+ ];
+ expect(component.chartData.dataset[0].data).toBeDefined();
+ expect(component.chartData.dataset[1].data).toBeDefined();
+ });
+
+ it('should set label', () => {
+ component.label = 'Write';
+ expect(component.label).toBe('Write');
+ });
+
+ it('should transform and update data', () => {
+ expect(component.chartData.dataset[0].data).toEqual([{ x: 0, y: 0 }]);
+
+ component.ngOnChanges({ data: new SimpleChange(null, component.data, false) });
+
+ expect(component.chartData.dataset[0].data).toEqual([
+ { x: 1000, y: 110 },
+ { x: 3000, y: 130 }
+ ]);
+ });
+
+ it('should set currentData to last value', () => {
+ component.ngOnChanges({ data: new SimpleChange(null, component.data, false) });
+ expect(component.currentData).toBe('130');
+ });
+
+ it('should keep data units consistency', () => {
+ // Timeout to be able to access chart object
+ setTimeout(() => {
+ fixture.detectChanges();
+
+ component.data = [
+ [1, '1100'],
+ [3, '1300']
+ ];
+ component.dataUnits = 'B';
+ component.ngOnChanges({ data: new SimpleChange(null, component.data, false) });
+
+ expect(component.currentDataUnits).toBe('KiB');
+ expect(component.chartDataUnits).toBe('KiB');
+ }, 1000);
+ });
});
-import { AfterViewInit, Component, Input, OnChanges, ViewChild } from '@angular/core';
+import { Component, Input, OnChanges, SimpleChanges, ViewChild } from '@angular/core';
import { CssHelper } from '~/app/shared/classes/css-helper';
import { DimlessBinaryPipe } from '~/app/shared/pipes/dimless-binary.pipe';
templateUrl: './dashboard-area-chart.component.html',
styleUrls: ['./dashboard-area-chart.component.scss']
})
-export class DashboardAreaChartComponent implements OnChanges, AfterViewInit {
+export class DashboardAreaChartComponent implements OnChanges {
@ViewChild(BaseChartDirective) chart: BaseChartDirective;
@Input()
};
}
- ngOnChanges(): void {
- this.updateChartData();
+ ngOnChanges(changes: SimpleChanges): void {
+ this.updateChartData(changes);
}
- ngAfterViewInit(): void {
- this.updateChartData();
- }
-
- private updateChartData(): void {
+ private updateChartData(changes: SimpleChanges): void {
this.chartData.dataset[0].label = this.label;
this.chartData.dataset[1].label = this.label2;
this.setChartTicks();
- if (this.data) {
+ if (changes.data && changes.data.currentValue) {
+ this.data = changes.data.currentValue;
this.chartData.dataset[0].data = this.formatData(this.data);
[this.currentData, this.currentDataUnits] = this.convertUnits(
this.data[this.data.length - 1][1]
this.maxValue
).split(' ');
}
- if (this.data2) {
+ if (changes.data2 && changes.data2.currentValue) {
+ this.data2 = changes.data2.currentValue;
this.chartData.dataset[1].data = this.formatData(this.data2);
[this.currentData2, this.currentDataUnits2] = this.convertUnits(
this.data2[this.data2.length - 1][1]