/* License: MIT. * Copyright (C) 2013, 2014, Uri Shaked. */ /* global describe, inject, module, beforeEach, afterEach, it, expect, spyOn, jasmine */ 'use strict'; describe('module angularMoment', function () { 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'); moment = $injector.get('moment'); amMoment = $injector.get('amMoment'); amTimeAgoConfig = $injector.get('amTimeAgoConfig'); angularMomentConfig = $injector.get('angularMomentConfig'); originalTimeAgoConfig = angular.copy(amTimeAgoConfig); originalAngularMomentConfig = angular.copy(angularMomentConfig); // Ensure the locale of moment.js is set to en by default (moment.locale || moment.lang)('en'); // Add a sample timezone for tests moment.tz.add('Pacific/Tahiti|LMT TAHT|9W.g a0|01|-2joe1.I'); })); afterEach(function () { // Restore original configuration after each test angular.copy(originalTimeAgoConfig, amTimeAgoConfig); angular.copy(originalAngularMomentConfig, angularMomentConfig); jasmine.clock().uninstall(); }); describe('am-time-ago directive', function () { it('should change the text of the element to "a few seconds ago" when given unix timestamp', function () { $rootScope.testDate = new Date().getTime() / 1000; var element = angular.element(''); element = $compile(element)($rootScope); $rootScope.$digest(); expect(element.text()).toBe('a few seconds ago'); }); it('should change the text of the element to "a few seconds ago" when given current time', function () { $rootScope.testDate = new Date(); var element = angular.element(''); element = $compile(element)($rootScope); $rootScope.$digest(); expect(element.text()).toBe('a few seconds ago'); }); it('should change the text of the div to "3 minutes ago" when given a date 3 minutes ago', function () { $rootScope.testDate = new Date(new Date().getTime() - 3 * 60 * 1000); var element = angular.element('
'); element = $compile(element)($rootScope); $rootScope.$digest(); expect(element.text()).toBe('3 minutes ago'); }); it('should change the text of the div to "2 hours ago" when given a date 2 hours ago', function () { $rootScope.testDate = new Date(new Date().getTime() - 2 * 60 * 60 * 1000); var element = angular.element('
'); element = $compile(element)($rootScope); $rootScope.$digest(); expect(element.text()).toBe('2 hours ago'); }); it('should change the text of the div to "one year ago" when given a date one year ago', function () { var today = new Date(); $rootScope.testDate = new Date(today.getFullYear() - 1, today.getMonth(), today.getDate()); var element = angular.element('
'); element = $compile(element)($rootScope); $rootScope.$digest(); expect(element.text()).toBe('a year ago'); }); it('should parse correctly numeric dates as milliseconds since the epoch', function () { $rootScope.testDate = new Date().getTime(); var element = angular.element('
'); element = $compile(element)($rootScope); $rootScope.$digest(); expect(element.text()).toBe('a few seconds ago'); }); it('should update the value if date changes on scope', function () { var today = new Date(); $rootScope.testDate = new Date(today.getFullYear() - 1, today.getMonth(), today.getDate()).getTime(); var element = angular.element('
'); element = $compile(element)($rootScope); $rootScope.$digest(); expect(element.text()).toBe('a year ago'); $rootScope.testDate = new Date(); $rootScope.$digest(); expect(element.text()).toBe('a few seconds ago'); }); it('should update the span text as time passes', function (done) { $rootScope.testDate = new Date(new Date().getTime() - 44000); var element = angular.element('
'); element = $compile(element)($rootScope); $rootScope.$digest(); expect(element.text()).toBe('a few seconds ago'); var waitsInterval = setInterval(function () { // Wait until $rootScope.date is more than 45 seconds old if (new Date().getTime() - $rootScope.testDate.getTime() < 45000) { return; } clearInterval(waitsInterval); $rootScope.$digest(); expect(element.text()).toBe('a minute ago'); done(); }, 50); }); it('should schedule the update timer to one hour ahead for date in the far future (#73)', function () { $rootScope.testDate = new Date(new Date().getTime() + 86400000); jasmine.clock().install(); spyOn($window, 'setTimeout'); var element = angular.element('
'); element = $compile(element)($rootScope); $rootScope.$digest(); expect($window.setTimeout).toHaveBeenCalledWith(jasmine.any(Function), 3600000); }); describe('bindonce', function () { it('should change the text of the div to "3 minutes ago" when given a date 3 minutes ago with one time binding', function () { $rootScope.testDate = new Date(new Date().getTime() - 3 * 60 * 1000); var element = angular.element('
'); element = $compile(element)($rootScope); $rootScope.$digest(); expect(element.text()).toBe('3 minutes ago'); }); it('should parse correctly numeric dates as milliseconds since the epoch with one time binding', function () { $rootScope.testDate = new Date().getTime(); var element = angular.element('
'); element = $compile(element)($rootScope); $rootScope.$digest(); expect(element.text()).toBe('a few seconds ago'); }); it('should not update the value if date changes on scope when using one time binding', function () { var today = new Date(); $rootScope.testDate = new Date(today.getFullYear() - 1, today.getMonth(), today.getDate()).getTime(); var element = angular.element('
'); element = $compile(element)($rootScope); $rootScope.$digest(); expect(element.text()).toBe('a year ago'); $rootScope.testDate = new Date(); $rootScope.$digest(); expect(element.text()).toBe('a year ago'); }); it('should not update the span text as time passes when using one time binding', function (done) { $rootScope.testDate = new Date(new Date().getTime() - 44000); var element = angular.element('
'); element = $compile(element)($rootScope); $rootScope.$digest(); expect(element.text()).toBe('a few seconds ago'); var waitsInterval = setInterval(function () { // Wait until $rootScope.date is more than 45 seconds old if (new Date().getTime() - $rootScope.testDate.getTime() < 45000) { return; } clearInterval(waitsInterval); $rootScope.$digest(); expect(element.text()).toBe('a few seconds ago'); done(); }, 50); }); }); it('should handle undefined data', function () { $rootScope.testDate = null; var element = angular.element('
'); element = $compile(element)($rootScope); var digest = function () { $rootScope.$digest(); }; expect(digest).not.toThrow(); }); it('should remove the element text and cancel the timer when an empty string is given (#15)', function () { $rootScope.testDate = new Date().getTime(); var element = angular.element('
'); element = $compile(element)($rootScope); $rootScope.$digest(); expect(element.text()).toBe('a few seconds ago'); $rootScope.testDate = ''; spyOn($window, 'clearTimeout').and.callThrough(); $rootScope.$digest(); 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('
Initial text
'); element = $compile(element)($rootScope); $rootScope.$digest(); expect(element.text()).toBe('Initial text'); $rootScope.testDate = new Date().getTime(); $rootScope.$digest(); expect(element.text()).toBe('a few seconds ago'); }); it('should cancel the timer when the scope is destroyed', function () { var scope = $rootScope.$new(); $rootScope.testDate = new Date(); var element = angular.element(''); element = $compile(element)(scope); $rootScope.$digest(); spyOn($window, 'clearTimeout').and.callThrough(); scope.$destroy(); expect($window.clearTimeout).toHaveBeenCalled(); }); it('should generate a time string without suffix when configured to do so', function () { amTimeAgoConfig.withoutSuffix = true; $rootScope.testDate = new Date(); var element = angular.element(''); element = $compile(element)($rootScope); $rootScope.$digest(); expect(element.text()).toBe('a few seconds'); }); it('should generate update the text following a locale change via amMoment.changeLocale() method', function () { $rootScope.testDate = new Date(); var element = angular.element(''); element = $compile(element)($rootScope); $rootScope.$digest(); expect(element.text()).toBe('a few seconds ago'); amMoment.changeLocale('fr'); expect(element.text()).toBe('il y a quelques secondes'); }); it('should update the `datetime` attr if applied to a TIME element', function () { $rootScope.testDate = Date.UTC(2012, 8, 20, 15, 20, 12); var element = angular.element('