forked from date-fns/date-fns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
200 lines (169 loc) · 5.83 KB
/
test.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { UTCDate } from "@date-fns/utc";
import sinon from "sinon";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import type { FormatDistanceFn } from "../locale/types.js";
import { formatDistanceToNow } from "./index.js";
describe("formatDistanceToNow", () => {
let clock: sinon.SinonFakeTimers;
beforeEach(() => {
clock = sinon.useFakeTimers(new Date(1986, 3, 4, 10, 32, 0).getTime());
});
afterEach(() => {
clock.restore();
});
describe("seconds", () => {
describe("when the includeSeconds option is true", () => {
it("less than 5 seconds", () => {
const result = formatDistanceToNow(new Date(1986, 3, 4, 10, 31, 58), {
includeSeconds: true,
});
expect(result).toBe("less than 5 seconds");
});
it("less than 10 seconds", () => {
const result = formatDistanceToNow(new Date(1986, 3, 4, 10, 31, 52), {
includeSeconds: true,
});
expect(result).toBe("less than 10 seconds");
});
it("less than 20 seconds", () => {
const result = formatDistanceToNow(new Date(1986, 3, 4, 10, 31, 45), {
includeSeconds: true,
});
expect(result).toBe("less than 20 seconds");
});
it("half a minute", () => {
const result = formatDistanceToNow(new Date(1986, 3, 4, 10, 31, 35), {
includeSeconds: true,
});
expect(result).toBe("half a minute");
});
it("less than a minute", () => {
const result = formatDistanceToNow(new Date(1986, 3, 4, 10, 31, 15), {
includeSeconds: true,
});
expect(result).toBe("less than a minute");
});
it("1 minute", () => {
const result = formatDistanceToNow(new Date(1986, 3, 4, 10, 31, 0), {
includeSeconds: true,
});
expect(result).toBe("1 minute");
});
});
});
describe("minutes", () => {
it("less than a minute", () => {
const result = formatDistanceToNow(new Date(1986, 3, 4, 10, 31, 40));
expect(result).toBe("less than a minute");
});
it("1 minute", () => {
const result = formatDistanceToNow(new Date(1986, 3, 4, 10, 31, 10));
expect(result).toBe("1 minute");
});
it("n minutes", () => {
const result = formatDistanceToNow(new Date(1986, 3, 4, 10, 29, 10));
expect(result).toBe("3 minutes");
});
});
describe("hours", () => {
it("about 1 hour", () => {
const result = formatDistanceToNow(new Date(1986, 3, 4, 9, 32, 0));
expect(result).toBe("about 1 hour");
});
it("about n hours", () => {
const result = formatDistanceToNow(new Date(1986, 3, 4, 7, 32, 0));
expect(result).toBe("about 3 hours");
});
});
describe("days", () => {
it("1 day", () => {
const result = formatDistanceToNow(new Date(1986, 3, 3, 10, 32, 0));
expect(result).toBe("1 day");
});
it("n days", () => {
const result = formatDistanceToNow(new Date(1986, 3, 1, 10, 32, 0));
expect(result).toBe("3 days");
});
});
describe("months", () => {
it("about 1 month", () => {
const result = formatDistanceToNow(new Date(1986, 2, 4, 10, 32, 0));
expect(result).toBe("about 1 month");
});
it("n months", () => {
const result = formatDistanceToNow(new Date(1986, 0, 4, 10, 32, 0));
expect(result).toBe("3 months");
});
});
describe("years", () => {
it("about 1 year", () => {
const result = formatDistanceToNow(new Date(1985, 3, 4, 10, 32, 0));
expect(result).toBe("about 1 year");
});
it("over 1 year", () => {
const result = formatDistanceToNow(new Date(1984, 10, 4, 10, 32, 0));
expect(result).toBe("over 1 year");
});
it("almost n years", () => {
const result = formatDistanceToNow(new Date(1983, 4, 4, 10, 32, 0));
expect(result).toBe("almost 3 years");
});
it("about n years", () => {
const result = formatDistanceToNow(new Date(1983, 3, 4, 10, 32, 0));
expect(result).toBe("about 3 years");
});
it("over n years", () => {
const result = formatDistanceToNow(new Date(1982, 10, 4, 10, 32, 0));
expect(result).toBe("over 3 years");
});
});
it("accepts a timestamp", () => {
const result = formatDistanceToNow(
new Date(1986, 3, 4, 10, 31, 40).getTime(),
);
expect(result).toBe("less than a minute");
});
describe("when the addSuffix option is true", () => {
it("adds a past suffix", () => {
const result = formatDistanceToNow(new Date(1986, 3, 4, 10, 31, 35), {
includeSeconds: true,
addSuffix: true,
});
expect(result).toBe("half a minute ago");
});
it("adds a future suffix", () => {
const result = formatDistanceToNow(new Date(1986, 3, 4, 11, 32, 0), {
addSuffix: true,
});
expect(result).toBe("in about 1 hour");
});
});
describe("custom locale", () => {
it("can be passed to the function", () => {
const localizeDistance: FormatDistanceFn = (token, count, options) => {
expect(token).toBe("aboutXHours");
expect(count).toBe(1);
expect(options!.addSuffix).toBe(true);
expect(options!.comparison!).toBeGreaterThan(0);
return "It works!";
};
const customLocale = {
formatDistance: localizeDistance,
};
const result = formatDistanceToNow(new Date(1986, 3, 4, 11, 32, 0), {
addSuffix: true,
locale: customLocale,
});
expect(result).toBe("It works!");
});
});
it("throws RangeError if the passed date is `Invalid Date`", function () {
expect(formatDistanceToNow.bind(null, new Date(NaN))).toThrow(RangeError);
});
it("respects date extensions", () => {
const result = formatDistanceToNow(
new UTCDate(+new Date(1986, 3, 4, 9, 32, 0)),
);
expect(result).toBe("about 1 hour");
});
});