Adding vitest for testing
Currently implemented - FormatBytes - MathHelpers
This commit is contained in:
81
tests/FormatBytes.test.js
Normal file
81
tests/FormatBytes.test.js
Normal file
@@ -0,0 +1,81 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
|
||||
import {
|
||||
formatBytes,
|
||||
formatBytesLong,
|
||||
stringByteFormat
|
||||
} from '../src/utils/FormatBytes.mjs';
|
||||
|
||||
let bytes_map = [
|
||||
{
|
||||
"in": -123123123,
|
||||
"out": "-120237.42kB",
|
||||
"out_l": "-117.42 MB",
|
||||
},
|
||||
{
|
||||
"in": 999999, // KB-1,
|
||||
"out": "0.95MB",
|
||||
"out_l": "976.56 KB",
|
||||
},
|
||||
{
|
||||
"in": 999999999, // MB-1
|
||||
"out": "0.93GB",
|
||||
"out_l": "953.67 MB",
|
||||
},
|
||||
{
|
||||
"in": 254779258, // MB-n
|
||||
"out": "0.24GB",
|
||||
"out_l": "242.98 MB",
|
||||
},
|
||||
{
|
||||
"in": 999999999999999, // TB-1
|
||||
"out": "0.89PB",
|
||||
"out_l": "909.49 TB",
|
||||
},
|
||||
{
|
||||
"in": 588795544887632, // TB-n
|
||||
"out": "0.52PB",
|
||||
"out_l": "535.51 TB",
|
||||
},
|
||||
{
|
||||
"in": 999999999999999999n, // PB-1
|
||||
"out": "0.87EB",
|
||||
"out_l": "888.18 PB",
|
||||
},
|
||||
{
|
||||
"in": 9223372036854775807n, // MAX INT
|
||||
"out": "8EB",
|
||||
"out_l": "8.00 EB",
|
||||
},
|
||||
// {
|
||||
// "in": 999999999999999999999n, // EB-1
|
||||
// "out": "",
|
||||
// }
|
||||
];
|
||||
|
||||
describe("formatBytes", () => {
|
||||
it('convert bytes to human readable, round up to next set', () => {
|
||||
// expect(formatBytes(1021152)).toBe('0.97MB');
|
||||
for (const bytes of bytes_map) {
|
||||
expect(formatBytes(bytes.in)).toBe(bytes.out);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("formatBytesLong", () => {
|
||||
it('convert bytes to human readable, keep on current set', () => {
|
||||
expect(formatBytesLong(1021152)).toBe('997.22 KB');
|
||||
for (const bytes of bytes_map) {
|
||||
expect(formatBytesLong(bytes.in)).toBe(bytes.out_l);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("stringByteFormat", () => {
|
||||
it('convert a string with byte suffix to bytes', () => {
|
||||
expect(stringByteFormat('997.22 KB')).toBe(1021153);
|
||||
expect(stringByteFormat('997.22 KB', true)).toBe(1021153.28);
|
||||
});
|
||||
});
|
||||
|
||||
// __END__
|
||||
30
tests/MathHelpers.test.js
Normal file
30
tests/MathHelpers.test.js
Normal file
@@ -0,0 +1,30 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
|
||||
import {
|
||||
dec2hex,
|
||||
getRandomIntInclusive,
|
||||
roundPrecision
|
||||
} from '../src/utils/MathHelpers.mjs';
|
||||
|
||||
describe("dec2hex", () => {
|
||||
it('should convert decimal to hexadecimal values', () => {
|
||||
expect(dec2hex(255)).toBe('0xff');
|
||||
});
|
||||
});
|
||||
|
||||
describe("getRandomIntInclusive", () => {
|
||||
it('should create a random number from min to max inclusive', () => {
|
||||
let val = getRandomIntInclusive(1, 5);
|
||||
expect(val).toBeGreaterThanOrEqual(1);
|
||||
expect(val).toBeLessThanOrEqual(5);
|
||||
});
|
||||
});
|
||||
|
||||
describe("roundPrecision", () => {
|
||||
it('should round numbers to a given precision', () => {
|
||||
let val = roundPrecision(10.1234, 2);
|
||||
expect(val).toBe(10.12);
|
||||
});
|
||||
});
|
||||
|
||||
// __END__
|
||||
Reference in New Issue
Block a user