update progress service with get percentValue and table tests

This commit is contained in:
Jesse Lucas 2020-04-04 16:13:12 -04:00
parent 236816cb93
commit ee465c0890
2 changed files with 51 additions and 1 deletions

View File

@ -1,6 +1,7 @@
import { TestBed } from '@angular/core/testing';
import { ProgressService } from './progress.service';
import { stringToKeyValue } from '@angular/flex-layout/extended/typings/style/style-transforms';
describe('ProgressService', () => {
let service: ProgressService;
@ -13,4 +14,27 @@ describe('ProgressService', () => {
it('should be created', () => {
expect(service).toBeTruthy();
});
it('#percentValue should return 0 - 100', () => {
interface iTest {
total: number,
progress: number,
expected: string
}
const tests: Map<string, iTest> = new Map([
["default", { total: 0, progress: 0, expected: '0' }],
["NaN return 0", { total: 0, progress: 100, expected: '0' }],
["greater than 100 return 100", { total: 10, progress: 100, expected: '100' }],
["valid", { total: 100, progress: 100, expected: '100' }],
["valid", { total: 100, progress: 50, expected: '50' }],
["test floor", { total: 133, progress: 41, expected: '30' }],
]);
service = new ProgressService();
for (let test of tests.values()) {
service.total = test.total;
service.updateProgress(test.progress);
expect(service.percentValue).toBe(test.expected);
}
});
});

View File

@ -4,6 +4,32 @@ import { Injectable } from '@angular/core';
providedIn: 'root'
})
export class ProgressService {
private progress: number = 0;
private _total: number = 0;
set total(t: number) {
this._total = t;
}
get percentValue(): string {
let p: number = Math.floor((this.progress / this._total) * 100);
console.log("P?!", NaN)
if (p < 0 || isNaN(p) || p === Infinity) {
p = 0;
} else if (p > 100) {
p = 100;
}
return p.toString();
}
constructor() { }
}
updateProgress(n: number) {
if (n < 0) {
n = 0
} else if (n > 100) {
n = 100
}
this.progress = n;
}
}