1 | import { createParsingComponentsAtWeekday, getDaysToWeekday } from "../src/common/calculation/weekdays";
|
2 | import { Weekday } from "../src";
|
3 | import { ReferenceWithTimezone } from "../src/results";
|
4 |
|
5 | test("Test - This Weekday Calculation", () => {
|
6 | {
|
7 | const reference = new ReferenceWithTimezone(new Date("Sat, 20 Aug 2022 12:00:00"));
|
8 | const output = createParsingComponentsAtWeekday(reference, Weekday.MONDAY, "this");
|
9 | expect(output.date()).toStrictEqual(new Date("Mon, Aug 22 2022 12:00:00"));
|
10 | }
|
11 | {
|
12 | const reference = new ReferenceWithTimezone(new Date("Sun, 21 Aug 2022 12:00:00"));
|
13 | const output = createParsingComponentsAtWeekday(reference, Weekday.FRIDAY, "this");
|
14 | expect(output.date()).toStrictEqual(new Date("Fri, Aug 26 2022 12:00:00"));
|
15 | }
|
16 | {
|
17 | const reference = new ReferenceWithTimezone(new Date("Tue, Aug 2 2022 12:00:00"));
|
18 | const output = createParsingComponentsAtWeekday(reference, Weekday.SUNDAY, "this");
|
19 | expect(output.date()).toStrictEqual(new Date("Sun, Aug 7 2022 12:00:00"));
|
20 | }
|
21 | });
|
22 |
|
23 | test("Test - Last Weekday Calculation", () => {
|
24 | {
|
25 | const reference = new ReferenceWithTimezone(new Date("Sat, 20 Aug 2022 12:00:00"));
|
26 | const output = createParsingComponentsAtWeekday(reference, Weekday.FRIDAY, "last");
|
27 | expect(output.date()).toStrictEqual(new Date("Fri, Aug 19 2022 12:00:00"));
|
28 | }
|
29 | {
|
30 | const reference = new ReferenceWithTimezone(new Date("Sat, 20 Aug 2022 12:00:00"));
|
31 | const output = createParsingComponentsAtWeekday(reference, Weekday.MONDAY, "last");
|
32 | expect(output.date()).toStrictEqual(new Date("Mon, Aug 15 2022 12:00:00"));
|
33 | }
|
34 | {
|
35 | const reference = new ReferenceWithTimezone(new Date("Sat, 20 Aug 2022 12:00:00"));
|
36 | const output = createParsingComponentsAtWeekday(reference, Weekday.SUNDAY, "last");
|
37 | expect(output.date()).toStrictEqual(new Date("Sun, Aug 14 2022 12:00:00"));
|
38 | }
|
39 | {
|
40 | const reference = new ReferenceWithTimezone(new Date("Sat, 20 Aug 2022 12:00:00"));
|
41 | const output = createParsingComponentsAtWeekday(reference, Weekday.SATURDAY, "last");
|
42 | expect(output.date()).toStrictEqual(new Date("Sat, Aug 13 2022 12:00:00"));
|
43 | }
|
44 | });
|
45 |
|
46 | test("Test - Next Weekday Calculation", () => {
|
47 | {
|
48 | const reference = new ReferenceWithTimezone(new Date("Sun, Aug 21 2022 12:00:00"));
|
49 | const output = createParsingComponentsAtWeekday(reference, Weekday.MONDAY, "next");
|
50 | expect(output.date()).toStrictEqual(new Date("Mon, Aug 22 2022 12:00:00"));
|
51 | }
|
52 | {
|
53 | const reference = new ReferenceWithTimezone(new Date("Sun, Aug 21 2022 12:00:00"));
|
54 | const output = createParsingComponentsAtWeekday(reference, Weekday.SATURDAY, "next");
|
55 | expect(output.date()).toStrictEqual(new Date("Sat, Aug 27 2022 12:00:00"));
|
56 | }
|
57 | {
|
58 | const reference = new ReferenceWithTimezone(new Date("Sun, Aug 21 2022 12:00:00"));
|
59 | const output = createParsingComponentsAtWeekday(reference, Weekday.SUNDAY, "next");
|
60 | expect(output.date()).toStrictEqual(new Date("Sun, Aug 28 2022 12:00:00"));
|
61 | }
|
62 | {
|
63 | const reference = new ReferenceWithTimezone(new Date("Sat, Aug 20 2022 12:00:00"));
|
64 | const output = createParsingComponentsAtWeekday(reference, Weekday.FRIDAY, "next");
|
65 | expect(output.date()).toStrictEqual(new Date("Fri, Aug 26 2022 12:00:00"));
|
66 | }
|
67 | {
|
68 | const reference = new ReferenceWithTimezone(new Date("Sat, Aug 20 2022 12:00:00"));
|
69 | const output = createParsingComponentsAtWeekday(reference, Weekday.SATURDAY, "next");
|
70 | expect(output.date()).toStrictEqual(new Date("Sat, Aug 27 2022 12:00:00"));
|
71 | }
|
72 | {
|
73 | const reference = new ReferenceWithTimezone(new Date("Sat, Aug 20 2022 12:00:00"));
|
74 | const output = createParsingComponentsAtWeekday(reference, Weekday.SUNDAY, "next");
|
75 | expect(output.date()).toStrictEqual(new Date("Sun, Aug 28 2022 12:00:00"));
|
76 | }
|
77 | {
|
78 | const reference = new ReferenceWithTimezone(new Date("Tue, Aug 2 2022 12:00:00"));
|
79 | const output = createParsingComponentsAtWeekday(reference, Weekday.MONDAY, "next");
|
80 | expect(output.date()).toStrictEqual(new Date("Mon, Aug 8 2022 12:00:00"));
|
81 | }
|
82 | {
|
83 | const reference = new ReferenceWithTimezone(new Date("Tue, Aug 2 2022 12:00:00"));
|
84 | const output = createParsingComponentsAtWeekday(reference, Weekday.FRIDAY, "next");
|
85 | expect(output.date()).toStrictEqual(new Date("Fri, Aug 12 2022 12:00:00"));
|
86 | }
|
87 |
|
88 | {
|
89 | const reference = new ReferenceWithTimezone(new Date("Tue, Aug 2 2022 12:00:00"));
|
90 | const output = createParsingComponentsAtWeekday(reference, Weekday.SUNDAY, "next");
|
91 | expect(output.date()).toStrictEqual(new Date("Sun, Aug 14 2022 12:00:00"));
|
92 | }
|
93 | });
|
94 |
|
95 | test("Test - Closest Weekday Calculation", () => {
|
96 | {
|
97 | const refDate = new Date("Sat, 20 Aug 2022 12:00:00");
|
98 | expect(getDaysToWeekday(refDate, Weekday.MONDAY)).toBe(2);
|
99 | }
|
100 | {
|
101 | const refDate = new Date("Sat, 20 Aug 2022 12:00:00");
|
102 | expect(getDaysToWeekday(refDate, Weekday.TUESDAY)).toBe(3);
|
103 | }
|
104 | {
|
105 | const refDate = new Date("Sat, 20 Aug 2022 12:00:00");
|
106 | expect(getDaysToWeekday(refDate, Weekday.FRIDAY)).toBe(-1);
|
107 | }
|
108 | {
|
109 | const refDate = new Date("Sat, 20 Aug 2022 12:00:00");
|
110 | expect(getDaysToWeekday(refDate, Weekday.THURSDAY)).toBe(-2);
|
111 | }
|
112 | {
|
113 | const refDate = new Date("Sat, 20 Aug 2022 12:00:00");
|
114 | expect(getDaysToWeekday(refDate, Weekday.WEDNESDAY)).toBe(-3);
|
115 | }
|
116 | });
|