forked from urish/ngx-moment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time-ago.pipe.spec.ts
136 lines (117 loc) · 5.41 KB
/
time-ago.pipe.spec.ts
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { NgZone } from '@angular/core';
import { TimeAgoPipe } from './time-ago.pipe';
import * as moment from 'moment';
import 'moment/min/locales';
declare var global: any;
jest.useFakeTimers();
class NgZoneMock {
runOutsideAngular(fn: Function) {
return fn();
}
run(fn: Function) {
return fn();
}
}
const _Date = Date;
function fakeDate(defaultDate: string | number) {
global.Date = function (arg: any) {
return new _Date(typeof arg !== 'undefined' ? arg : defaultDate);
};
global.Date.UTC = _Date.UTC;
}
describe('TimeAgoPipe', () => {
describe('#transform', () => {
beforeEach(() => {
moment.locale('en-gb');
});
afterEach(() => {
global.Date = _Date;
jest.clearAllTimers();
});
it('should transform the current date to "a few seconds ago"', () => {
const pipe = new TimeAgoPipe(null, new NgZoneMock() as NgZone);
expect(pipe.transform(new Date())).toBe('a few seconds ago');
});
it('should support string dates', () => {
const pipe = new TimeAgoPipe(null, new NgZoneMock() as NgZone);
const dateStr = new Date().toISOString();
expect(pipe.transform(dateStr)).toBe('a few seconds ago');
});
it('should omit the suffix if second parameter is truthy', () => {
const pipe = new TimeAgoPipe(null, new NgZoneMock() as NgZone);
expect(pipe.transform(new Date(new Date().getTime() + 60000), true)).toBe('a minute');
});
it('should automatically update the text as time passes', () => {
const changeDetectorMock = { markForCheck: jest.fn() };
const pipe = new TimeAgoPipe(changeDetectorMock as any, new NgZoneMock() as NgZone);
expect(pipe.transform(new Date())).toBe('a few seconds ago');
expect(changeDetectorMock.markForCheck).not.toHaveBeenCalled();
jest.advanceTimersByTime(60000);
expect(changeDetectorMock.markForCheck).toHaveBeenCalled();
});
it('should update the text with a new date instance different from the previous one', () => {
const changeDetectorMock = { markForCheck: jest.fn() };
const pipe = new TimeAgoPipe(changeDetectorMock as any, new NgZoneMock() as NgZone);
fakeDate('2016-05-01');
expect(pipe.transform(new Date())).toBe('a few seconds ago');
expect(pipe.transform(new Date(0))).toBe('46 years ago');
expect(pipe.transform(moment())).toBe('a few seconds ago');
expect(pipe.transform(moment(0))).toBe('46 years ago');
});
it('should update the text when moment locale changes', () => {
const changeDetectorMock = { markForCheck: jest.fn() };
const pipe = new TimeAgoPipe(changeDetectorMock as any, new NgZoneMock() as NgZone);
fakeDate('2016-05-01');
expect(pipe.transform(moment(0))).toBe('46 years ago');
expect(pipe.transform(moment(0).locale('pl'))).toBe('46 lat temu');
});
it('should reset language when localized moment changes to Date', () => {
const changeDetectorMock = { markForCheck: jest.fn() };
const pipe = new TimeAgoPipe(changeDetectorMock as any, new NgZoneMock() as NgZone);
fakeDate('2016-05-01');
expect(pipe.transform(moment(0).locale('pl'))).toBe('46 lat temu');
expect(pipe.transform(new Date(0))).toBe('46 years ago');
});
it('should update the text when using Date Objects and locale changes', () => {
const changeDetectorMock = { markForCheck: jest.fn() };
const pipe = new TimeAgoPipe(changeDetectorMock as any, new NgZoneMock() as NgZone);
fakeDate('2016-05-01');
expect(pipe.transform(new Date(0))).toBe('46 years ago');
moment.locale('de');
expect(pipe.transform(new Date(0))).toBe('vor 46 Jahren');
});
it('should update the text when the date instance time is updated', () => {
const changeDetectorMock = { markForCheck: jest.fn() };
const pipe = new TimeAgoPipe(changeDetectorMock as any, new NgZoneMock() as NgZone);
fakeDate('2016-05-01');
const date = new Date();
expect(pipe.transform(date)).toBe('a few seconds ago');
date.setFullYear(2000);
expect(pipe.transform(date)).toBe('16 years ago');
const dateAsMoment = moment();
expect(pipe.transform(dateAsMoment)).toBe('a few seconds ago');
dateAsMoment.year(2000);
expect(pipe.transform(dateAsMoment)).toBe('16 years ago');
});
it('should remove all timers when destroyed', () => {
const changeDetectorMock = { markForCheck: jest.fn() };
const pipe = new TimeAgoPipe(changeDetectorMock as any, new NgZoneMock() as NgZone);
expect(pipe.transform(new Date())).toBe('a few seconds ago');
pipe.ngOnDestroy();
jest.advanceTimersByTime(60000);
expect(changeDetectorMock.markForCheck).not.toHaveBeenCalled();
});
it('should transform a date to a different format when a format function is provided', () => {
const changeDetectorMock = { markForCheck: jest.fn() };
const formatFnMock = (m: moment.Moment) => {
const seconds = m.diff(moment()) / 1000;
return seconds >= 3600 ? '' : `${Math.floor(seconds / 60)} min ago`;
};
const pipe = new TimeAgoPipe(changeDetectorMock as any, new NgZoneMock() as NgZone);
fakeDate('2016-05-01');
expect(pipe.transform(moment().add(20, 'm'), false, formatFnMock)).toBe('20 min ago');
expect(pipe.transform(moment().add(1, 'h'), false, formatFnMock)).toBe('');
expect(pipe.transform(moment().add(3, 'h'), false, formatFnMock)).toBe('');
});
});
});