-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatting.test.js
128 lines (111 loc) · 3.87 KB
/
formatting.test.js
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { describe, jest, test, afterEach } from '@jest/globals';
import { easyMock, easyResolve } from './mocks.js';
// setup mocks
const terminalLink = jest.fn();
const easyMocks = [
['terminal-link', [['default', terminalLink]]],
['src//api/urls.js', ['puzzleBaseUrl']],
];
easyMock(easyMocks);
// import after mocks set up.
const { puzzleBaseUrl } = await easyResolve(easyMocks);
const {
sizeOfStringInKb,
humanizeMinutesDifference,
humanizeDuration,
clickablePuzzleUrl,
} = await import('../src/formatting.js');
describe('formatting', () => {
afterEach(() => {
jest.resetAllMocks();
});
describe('sizeOfStringInKb()', () => {
test.each([null, undefined, 0, ''])(
'returns 0kb if value is: "%s"',
(value) => {
const result = sizeOfStringInKb(value);
expect(result).toBe('0kb');
}
);
test.each([1234, {}])('throws if value is non-string: "%s"', (value) => {
expect(() => sizeOfStringInKb(value)).toThrow(
'type string or an instance of Buffer'
);
});
test('returns expected value - empty string', () => {
const result = sizeOfStringInKb('');
expect(result).toBe('0kb');
});
test('returns expected value', () => {
const string = 'A'.repeat(550);
const result = sizeOfStringInKb(string);
// assuming each character in string is 1 byte (since A is first 128 chars)
expect(result).toBe('0.55kb');
});
});
describe('humanizeMinutesDifference()', () => {
test('returns expected value - difference under a minute', () => {
const start = new Date();
// add 15 seconds
const end = new Date(start.getTime() + 15000);
const result = humanizeMinutesDifference(start, end);
expect(result).toBe('15s');
});
test('returns expected value - difference under an hour', () => {
const start = new Date();
// add 15 minutes 22 seconds
const end = new Date(start.getTime() + 22000 + 15 * 60000);
const result = humanizeMinutesDifference(start, end);
expect(result).toBe('15m 22s');
});
test('returns expected value - difference over an hour', () => {
const start = new Date();
// add 2 hours 15 minutes 16 seconds
const end = new Date(start.getTime() + 16000 + 15 * 60000 + 120 * 60000);
const result = humanizeMinutesDifference(start, end);
expect(result).toBe('135m 16s');
});
});
describe('humanizeDuration()', () => {
test.each([null, undefined])(
'throws if nanoseconds is: "%s"',
(nanoseconds) => {
expect(humanizeDuration(nanoseconds)).toBe('');
}
);
test('returns expected value - seconds', () => {
const nanoseconds = 4.2 * (1000 * 1000 * 1000);
const result = humanizeDuration(nanoseconds);
expect(result).toBe('4.2s');
});
test('returns expected value - milliseconds', () => {
const nanoseconds = 96.27 * (1000 * 1000);
const result = humanizeDuration(nanoseconds);
expect(result).toBe('96.270ms');
});
test('returns expected value - microseconds', () => {
const nanoseconds = 122.052 * 1000;
const result = humanizeDuration(nanoseconds);
expect(result).toBe('122.052μs');
});
test('returns expected value - nanoseconds', () => {
const nanoseconds = 662;
const result = humanizeDuration(nanoseconds);
expect(result).toBe('662ns');
});
});
describe('clickablePuzzleUrl()', () => {
test('passes year and day to puzzleBaseUrl', () => {
const year = 2022;
const day = 14;
clickablePuzzleUrl(year, day);
expect(puzzleBaseUrl).toHaveBeenCalledWith(year, day);
});
test('passes puzzle url to terminal-link', () => {
const url = 'cool.com';
puzzleBaseUrl.mockReturnValue(url);
clickablePuzzleUrl(2022, 11);
expect(terminalLink).toHaveBeenCalledWith(expect.any(String), url);
});
});
});