/[sudobot]/trunk/tests/framework/datetime/Duration.test.ts
ViewVC logotype

Annotation of /trunk/tests/framework/datetime/Duration.test.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 575 - (hide annotations)
Mon Jul 29 17:59:26 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 6361 byte(s)
chore: add trunk
1 rakinar2 575 import Duration from "@framework/datetime/Duration";
2     import { describe, expect, it } from "vitest";
3    
4     describe("Duration", () => {
5     describe("constructor", () => {
6     it("should create a new instance with default values", () => {
7     const duration = new Duration({});
8     expect(duration.years).toBe(0);
9     expect(duration.months).toBe(0);
10     expect(duration.weeks).toBe(0);
11     expect(duration.days).toBe(0);
12     expect(duration.hours).toBe(0);
13     expect(duration.minutes).toBe(0);
14     expect(duration.seconds).toBe(0);
15     expect(duration.milliseconds).toBe(0);
16     });
17    
18     it("should create a new instance with the given values", () => {
19     const duration = new Duration({
20     years: 1,
21     months: 2,
22     weeks: 3,
23     days: 4,
24     hours: 5,
25     minutes: 6,
26     seconds: 7,
27     milliseconds: 8
28     });
29     expect(duration.years).toBe(1);
30     expect(duration.months).toBe(2);
31     expect(duration.weeks).toBe(3);
32     expect(duration.days).toBe(4);
33     expect(duration.hours).toBe(5);
34     expect(duration.minutes).toBe(6);
35     expect(duration.seconds).toBe(7);
36     expect(duration.milliseconds).toBe(8);
37     });
38     });
39    
40     describe("toJSON", () => {
41     it("should return the serialized duration", () => {
42     const duration = new Duration({
43     years: 1,
44     months: 2,
45     weeks: 3,
46     days: 4,
47     hours: 5,
48     minutes: 6,
49     seconds: 7,
50     milliseconds: 8
51     });
52    
53     expect(duration.toJSON()).toBe(
54     1 * 31536000000 +
55     2 * 2592000000 +
56     3 * 604800000 +
57     4 * 86400000 +
58     5 * 3600000 +
59     6 * 60000 +
60     7 * 1000 +
61     8
62     );
63     });
64     });
65    
66     describe("toString", () => {
67     it("should return the formatted duration", () => {
68     const duration = new Duration({
69     years: 1,
70     months: 2,
71     weeks: 3,
72     days: 4,
73     hours: 5,
74     minutes: 6,
75     seconds: 7,
76     milliseconds: 8
77     });
78     expect(duration.toString()).toBe(
79     "1 year 2 months 3 weeks 4 days 5 hours 6 minutes 7 seconds 8 milliseconds"
80     );
81     });
82    
83     it("should return the formatted duration with only the given fields", () => {
84     const duration = new Duration({
85     years: 1,
86     days: 4,
87     hours: 5,
88     minutes: 6,
89     seconds: 7,
90     milliseconds: 8
91     });
92     expect(duration.toString()).toBe(
93     "1 year 4 days 5 hours 6 minutes 7 seconds 8 milliseconds"
94     );
95     });
96     });
97    
98     describe("toPrimitive", () => {
99     it("should return the duration as a string", () => {
100     const duration = new Duration({
101     years: 1,
102     months: 2,
103     weeks: 3,
104     days: 4,
105     hours: 5,
106     minutes: 6,
107     seconds: 7,
108     milliseconds: 8
109     });
110    
111     expect(String(duration)).toBe(
112     "1 year 2 months 3 weeks 4 days 5 hours 6 minutes 7 seconds 8 milliseconds"
113     );
114     });
115    
116     it("should return the duration as a number", () => {
117     const duration = new Duration({
118     years: 1,
119     months: 2,
120     weeks: 3,
121     days: 4,
122     hours: 5,
123     minutes: 6,
124     seconds: 7,
125     milliseconds: 8
126     });
127    
128     expect(+duration).toBe(
129     1 * 31536000000 +
130     2 * 2592000000 +
131     3 * 604800000 +
132     4 * 86400000 +
133     5 * 3600000 +
134     6 * 60000 +
135     7 * 1000 +
136     8
137     );
138     });
139     });
140    
141     describe("toMilliseconds", () => {
142     it("should return the total duration in milliseconds", () => {
143     const duration = new Duration({
144     years: 1,
145     months: 2,
146     weeks: 3,
147     days: 4,
148     hours: 5,
149     minutes: 6,
150     seconds: 7,
151     milliseconds: 8
152     });
153    
154     expect(duration.toMilliseconds()).toBe(
155     1 * 31536000000 +
156     2 * 2592000000 +
157     3 * 604800000 +
158     4 * 86400000 +
159     5 * 3600000 +
160     6 * 60000 +
161     7 * 1000 +
162     8
163     );
164     });
165     });
166    
167     describe("fromDurationStringExpression", () => {
168     it("should return the duration in milliseconds", () => {
169     expect(Duration.fromDurationStringExpression("1 y", true)).toBe(31536000000);
170     expect(Duration.fromDurationStringExpression("1 mo", true)).toBe(2592000000);
171     expect(Duration.fromDurationStringExpression("1 w", true)).toBe(604800000);
172     expect(Duration.fromDurationStringExpression("1 d", true)).toBe(86400000);
173     expect(Duration.fromDurationStringExpression("1 h", true)).toBe(3600000);
174     expect(Duration.fromDurationStringExpression("1 m", true)).toBe(60000);
175     expect(Duration.fromDurationStringExpression("1 s", true)).toBe(1000);
176     expect(Duration.fromDurationStringExpression("1 ms", true)).toBe(1);
177     });
178    
179     it("should return the duration", () => {
180     expect(Duration.fromDurationStringExpression("1y4month2d6h20minutes")).toEqual(
181     new Duration({
182     years: 1,
183     months: 4,
184     weeks: 0,
185     days: 2,
186     hours: 6,
187     minutes: 20,
188     seconds: 0,
189     milliseconds: 0
190     })
191     );
192     });
193     });
194     });

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26