forked from urish/ngx-moment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime-ago.pipe.spec.ts
92 lines (77 loc) · 3.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
import 'es6-shim';
import 'reflect-metadata';
import { NgZone } from '@angular/core';
import { TimeAgoPipe } from './time-ago.pipe';
import * as moment from 'moment';
declare var global: any;
// under systemjs, moment is actually exported as the default export, so we account for that
const momentConstructor: (value?: any) => moment.Moment = (<any>moment).default || moment;
class NgZoneMock {
runOutsideAngular(fn: Function) {
return fn();
}
run(fn: Function) {
return fn();
}
};
const _Date = Date;
function fakeDate(defaultDate: string | number) {
global.Date = (arg: any) => new _Date(typeof arg !== 'undefined' ? arg : defaultDate);
global.Date.UTC = _Date.UTC;
}
describe('TimeAgoPipe', () => {
describe('#transform', () => {
beforeEach(() => jest.useFakeTimers());
afterEach(() => {
global.Date = _Date;
jest.clearAllTimers();
jest.useRealTimers();
});
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 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.runTimersToTime(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 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');
let date = new Date();
expect(pipe.transform(date)).toBe('a few seconds ago');
date.setFullYear(2000);
expect(pipe.transform(date)).toBe('16 years ago');
let 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.runTimersToTime(60000);
expect(changeDetectorMock.markForCheck).not.toHaveBeenCalled();
});
});
});