UNPKG

894 BJavaScriptView Raw
1const prettierBytes = require('./prettierBytes')
2
3const testData = [
4 [2, '2 B'],
5 [9, '9 B'],
6 [25, '25 B'],
7 [235, '235 B'],
8 [2335, '2.3 KB'],
9 [23552, '23 KB'],
10 [235520, '230 KB'],
11 [2355520, '2.2 MB'],
12 [23555520, '22 MB'],
13 [235555520, '225 MB'],
14 [2355555520, '2.2 GB'],
15 [23555555520, '22 GB'],
16 [235556555520, '219 GB'],
17 [2355556655520, '2.1 TB'],
18 [23555566655520, '21 TB'],
19 [235555566665520, '214 TB'],
20]
21
22describe('prettierBytes', () => {
23 it('should convert the specified number of bytes to a human-readable string like 236 MB', () => {
24 testData.forEach(function (data) {
25 expect(prettierBytes(data[0])).toEqual(data[1])
26 })
27 })
28
29 it('throws on non-number', () => {
30 expect(() => {
31 prettierBytes('this is a string')
32 }).toThrow()
33 })
34
35 it('throws on NaN', () => {
36 expect(() => {
37 prettierBytes(NaN)
38 }).toThrow()
39 })
40})