-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreactive-fromnow-tests.js
85 lines (74 loc) · 2.33 KB
/
reactive-fromnow-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
Tinytest.add('reactive-fromnow - expiration MS', function (test) {
var t = function (durationArg, expected) {
test.equal(
ReactiveFromNowTest.expirationMsFromDuration(
moment.duration(durationArg)),
expected
);
};
t({ seconds: -1 }, 1000);
t({ seconds: 1 }, 1000);
t({ seconds: 59 }, 1000);
t({ minutes: 1 }, 60*1000);
t({ minutes: 5 }, 60*1000);
t({ minutes: 5, seconds: 3 }, 57*1000);
t({ minutes: 59, seconds: 58 }, 2*1000);
t({ minutes: 59, seconds: 59 }, 1000);
t({ hours: 1 }, 60*60*1000);
t({ hours: 2, minutes: 9, seconds: 10 }, 50*60*1000 + 50*1000);
t({ days: 1 }, 60*60*1000);
t({ days: 1, hours: 3 }, 60*60*1000);
});
Tinytest.add('reactive-fromnow - results', function (test) {
var t = function (howLongAgoMs, expected) {
var now = new Date;
var then = new Date(+now - howLongAgoMs);
var actual = ReactiveFromNow(then, {
maxRelativeMs: 7*24*60*60*1000,
fromWhen: now,
noReactivity: true
});
if (expected instanceof RegExp) {
test.matches(actual, expected);
} else {
test.equal(actual, expected);
}
};
t(-1, 'just now');
t(0, 'just now');
// These rules are based on the relativeTimeThresholds in moment.js.
t(1000, 'a few seconds ago');
t(30*1000, 'a few seconds ago');
t(59*1000, 'a minute ago');
t(60*1000, 'a minute ago');
t(61*1000, 'a minute ago');
t(89*1000, 'a minute ago');
t(91*1000, '2 minutes ago');
t(30*60*1000, '30 minutes ago');
t(44*60*1000, '44 minutes ago');
t(50*60*1000, 'an hour ago');
t(65*60*1000, 'an hour ago');
t(3*60*60*1000, '3 hours ago');
t(21*60*60*1000, '21 hours ago');
t(22*60*60*1000, 'a day ago');
t(26*60*60*1000, 'a day ago');
t(2*24*60*60*1000, '2 days ago');
t(7*24*60*60*1000, '7 days ago');
var absRegexp = /^\d{4}-[A-Z][a-z][a-z]-\d{2} \d{1,2}:\d{2} [ap]m$/;
t(7*24*60*60*1000 + 1, absRegexp);
t(10*24*60*60*1000, absRegexp);
});
Tinytest.addAsync('reactive-fromnow - reactive', function (test, onComplete) {
var then = new Date(+new Date() - 30*1000);
var autorunCount = 0;
var handle = Tracker.autorun(function () {
++autorunCount;
test.equal(ReactiveFromNow(then), "a few seconds ago");
});
test.equal(autorunCount, 1);
setTimeout(function () {
test.equal(autorunCount, 3);
handle.stop();
onComplete();
}, 2100);
});