forked from urish/angular-moment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.js
90 lines (76 loc) · 3.3 KB
/
tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* License: MIT.
* Copyright (C) 2013, Uri Shaked.
*/
/* global describe, inject, module, beforeEach, it, expect, waitsFor, runs */
'use strict';
describe('Directive: am-time-ago', function () {
var $rootScope, $compile, $timeout;
beforeEach(module('angularMoment'));
beforeEach(inject(function ($injector) {
$rootScope = $injector.get('$rootScope');
$compile = $injector.get('$compile');
$timeout = $injector.get('$timeout');
}));
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('<span am-time-ago="testDate"></span>');
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('<div am-time-ago="testDate"></div>');
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('<div am-time-ago="testDate"></div>');
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('<div am-time-ago="testDate"></div>');
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('<div am-time-ago="testDate"></div>');
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('<div am-time-ago="testDate"></div>');
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 () {
$rootScope.testDate = new Date(new Date().getTime() - 44000);
var element = angular.element('<div am-time-ago="testDate"></div>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('a few seconds ago');
waitsFor(function () {
return (new Date().getTime() - $rootScope.testDate.getTime()) > 45000;
}, '$rootScope.date is more than 45 seconds old', 1500);
runs(function () {
$timeout.flush();
$rootScope.$digest();
expect(element.text()).toBe('a minute ago');
});
});
});