-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.test.js
More file actions
78 lines (58 loc) · 2.32 KB
/
Copy pathrun.test.js
File metadata and controls
78 lines (58 loc) · 2.32 KB
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
const { run } = require("./run.js");
const chronoNode = require("chrono-node");
jest.mock("chrono-node");
describe("run", () => {
beforeEach(() => {
jest.clearAllMocks();
});
it("should throw if parsedDate is null", () => {
chronoNode.parse.mockReturnValue(null);
expect(() => run(null, "invalid")).toThrow("Invalid date expression.");
});
it("should throw if parsedDate.length is 0", () => {
chronoNode.parse.mockReturnValue([]);
expect(() => run(null, "invalid")).toThrow("Invalid date expression.");
});
it("should throw error if parse throws an error", () => {
chronoNode.parse.mockImplementation(() => {
throw new Error("Parse error");
});
expect(() => run(null, "invalid")).toThrow(
"Error parsing date: Parse error"
);
});
it("should return correct date for tomorrow", () => {
const realChronoNode = jest.requireActual("chrono-node");
chronoNode.parse.mockImplementation(realChronoNode.parse);
const result = run(null, "tomorrow");
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
const expected = tomorrow.toISOString().split("T")[0];
expect(result).toContain(expected);
});
it("should return correct date for next year", () => {
const realChronoNode = jest.requireActual("chrono-node");
chronoNode.parse.mockImplementation(realChronoNode.parse);
const result = run(null, "next year");
const nextYear = new Date();
nextYear.setFullYear(nextYear.getFullYear() + 1);
const expected = nextYear.toISOString().split("T")[0];
expect(result).toContain(expected);
});
it("should return correct date for last year", () => {
const realChronoNode = jest.requireActual("chrono-node");
chronoNode.parse.mockImplementation(realChronoNode.parse);
const result = run(null, "last year");
const lastYear = new Date();
lastYear.setFullYear(lastYear.getFullYear() - 1);
const expected = lastYear.toISOString().split("T")[0];
expect(result).toContain(expected);
});
it("should return correct date for a specific date", () => {
const realChronoNode = jest.requireActual("chrono-node");
chronoNode.parse.mockImplementation(realChronoNode.parse);
const result = run(null, "2025-05-01");
const expected = "2025-05-01";
expect(result).toContain(expected);
});
});