Skip to content

Commit

Permalink
Merge pull request siddii#69 from creativedrewy/master
Browse files Browse the repository at this point in the history
Added event handler so users can set countdown timer value dynamically.
  • Loading branch information
siddii committed May 2, 2014
2 parents f4f6fce + a19ced2 commit d34157e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/js/timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ angular.module('timer', [])
$scope.addCDSeconds(extraSeconds);
});
});

$scope.$on('timer-set-countdown-seconds', function(e, countdownSeconds) {
if (!$scope.isRunning) {
$scope.clear();
}

$scope.countdown = countdownSeconds;
$scope.millis = countdownSeconds * 1000;
calculateTimeUnits();
});
} else {
$scope.millis = 0;
}
Expand Down
39 changes: 39 additions & 0 deletions test/unit/timerSetTimeTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

describe('timer-set-countdown-seconds event handling tests', function () {
beforeEach(module('timer'));

it('should call the event and set single digit seconds correctly', function () {
inject(function ($compile, $rootScope, $timeout) {
var scope = $rootScope.$new();
var element = $compile('<timer countdown="10" interval="1000" autostart="false">{{sseconds}}</timer>')(scope);
scope.$digest();

scope.$broadcast('timer-set-countdown-seconds', 5);

$timeout(function () {
scope.$digest();
expect(element.html().indexOf('05')).toBeGreaterThan(-1);
}, 500);

$timeout.flush();
});
});

it('should call the event and set larger second values correctly', function () {
inject(function ($compile, $rootScope, $timeout) {
var scope = $rootScope.$new();
var element = $compile('<timer countdown="10" interval="1000" autostart="false">{{mminutes}}:{{sseconds}}</timer>')(scope);
scope.$digest();

scope.$broadcast('timer-set-countdown-seconds', 135);

$timeout(function () {
scope.$digest();
expect(element.html().indexOf('02:15')).toBeGreaterThan(-1);
}, 500);

$timeout.flush();
});
});
});

0 comments on commit d34157e

Please sign in to comment.