Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/resources/scripts/lib/formatters.spec.ts
7458 views
1
import { bytesToString, ip, mbToBytes } from '@/lib/formatters';
2
3
describe('@/lib/formatters.ts', function () {
4
describe('mbToBytes()', function () {
5
it('should convert from MB to Bytes', function () {
6
expect(mbToBytes(1)).toBe(1_048_576);
7
expect(mbToBytes(0)).toBe(0);
8
expect(mbToBytes(0.1)).toBe(104_857);
9
expect(mbToBytes(0.001)).toBe(1_048);
10
expect(mbToBytes(1024)).toBe(1_073_741_824);
11
});
12
});
13
14
describe('bytesToString()', function () {
15
it.each([
16
[0, '0 Bytes'],
17
[0.5, '0 Bytes'],
18
[0.9, '0 Bytes'],
19
[100, '100 Bytes'],
20
[100.25, '100.25 Bytes'],
21
[100.998, '101 Bytes'],
22
[512, '512 Bytes'],
23
[1000, '1000 Bytes'],
24
[1024, '1 KiB'],
25
[5068, '4.95 KiB'],
26
[10_000, '9.77 KiB'],
27
[10_240, '10 KiB'],
28
[11_864, '11.59 KiB'],
29
[1_000_000, '976.56 KiB'],
30
[1_024_000, '1000 KiB'],
31
[1_025_000, '1000.98 KiB'],
32
[1_048_576, '1 MiB'],
33
[1_356_000, '1.29 MiB'],
34
[1_000_000_000, '953.67 MiB'],
35
[1_070_000_100, '1020.43 MiB'],
36
[1_073_741_824, '1 GiB'],
37
[1_678_342_000, '1.56 GiB'],
38
[1_000_000_000_000, '931.32 GiB'],
39
[1_099_511_627_776, '1 TiB'],
40
])('should format %d bytes as "%s"', function (input, output) {
41
expect(bytesToString(input)).toBe(output);
42
});
43
});
44
45
describe('ip()', function () {
46
it('should format an IPv4 address', function () {
47
expect(ip('127.0.0.1')).toBe('127.0.0.1');
48
});
49
50
it('should format an IPv6 address', function () {
51
expect(ip(':::1')).toBe('[:::1]');
52
expect(ip('2001:db8::')).toBe('[2001:db8::]');
53
});
54
55
it('should handle random inputs', function () {
56
expect(ip('1')).toBe('1');
57
expect(ip('foobar')).toBe('foobar');
58
expect(ip('127.0.0.1:25565')).toBe('[127.0.0.1:25565]');
59
});
60
});
61
});
62
63