Skip to content

Commit fde55ff

Browse files
Merge pull request #1 from distributed-lab/feature/testing
Feature/testing
2 parents 198d5c7 + e2901a7 commit fde55ff

4 files changed

Lines changed: 237 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
- `all`: `tsc-alias` package to use aliases in TypeScript
2222
- `root`: `yarn rsc` Release Sanity Check script
2323
- `root`: Rollup and configuration file to build packages for CDN
24-
- `tools`: Handling big numbers
24+
- `@distributedlab/tools`: Handling big numbers
25+
- `@distributedlab/tools`: Add tests for time.ts and duration.ts
2526

2627
### Changed
2728
- `root`: Updated `README.md`
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { Duration } from './duration'
2+
3+
describe('Performs duration class unit test', () => {
4+
test('asDays should return correct value', () => {
5+
const dr = new Duration({ days: 2 })
6+
expect(dr.asDays).toStrictEqual(2)
7+
})
8+
9+
test('days should return correct value', () => {
10+
const dr = new Duration({ days: 2 })
11+
expect(dr.days).toStrictEqual(2)
12+
})
13+
14+
test('minutes should return correct value', () => {
15+
const dr = new Duration({ minutes: 2 })
16+
expect(dr.minutes).toStrictEqual(2)
17+
})
18+
19+
test('asMinutes should return correct value', () => {
20+
const dr = new Duration({ days: 2 })
21+
expect(dr.asMinutes).toStrictEqual(2880)
22+
})
23+
24+
test('seconds should return correct value', () => {
25+
const dr = new Duration({ seconds: 2 })
26+
expect(dr.seconds.toString()).toStrictEqual('2')
27+
})
28+
29+
test('as seconds should return correct value', () => {
30+
const dr = new Duration({ days: 2 })
31+
expect(dr.asSeconds).toStrictEqual(172800)
32+
})
33+
34+
test('milliseconds should return correct value', () => {
35+
const dr = new Duration({ milliseconds: 2 })
36+
expect(dr.milliseconds).toStrictEqual(2)
37+
})
38+
39+
test('as milliseconds should return correct value', () => {
40+
const dr = new Duration({ days: 2 })
41+
expect(dr.asMilliseconds).toStrictEqual(172800000)
42+
})
43+
44+
test('months should return correct value', () => {
45+
const dr = new Duration({ months: 2 })
46+
expect(dr.months).toStrictEqual(2)
47+
})
48+
49+
test('as months should return correct value', () => {
50+
const dr = new Duration({ days: 2 })
51+
expect(dr.asMonths).toStrictEqual(0.06666666666666667)
52+
})
53+
54+
test('hours should return correct value', () => {
55+
const dr = new Duration({ hours: 2 })
56+
expect(dr.hours).toStrictEqual(2)
57+
})
58+
59+
test('as hours should return correct value', () => {
60+
const dr = new Duration({ days: 2 })
61+
expect(dr.asHours).toStrictEqual(48)
62+
})
63+
64+
test('weeks should return correct value', () => {
65+
const dr = new Duration({ weeks: 2 })
66+
expect(dr.weeks).toStrictEqual(2)
67+
})
68+
69+
test('as weeks should return correct value', () => {
70+
const dr = new Duration({ days: 2 })
71+
expect(dr.asWeeks).toStrictEqual(0.2857142857142857)
72+
})
73+
})

packages/tools/src/duration.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import dayjs from 'dayjs'
22
import { Duration as _Duration } from 'dayjs/plugin/duration'
3+
import durationlib from 'dayjs/plugin/duration'
34

