From cce1662db3620217caa48dfc747d0472891f502c Mon Sep 17 00:00:00 2001 From: Christina Braun <11138584+chrstnbrn@users.noreply.github.com> Date: Fri, 4 Oct 2019 16:32:31 +0200 Subject: [PATCH] fix(progressbar): display progressbar correctly when max is zero closes #3386 --- src/progressbar/progressbar.spec.ts | 11 +++++++---- src/progressbar/progressbar.ts | 8 +++++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/progressbar/progressbar.spec.ts b/src/progressbar/progressbar.spec.ts index 105c67c340..521e778732 100644 --- a/src/progressbar/progressbar.spec.ts +++ b/src/progressbar/progressbar.spec.ts @@ -58,6 +58,13 @@ describe('ngb-progressbar', () => { expect(progressCmp.getPercentValue()).toBe(20); }); + it('should calculate the percentage (custom max size of zero)', () => { + progressCmp.max = 0; + + progressCmp.value = 25; + expect(progressCmp.getPercentValue()).toBe(100); + }); + it('should set the value to 0 for negative numbers', () => { progressCmp.value = -20; expect(progressCmp.getValue()).toBe(0); @@ -120,7 +127,6 @@ describe('ngb-progressbar', () => { expect(getBarWidth(fixture.nativeElement)).toBe('5%'); }); - it('accepts a value and max value above default values', () => { const html = ''; const fixture = createTestComponent(html); @@ -128,7 +134,6 @@ describe('ngb-progressbar', () => { expect(getBarWidth(fixture.nativeElement)).toBe('100%'); }); - it('accepts a custom type', () => { const html = ''; const fixture = createTestComponent(html); @@ -151,7 +156,6 @@ describe('ngb-progressbar', () => { expect(getProgressbar(fixture.nativeElement)).not.toHaveCssClass('progress-bar-animated'); }); - it('accepts striped as normal attr', () => { const html = ''; const fixture = createTestComponent(html); @@ -163,7 +167,6 @@ describe('ngb-progressbar', () => { expect(getProgressbar(fixture.nativeElement)).not.toHaveCssClass('progress-bar-striped'); }); - it('should not add "false" CSS class', () => { const html = ''; const fixture = createTestComponent(html); diff --git a/src/progressbar/progressbar.ts b/src/progressbar/progressbar.ts index cbc39fa736..9170ba0ed3 100644 --- a/src/progressbar/progressbar.ts +++ b/src/progressbar/progressbar.ts @@ -73,5 +73,11 @@ export class NgbProgressbar { getValue() { return getValueInRange(this.value, this.max); } - getPercentValue() { return 100 * this.getValue() / this.max; } + getPercentValue() { + if (!this.max) { + return 100; + } + + return (100 * this.getValue()) / this.max; + } }