From ee465c089027632c396b3f0d1599c1c6d7408b5b Mon Sep 17 00:00:00 2001 From: Jesse Lucas Date: Sat, 4 Apr 2020 16:13:12 -0400 Subject: [PATCH] update progress service with get percentValue and table tests --- src/app/services/progress.service.spec.ts | 24 +++++++++++++++++++ src/app/services/progress.service.ts | 28 ++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/app/services/progress.service.spec.ts b/src/app/services/progress.service.spec.ts index 79d482a8f..fb55baca2 100644 --- a/src/app/services/progress.service.spec.ts +++ b/src/app/services/progress.service.spec.ts @@ -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 = 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); + } + }); }); diff --git a/src/app/services/progress.service.ts b/src/app/services/progress.service.ts index 23420fc16..c24a1330a 100644 --- a/src/app/services/progress.service.ts +++ b/src/app/services/progress.service.ts @@ -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; + } +} \ No newline at end of file