Skip to content

Commit 04931de

Browse files
authored
fix: count down with increment and last emitted value (#27)
added tests for new cases, refactored example app and test to render params variations with *ngFor
1 parent 0ee850a commit 04931de

File tree

5 files changed

+124
-41
lines changed

5 files changed

+124
-41
lines changed

projects/ngx-animated-counter-example/src/app/app.component.html

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,8 @@ <h1>
1111
{{ title }}
1212
</a>
1313
</h1>
14-
<div class="row">
15-
<ngx-animated-counter [params]="params0"></ngx-animated-counter>
16-
</div>
17-
<div class="row">
18-
<ngx-animated-counter [params]="params1"></ngx-animated-counter>
19-
</div>
20-
<div class="row">
21-
<ngx-animated-counter [params]="params2"></ngx-animated-counter>
22-
</div>
23-
<div class="row">
24-
<ngx-animated-counter [params]="params3"></ngx-animated-counter>
14+
<div class="row" *ngFor="let params of paramsVariations">
15+
<ngx-animated-counter [params]="params"></ngx-animated-counter>
2516
</div>
2617
</div>
2718
</div>

projects/ngx-animated-counter-example/src/app/app.component.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ describe('AppComponent', () => {
2626
expect(component.title).toEqual('@bugsplat/ngx-animated-counter');
2727
});
2828

29-
it('should render 4 ngx-animated-counters', () => {
29+
it('should render ngx-animated-counter for each params variation', () => {
3030
const compiled = fixture.debugElement.nativeElement;
31-
expect(compiled.querySelectorAll('ngx-animated-counter').length).toEqual(4);
31+
fixture.detectChanges(); // run *ngFor
32+
const renderedCounters = compiled.querySelectorAll('ngx-animated-counter');
33+
expect(renderedCounters.length).toEqual(component.paramsVariations.length);
3234
});
3335
});

projects/ngx-animated-counter-example/src/app/app.component.ts

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,33 @@ import { NgxAnimatedCounterParams } from '@bugsplat/ngx-animated-counter';
88
})
99
export class AppComponent {
1010
public readonly title: string = '@bugsplat/ngx-animated-counter';
11-
12-
public params0: NgxAnimatedCounterParams = {
13-
start: 0,
14-
end: 100,
15-
interval: 100,
16-
};
17-
public params1: NgxAnimatedCounterParams = {
18-
start: 1000,
19-
end: 10000,
20-
interval: 10,
21-
};
22-
public params2: NgxAnimatedCounterParams = {
23-
start: 100,
24-
end: 0,
25-
interval: 100,
26-
};
27-
public params3: NgxAnimatedCounterParams = {
28-
start: 0,
29-
end: 123123123123,
30-
interval: 10,
31-
increment: 123123,
32-
};
11+
public readonly paramsVariations: NgxAnimatedCounterParams[] = [
12+
{
13+
start: 0,
14+
end: 100,
15+
interval: 100,
16+
},
17+
{
18+
start: 1000,
19+
end: 10000,
20+
interval: 10,
21+
},
22+
{
23+
start: 100,
24+
end: 0,
25+
interval: 100,
26+
},
27+
{
28+
start: 0,
29+
end: 123123123123,
30+
interval: 10,
31+
increment: 123123,
32+
},
33+
{
34+
start: 100,
35+
end: 96,
36+
interval: 3000,
37+
increment: 3,
38+
},
39+
];
3340
}

projects/ngx-animated-counter/src/lib/ngx-animated-counter.component.spec.ts

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('NgxAnimatedCounter', () => {
4242
expect(component.current).toEqual(end);
4343
}));
4444

45-
it('should count by increment if provided', fakeAsync(() => {
45+
it('should count up by increment if provided', fakeAsync(() => {
4646
const interval = 10;
4747
const start = 0;
4848
const end = 1000;
@@ -63,7 +63,7 @@ describe('NgxAnimatedCounter', () => {
6363
expect(result).toEqual(increment);
6464
}));
6565

66-
it('should count by 1 if increment not provided', fakeAsync(() => {
66+
it('should count up by 1 if increment not provided', fakeAsync(() => {
6767
const interval = 10;
6868
const start = 0;
6969
const end = 1000;
@@ -81,4 +81,80 @@ describe('NgxAnimatedCounter', () => {
8181

8282
expect(result).toEqual(1);
8383
}));
84+
85+
it('should count down by increment if provided', fakeAsync(() => {
86+
const interval = 10;
87+
const start = 1000;
88+
const end = 0;
89+
const increment = 25;
90+
const totalTime = interval * (start - end);
91+
92+
component.params = {
93+
start,
94+
end,
95+
interval,
96+
increment,
97+
};
98+
99+
tick(0);
100+
const result = component.current;
101+
tick(totalTime);
102+
103+
expect(result).toEqual(start - increment);
104+
}));
105+
106+
it('should count down by 1 if increment not provided', fakeAsync(() => {
107+
const interval = 10;
108+
const start = 1000;
109+
const end = 0;
110+
const totalTime = interval * (start - end);
111+
112+
component.params = {
113+
start,
114+
end,
115+
interval,
116+
};
117+
118+
tick(0);
119+
const result = component.current;
120+
tick(totalTime);
121+
122+
expect(result).toEqual(start - 1);
123+
}));
124+
125+
it('should finish counting up with end value when value delta is not divisible by increment', fakeAsync(() => {
126+
const interval = 10;
127+
const start = 90;
128+
const end = 100;
129+
const increment = 7;
130+
const totalTime = interval * (end - start);
131+
132+
component.params = {
133+
start,
134+
end,
135+
interval,
136+
increment,
137+
};
138+
139+
tick(totalTime);
140+
expect(component.current).toEqual(end);
141+
}));
142+
143+
it('should finish counting down with end value when value delta is not divisible by increment', fakeAsync(() => {
144+
const interval = 10;
145+
const start = 100;
146+
const end = 90;
147+
const increment = 7;
148+
const totalTime = interval * (start - end);
149+
150+
component.params = {
151+
start,
152+
end,
153+
interval,
154+
increment,
155+
};
156+
157+
tick(totalTime);
158+
expect(component.current).toEqual(end);
159+
}));
84160
});

projects/ngx-animated-counter/src/lib/ngx-animated-counter.component.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { Component, Input, OnDestroy } from '@angular/core';
22
import { Subscription, timer, Subject } from 'rxjs';
3-
import { mapTo, scan, startWith, switchMap, takeWhile } from 'rxjs/operators';
3+
import {
4+
endWith,
5+
mapTo,
6+
scan,
7+
startWith,
8+
switchMap,
9+
takeWhile,
10+
} from 'rxjs/operators';
411
import { NgxAnimatedCounterParams } from './ngx-animated-counter-params';
512

613
@Component({
@@ -23,12 +30,12 @@ export class NgxAnimatedCounterComponent implements OnDestroy {
2330
startWith(this.current),
2431
scan((acc: number, curr: number) => {
2532
if (value.increment) {
26-
return acc + value.increment;
33+
return acc + value.increment * curr;
2734
}
28-
2935
return acc + curr;
3036
}),
31-
takeWhile(this.isApproachingRange(end, this.current))
37+
takeWhile(this.isApproachingRange(end, this.current)),
38+
endWith(value.end)
3239
);
3340
})
3441
)

0 commit comments

Comments
 (0)