From 9f5a93ea1557b5e08d660f6ea28d914cda2ec883 Mon Sep 17 00:00:00 2001 From: Timon Borter Date: Mon, 6 Nov 2023 21:55:21 +0100 Subject: [PATCH] fix(simulator-ui): round summary to two fixed decimals --- .../test-result-summary.component.spec.ts | 30 +++++++++++++++---- .../app/home/test-result-summary.component.ts | 13 +++++--- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/simulator-ui/src/main/webapp/app/home/test-result-summary.component.spec.ts b/simulator-ui/src/main/webapp/app/home/test-result-summary.component.spec.ts index e0f7de1dc..2afdda7ae 100644 --- a/simulator-ui/src/main/webapp/app/home/test-result-summary.component.spec.ts +++ b/simulator-ui/src/main/webapp/app/home/test-result-summary.component.spec.ts @@ -38,7 +38,26 @@ describe('TestResultSummaryComponent', () => { }); describe('ngOnInit', () => { - it('should correctly calculate percentages', fakeAsync(() => { + it('should correctly calculate fixed percentages', fakeAsync(() => { + const mockData = new HttpResponse({ + body: { + total: 3, + successful: 2, + failed: 1, + }, + }); + + testResultService.countByStatus.mockReturnValue(of(mockData)); + + component.ngOnInit(); + tick(); + + expect(component.testResults).toEqual(mockData.body); + expect(component.successfulPercentage).toEqual('66.67'); + expect(component.failedPercentage).toEqual('33.33'); + })); + + it('should return even numbers with even results', fakeAsync(() => { const mockData = new HttpResponse({ body: { total: 2, @@ -53,8 +72,8 @@ describe('TestResultSummaryComponent', () => { tick(); expect(component.testResults).toEqual(mockData.body); - expect(component.successfulPercentage).toEqual(50); - expect(component.failedPercentage).toEqual(50); + expect(component.successfulPercentage).toEqual('50'); + expect(component.failedPercentage).toEqual('50'); })); it('default to a zero-result', fakeAsync(() => { @@ -70,8 +89,9 @@ describe('TestResultSummaryComponent', () => { successful: 0, failed: 0, }); - expect(component.successfulPercentage).toEqual(0); - expect(component.failedPercentage).toEqual(0); + + expect(component.successfulPercentage).toEqual('0'); + expect(component.failedPercentage).toEqual('0'); })); }); }); diff --git a/simulator-ui/src/main/webapp/app/home/test-result-summary.component.ts b/simulator-ui/src/main/webapp/app/home/test-result-summary.component.ts index 5428b9861..e8e0b9c61 100644 --- a/simulator-ui/src/main/webapp/app/home/test-result-summary.component.ts +++ b/simulator-ui/src/main/webapp/app/home/test-result-summary.component.ts @@ -16,8 +16,8 @@ import SharedModule from 'app/shared/shared.module'; export default class TestResultSummaryComponent implements OnInit { testResults: TestResultsByStatus | null = null; - successfulPercentage = 0; - failedPercentage = 0; + successfulPercentage = '0'; + failedPercentage = '0'; statusSuccess = STATUS_SUCCESS; statusFailed = STATUS_FAILED; @@ -36,9 +36,14 @@ export default class TestResultSummaryComponent implements OnInit { this.testResults = testResults; if (testResults.total > 0) { - this.successfulPercentage = (testResults.successful / testResults.total) * 100; - this.failedPercentage = (testResults.failed / testResults.total) * 100; + this.successfulPercentage = this.toFixedDecimalIfNotMatching((testResults.successful / testResults.total) * 100); + this.failedPercentage = this.toFixedDecimalIfNotMatching((testResults.failed / testResults.total) * 100); } }); } + + private toFixedDecimalIfNotMatching(percentage: number): string { + const fixed = percentage.toFixed(2); + return fixed.endsWith('.00') ? fixed.slice(0, fixed.length - 3) : fixed; + } }