-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.spec.ts
91 lines (76 loc) · 2.19 KB
/
utils.spec.ts
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
79
80
81
82
83
84
85
86
87
88
89
90
91
import {
generateTimestamp,
parseLogLines,
millisecondsToDdHhMmSs
} from './utils'
describe('utils', () => {
describe('generateTimestamp', () => {
let realDate: DateConstructor
beforeAll(() => {
const currentDate = new Date('2020-10-02T10:10:10.10Z')
realDate = Date
global.Date = class extends DateConstructor {
constructor(date: string) {
if (date) {
return super(date)
}
return currentDate
}
} as DateConstructor
})
test('should generate a timestamp in the correct format', () => {
const expectedTimestamp = '2020102101010'
const timestamp = generateTimestamp()
expect(timestamp).toEqual(expectedTimestamp)
})
test('should generate plain text log from json', () => {
const expectedLog = `line1\nline2\nline3\nline4\n`
const json = {
items: [
{
line: 'line1'
},
{
line: 'line2'
},
{
line: 'line3'
},
{
line: 'line4'
}
]
}
expect(parseLogLines(json)).toEqual(expectedLog)
})
afterAll(() => {
global.Date = realDate
})
})
describe('millisecondsToDdHhMmSs', () => {
it('should throw an error if not supported type was provided', () => {
const error = new Error('Not supported attribute type.')
expect(() => millisecondsToDdHhMmSs(undefined)).toThrow(error)
})
it('should process negative number', () => {
expect(millisecondsToDdHhMmSs(-1000)).toEqual(
'0 day(s); 0 hour(s); 0 minute(s); 1 second(s)'
)
})
it('should process 0', () => {
expect(millisecondsToDdHhMmSs(0)).toEqual(
'0 day(s); 0 hour(s); 0 minute(s); 0 second(s)'
)
})
it('should process number with floating point', () => {
expect(millisecondsToDdHhMmSs(8 * 60 * 60 * 1000 + 0.93326263)).toEqual(
'0 day(s); 8 hour(s); 0 minute(s); 0 second(s)'
)
})
it('should process number', () => {
expect(millisecondsToDdHhMmSs(24 * 60 * 60 * 1000)).toEqual(
'1 day(s); 0 hour(s); 0 minute(s); 0 second(s)'
)
})
})
})