45
import { DurationUnitsObject, DurationUnitType } from '@/types'
5-
6+
dayjs.extend(durationlib)
67
export class Duration {
78
readonly #duration: _Duration
89

packages/tools/src/time.test.ts

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
import { Time } from './time'
2+
describe('Performs time class unit test', () => {
3+
test('test add function(time + 1) should return correct value', () => {
4+
const time = new Time('2023-03-03')
5+
time.utc(true)
6+
time.add(1)
7+
8+
expect(time.toDate().getTime().toString()).toStrictEqual('1677801600001')
9+
})
10+
test('test add function(time + 1 min), should return correct value', () => {
11+
const time = new Time('2023-03-03')
12+
time.utc(true)
13+
const plusMin = time.add(1, 'minute')
14+
expect(plusMin.toDate().getTime().toString()).toStrictEqual('1677801660000')
15+
})
16+
test('test validate time, should return wrong value ', () => {
17+
const time = new Time('wrong input')
18+
time.utc(true)
19+
expect(time.isValid).toStrictEqual(false)
20+
})
21+
test('test validate time, should return correct value', () => {
22+
const time = new Time()
23+
time.utc(true)
24+
expect(time.isValid).toStrictEqual(true)
25+
})
26+
test('test clone time, should return correct value', () => {
27+
const time1 = new Time()
28+
time1.utc(true)
29+
const clone = time1.clone()
30+
expect(time1.toDate()).toStrictEqual(clone.toDate())
31+
})
32+
test('test diff func, should return negative value', () => {
33+
const time1 = new Time('2023-03-03')
34+
const time2 = new Time('2023-03-04')
35+
expect(time1.diff(time2)).toStrictEqual(-86400000)
36+
})
37+
38+
test('test diff func, should return positive value', () => {
39+
const time1 = new Time('2023-03-04')
40+
const time2 = new Time('2023-03-03')
41+
expect(time1.diff(time2)).toStrictEqual(86400000)
42+
})
43+
44+
test('test timestamp, should return correct value', () => {
45+
const time = new Time('2023-03-03')
46+
time.utc(true)
47+
expect(time.timestamp).toStrictEqual(1677801600)
48+
})
49+
50+
test('test isSame, should return correct value', () => {
51+
const time1 = new Time('2023-03-03')
52+
const time2 = new Time('2023-03-03')
53+
expect(time2.isSame(time1.toDate())).toEqual(true)
54+
})
55+
56+
test('test isSam func, should return wrong value', () => {
57+
const time1 = new Time('2023-03-03')
58+
const time2 = new Time('2023-03-04')
59+
expect(time2.isSame(time1.toDate())).toEqual(false)
60+
})
61+
62+
test('test after func, should return correct value', () => {
63+
const time1 = new Time('2023-03-03')
64+
const time2 = new Time('2023-03-04')
65+
expect(time2.isAfter(time1.toDate())).toEqual(true)
66+
})
67+
68+
test('test after func, should return wrong value', () => {
69+
const time1 = new Time('2023-03-03')
70+
const time2 = new Time('2023-03-04')
71+
expect(time1.isAfter(time2.toDate())).toEqual(false)
72+
})
73+
74+
test('test before func, should return correct value', () => {
75+
const time1 = new Time('2023-03-03')
76+
const time2 = new Time('2023-03-04')
77+
expect(time1.isBefore(time2.toDate())).toEqual(true)
78+
})
79+
80+
test('test before func, should return wrong value', () => {
81+
const time1 = new Time('2023-03-03')
82+
const time2 = new Time('2023-03-04')
83+
expect(time2.isBefore(time1.toDate())).toEqual(false)
84+
})
85+
86+
test('test beforeOrSame func, should return correct value', () => {
87+
const time1 = new Time('2023-03-03')
88+
const time2 = new Time('2023-03-03')
89+
expect(time1.isSameOrBefore(time2.toDate())).toEqual(true)
90+
})
91+
92+
test('test beforeOrSame func, should return correct value', () => {
93+
const time1 = new Time('2023-03-03')
94+
const time2 = new Time('2023-03-04')
95+
expect(time1.isSameOrBefore(time2.toDate())).toEqual(true)
96+
})
97+
98+
test('test beforeOrSame func, should return wrong value', () => {
99+
const time1 = new Time('2023-03-03')
100+
const time2 = new Time('2023-03-04')
101+
expect(time2.isSameOrBefore(time1.toDate())).toEqual(false)
102+
})
103+
104+
test('test afterOrSame func, should return correct value', () => {
105+
const time1 = new Time('2023-03-03')
106+
const time2 = new Time('2023-03-03')
107+
expect(time2.isSameOrAfter(time1.toDate())).toEqual(true)
108+
})
109+
110+
test('test afterOrSame func, should return correct value', () => {
111+
const time1 = new Time('2023-03-03')
112+
const time2 = new Time('2023-03-04')
113+
expect(time2.isSameOrAfter(time1.toDate())).toEqual(true)
114+
})
115+
116+
test('test afterOrSame func, should return wrong value', () => {
117+
const time1 = new Time('2023-03-03')
118+
const time2 = new Time('2023-03-04')
119+
expect(time1.isSameOrAfter(time2.toDate())).toEqual(false)
120+
})
121+
122+
test('test between func, should return correct value', () => {
123+
const time1 = new Time('2023-03-02')
124+
const time2 = new Time('2023-03-04')
125+
const timeBetween = new Time('2023-03-03')
126+
expect(timeBetween.isBetween(time1.toDate(), time2.toDate())).toEqual(true)
127+
})
128+
129+
test('test between func, should return wrong value', () => {
130+
const time1 = new Time('2023-03-02')
131+
const time2 = new Time('2023-03-04')
132+
const timeBetween = new Time('2023-06-03')
133+
expect(timeBetween.isBetween(time1.toDate(), time2.toDate())).toEqual(false)
134+
})
135+
136+
test('test RFC3339, should return correct RFC3339', () => {
137+
const time = new Time('2023-03-03')
138+
time.utc(true)
139+
expect(time.RFC3339).toEqual('2023-03-03T00:00:00Z')
140+
})
141+
142+
test('test ISO, should return should correct ISO', () => {
143+
const time = new Time('2023-03-03')
144+
time.utc(true)
145+
expect(time.ISO).toEqual('2023-03-03T00:00:00.000Z')
146+
})
147+
148+
test('test subtract func, should return correct value', () => {
149+
const time = new Time('2023-03-03')
150+
time.utc(true)
151+
const subTime = time.subtract(1, 'day')
152+
expect(subTime.toDate().getTime()).toStrictEqual(1677715200000)
153+
})
154+
155+
test('test ms, should return correct value', () => {
156+
const time = new Time('2023-03-03')
157+
time.utc(true)
158+
expect(time.ms).toStrictEqual(1677801600000)
159+
})
160+
})

0 commit comments

Comments
 (0)