Skip to content

Commit

Permalink
fix(simulator-ui): round summary to two fixed decimals
Browse files Browse the repository at this point in the history
  • Loading branch information
bbortt committed Nov 6, 2023
1 parent 51bdef0 commit 408b563
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(() => {
Expand All @@ -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');
}));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}

0 comments on commit 408b563

Please sign in to comment.