Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

Commit

Permalink
Revert to using window.setTimeout instead of $timeout (see explanatio…
Browse files Browse the repository at this point in the history
…n in #35), update angular-moment.min.js
  • Loading branch information
urish committed Mar 16, 2014
1 parent 82ebe79 commit 0d418a7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
6 changes: 3 additions & 3 deletions angular-moment.js
Expand Up @@ -30,7 +30,7 @@
})
.constant('moment', window.moment)
.constant('amTimeAgoConfig', { withoutSuffix: false})
.directive('amTimeAgo', ['$timeout', 'moment', 'amTimeAgoConfig', function ($timeout, moment, amTimeAgoConfig) {
.directive('amTimeAgo', ['$window', 'moment', 'amTimeAgoConfig', function ($window, moment, amTimeAgoConfig) {

return function (scope, element, attr) {
var activeTimeout = null;
Expand All @@ -40,7 +40,7 @@

function cancelTimer() {
if (activeTimeout) {
$timeout.cancel(activeTimeout);
$window.clearTimeout(activeTimeout);
activeTimeout = null;
}
}
Expand All @@ -57,7 +57,7 @@
secondsUntilUpdate = 300;
}

activeTimeout = $timeout(function () {
activeTimeout = $window.setTimeout(function () {
updateTime(momentInstance);
}, secondsUntilUpdate * 1000);
}
Expand Down
2 changes: 1 addition & 1 deletion angular-moment.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 12 additions & 10 deletions tests.js
Expand Up @@ -2,21 +2,21 @@
* Copyright (C) 2013, 2014, Uri Shaked.
*/

/* global describe, inject, module, beforeEach, afterEach, it, expect */
/* global describe, inject, module, beforeEach, afterEach, it, expect, spyOn */

'use strict';

describe('module angularMoment', function () {
var $rootScope, $compile, $filter, $timeout, moment, amTimeAgoConfig, originalTimeAgoConfig, angularMomentConfig,
var $rootScope, $compile, $window, $filter, moment, amTimeAgoConfig, originalTimeAgoConfig, angularMomentConfig,
originalAngularMomentConfig, amMoment;

beforeEach(module('angularMoment'));

beforeEach(inject(function ($injector) {
$rootScope = $injector.get('$rootScope');
$compile = $injector.get('$compile');
$window = $injector.get('$window');
$filter = $injector.get('$filter');
$timeout = $injector.get('$timeout');
moment = $injector.get('moment');
amMoment = $injector.get('amMoment');
amTimeAgoConfig = $injector.get('amTimeAgoConfig');
Expand Down Expand Up @@ -99,7 +99,7 @@ describe('module angularMoment', function () {
$rootScope.testDate = new Date(new Date().getTime() - 44000);
var element = angular.element('<div am-time-ago="testDate"></div>');
element = $compile(element)($rootScope);
$timeout.flush();
$rootScope.$digest();
expect(element.text()).toBe('a few seconds ago');

var waitsInterval = setInterval(function () {
Expand All @@ -109,7 +109,7 @@ describe('module angularMoment', function () {
}

clearInterval(waitsInterval);
$timeout.flush();
$rootScope.$digest();
expect(element.text()).toBe('a minute ago');
done();
}, 50);
Expand All @@ -129,19 +129,20 @@ describe('module angularMoment', function () {
$rootScope.testDate = new Date().getTime();
var element = angular.element('<div am-time-ago="testDate"></div>');
element = $compile(element)($rootScope);
$timeout.flush();
$rootScope.$digest();
expect(element.text()).toBe('a few seconds ago');
$rootScope.testDate = '';
spyOn($window, 'clearTimeout').and.callThrough();
$rootScope.$digest();
$timeout.verifyNoPendingTasks();
expect($window.clearTimeout).toHaveBeenCalled();
expect(element.text()).toBe('');
});

it('should not change the contents of the element until a date is given', function () {
$rootScope.testDate = null;
var element = angular.element('<div am-time-ago="testDate">Initial text</div>');
element = $compile(element)($rootScope);
$timeout.flush();
$rootScope.$digest();
expect(element.text()).toBe('Initial text');
$rootScope.testDate = new Date().getTime();
$rootScope.$digest();
Expand All @@ -153,9 +154,10 @@ describe('module angularMoment', function () {
$rootScope.testDate = new Date();
var element = angular.element('<span am-time-ago="testDate"></span>');
element = $compile(element)(scope);
$timeout.flush();
$rootScope.$digest();
spyOn($window, 'clearTimeout').and.callThrough();
scope.$destroy();
$timeout.verifyNoPendingTasks();
expect($window.clearTimeout).toHaveBeenCalled();
});

it('should generate a time string without suffix when configured to do so', function () {
Expand Down

0 comments on commit 0d418a7

Please sign in to comment.