Skip to content

Commit d34157e

Browse files
committed
Merge pull request siddii#69 from creativedrewy/master
Added event handler so users can set countdown timer value dynamically.
2 parents f4f6fce + a19ced2 commit d34157e

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

app/js/timer.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,16 @@ angular.module('timer', [])
157157
$scope.addCDSeconds(extraSeconds);
158158
});
159159
});
160+
161+
$scope.$on('timer-set-countdown-seconds', function(e, countdownSeconds) {
162+
if (!$scope.isRunning) {
163+
$scope.clear();
164+
}
165+
166+
$scope.countdown = countdownSeconds;
167+
$scope.millis = countdownSeconds * 1000;
168+
calculateTimeUnits();
169+
});
160170
} else {
161171
$scope.millis = 0;
162172
}

test/unit/timerSetTimeTest.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
describe('timer-set-countdown-seconds event handling tests', function () {
4+
beforeEach(module('timer'));
5+
6+
it('should call the event and set single digit seconds correctly', function () {
7+
inject(function ($compile, $rootScope, $timeout) {
8+
var scope = $rootScope.$new();
9+
var element = $compile('<timer countdown="10" interval="1000" autostart="false">{{sseconds}}</timer>')(scope);
10+
scope.$digest();
11+
12+
scope.$broadcast('timer-set-countdown-seconds', 5);
13+
14+
$timeout(function () {
15+
scope.$digest();
16+
expect(element.html().indexOf('05')).toBeGreaterThan(-1);
17+
}, 500);
18+
19+
$timeout.flush();
20+
});
21+
});
22+
23+
it('should call the event and set larger second values correctly', function () {
24+
inject(function ($compile, $rootScope, $timeout) {
25+
var scope = $rootScope.$new();
26+
var element = $compile('<timer countdown="10" interval="1000" autostart="false">{{mminutes}}:{{sseconds}}</timer>')(scope);
27+
scope.$digest();
28+
29+
scope.$broadcast('timer-set-countdown-seconds', 135);
30+
31+
$timeout(function () {
32+
scope.$digest();
33+
expect(element.html().indexOf('02:15')).toBeGreaterThan(-1);
34+
}, 500);
35+
36+
$timeout.flush();
37+
});
38+
});
39+
});

0 commit comments

Comments
 (0)