From: Pedro Gonzalez Gomez Date: Thu, 19 Oct 2023 09:29:36 +0000 (+0200) Subject: mgr/dashboard: dashboard area chart unit test X-Git-Tag: v18.2.4~284^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=ec743529d722fb6e5545a67843ff9404d6c17e19;p=ceph.git mgr/dashboard: dashboard area chart unit test Fixes: https://tracker.ceph.com/issues/63243 Signed-off-by: Pedro Gonzalez Gomez (cherry picked from commit 445724fa61a27071bce02afa084bfc1437acd271) --- diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/dashboard-v3/dashboard-area-chart/dashboard-area-chart.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/dashboard-v3/dashboard-area-chart/dashboard-area-chart.component.spec.ts index 0501ac75dde..ec71070544f 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/dashboard-v3/dashboard-area-chart/dashboard-area-chart.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/dashboard-v3/dashboard-area-chart/dashboard-area-chart.component.spec.ts @@ -1,4 +1,4 @@ -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'; @@ -28,9 +28,65 @@ describe('DashboardAreaChartComponent', () => { 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); + }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/dashboard-v3/dashboard-area-chart/dashboard-area-chart.component.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/dashboard-v3/dashboard-area-chart/dashboard-area-chart.component.ts index 57713aeb3f7..cbf97691d27 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/dashboard-v3/dashboard-area-chart/dashboard-area-chart.component.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/dashboard-v3/dashboard-area-chart/dashboard-area-chart.component.ts @@ -1,4 +1,4 @@ -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'; @@ -14,7 +14,7 @@ import 'chartjs-adapter-moment'; 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() @@ -175,19 +175,16 @@ export class DashboardAreaChartComponent implements OnChanges, AfterViewInit { }; } - 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] @@ -196,7 +193,8 @@ export class DashboardAreaChartComponent implements OnChanges, AfterViewInit { 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] diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/formatter.service.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/formatter.service.ts index b5e0b9475a4..c1ad14b4742 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/formatter.service.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/formatter.service.ts @@ -40,7 +40,7 @@ export class FormatterService { */ formatNumberFromTo( n: any, - units: any, + units: string = '', targetedUnits: string = '', conversionFactor: number, unitsArray: string[], @@ -52,6 +52,9 @@ export class FormatterService { if (!_.isNumber(n)) { return '-'; } + if (!unitsArray) { + return '-'; + } const unitsArrayLowerCase = unitsArray.map((str) => str.toLowerCase()); if ( !unitsArrayLowerCase.includes(units.toLowerCase()) ||