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