UNPKG

6.46 kBPlain TextView Raw
1import { ParsingComponents, ParsingResult, ReferenceWithTimezone } from "../src/results";
2
3test("Test - Create & manipulate parsing components", () => {
4 const reference = new ReferenceWithTimezone(new Date());
5 const components = new ParsingComponents(reference, { year: 2014, month: 11, day: 24 });
6
7 expect(components.get("year")).toBe(2014);
8 expect(components.get("month")).toBe(11);
9 expect(components.get("day")).toBe(24);
10 expect(components.date()).toBeDefined();
11 expect(components.tags().size).toBe(0);
12
13 // null
14 expect(components.get("weekday")).toBeNull();
15 expect(components.isCertain("weekday")).toBe(false);
16
17 // "imply"
18 components.imply("weekday", 1);
19 expect(components.get("weekday")).toBe(1);
20 expect(components.isCertain("weekday")).toBe(false);
21
22 // "assign" overrides "imply"
23 components.assign("weekday", 2);
24 expect(components.get("weekday")).toBe(2);
25 expect(components.isCertain("weekday")).toBe(true);
26
27 // "imply" doesn't override "assign"
28 components.imply("year", 2013);
29 expect(components.get("year")).toBe(2014);
30
31 // "assign" overrides "assign"
32 components.assign("year", 2013);
33 expect(components.get("year")).toBe(2013);
34
35 components.addTag("custom/testing_component_tag");
36 expect(components.tags().size).toBe(1);
37 expect(components.tags()).toContain("custom/testing_component_tag");
38 expect(components.toString()).toContain("custom/testing_component_tag");
39});
40
41test("Test - Create & manipulate parsing results", () => {
42 const reference = new ReferenceWithTimezone(new Date());
43 const text = "1 - 2 hour later";
44
45 const startComponents = ParsingComponents.createRelativeFromReference(reference, { "hour": 1 }).addTag(
46 "custom/testing_start_component_tag"
47 );
48
49 const endComponents = ParsingComponents.createRelativeFromReference(reference, { "hour": 2 }).addTag(
50 "custom/testing_end_component_tag"
51 );
52
53 const result = new ParsingResult(reference, 0, text, startComponents, endComponents);
54
55 // The result's date() should be the same as the start components' date()
56 expect(result.date()).toStrictEqual(startComponents.date());
57
58 // The result's tags should include both the start and end components' tags
59 expect(result.tags()).toContain("custom/testing_start_component_tag");
60 expect(result.tags()).toContain("custom/testing_end_component_tag");
61
62 // The result's toString() should include the text and tags
63 expect(result.toString()).toContain(text);
64 expect(result.toString()).toContain("custom/testing_start_component_tag");
65 expect(result.toString()).toContain("custom/testing_end_component_tag");
66});
67
68test("Test - Calendar checking with implied components", () => {
69 const reference = new ReferenceWithTimezone(new Date());
70
71 {
72 const components = new ParsingComponents(reference, {
73 "day": 13,
74 "month": 3,
75 "year": 2021,
76 "hour": 14,
77 "minute": 22,
78 "second": 14,
79 "millisecond": 0,
80 });
81 components.imply("timezoneOffset", -300);
82
83 expect(components.isValidDate()).toBe(true);
84 }
85});
86
87test("Test - Calendar Checking", () => {
88 const reference = new ReferenceWithTimezone(new Date());
89
90 {
91 const components = new ParsingComponents(reference, { year: 2014, month: 11, day: 24 });
92 expect(components.isValidDate()).toBe(true);
93 }
94
95 {
96 const components = new ParsingComponents(reference, { year: 2014, month: 11, day: 24, hour: 12 });
97 expect(components.isValidDate()).toBe(true);
98 }
99
100 {
101 const components = new ParsingComponents(reference, { year: 2014, month: 11, day: 24, hour: 12, minute: 30 });
102 expect(components.isValidDate()).toBe(true);
103 }
104
105 {
106 const components = new ParsingComponents(reference, {
107 year: 2014,
108 month: 11,
109 day: 24,
110 hour: 12,
111 minute: 30,
112 second: 30,
113 });
114 expect(components.isValidDate()).toBe(true);
115 }
116
117 {
118 const components = new ParsingComponents(reference, { year: 2014, month: 13, day: 24 });
119 expect(components.isValidDate()).toBe(false);
120 }
121
122 {
123 const components = new ParsingComponents(reference, { year: 2014, month: 11, day: 32 });
124 expect(components.isValidDate()).toBe(false);
125 }
126
127 {
128 const components = new ParsingComponents(reference, { year: 2014, month: 11, day: 24, hour: 24 });
129 expect(components.isValidDate()).toBe(false);
130 }
131
132 {
133 const components = new ParsingComponents(reference, { year: 2014, month: 11, day: 24, hour: 12, minute: 60 });
134 expect(components.isValidDate()).toBe(false);
135 }
136
137 {
138 const components = new ParsingComponents(reference, {
139 year: 2014,
140 month: 11,
141 day: 24,
142 hour: 12,
143 minute: 30,
144 second: 60,
145 });
146 expect(components.isValidDate()).toBe(false);
147 }
148});
149
150test("Test - Checking non-existing date during DST skip", () => {
151 // Only CET (or CEST) timezones where the DST starts on "Sunday, March 27, 2022" at "02:00 (2 am) local time"
152 const dateDstPre = new Date(2022, 3 - 1, 27, 2);
153 const dateDstPost = new Date(2022, 3 - 1, 27, 3);
154 if (dateDstPre.getTime() == dateDstPost.getTime()) {
155 const reference = new ReferenceWithTimezone(new Date());
156
157 // On "Sunday, March 27, 2022" at "02:00 local time", the clock is moved forward to "03:00 local time".
158 // Thus, the time between "02:00 and 02:59:59" does not exist.
159 expect(
160 new ParsingComponents(reference, { year: 2022, month: 3, day: 27, hour: 2, minute: 0 }).isValidDate()
161 ).toBe(false);
162 expect(
163 new ParsingComponents(reference, { year: 2022, month: 3, day: 27, hour: 2, minute: 1 }).isValidDate()
164 ).toBe(false);
165 expect(
166 new ParsingComponents(reference, { year: 2022, month: 3, day: 27, hour: 2, minute: 59 }).isValidDate()
167 ).toBe(false);
168
169 // Otherwise, it
170 expect(
171 new ParsingComponents(reference, { year: 2022, month: 3, day: 27, hour: 1, minute: 59 }).isValidDate()
172 ).toBe(true);
173 expect(
174 new ParsingComponents(reference, { year: 2022, month: 3, day: 27, hour: 3, minute: 0 }).isValidDate()
175 ).toBe(true);
176 }
177});