Files
kandev/apps/web/lib/utils/format-bytes.test.ts
T

31 lines
907 B
TypeScript

import { describe, it, expect } from "vitest";
import { formatBytes } from "./format-bytes";
describe("formatBytes", () => {
it("returns '-' for nullish input", () => {
expect(formatBytes(null)).toBe("-");
expect(formatBytes(undefined)).toBe("-");
});
it("returns '0 B' for zero or negative input", () => {
expect(formatBytes(0)).toBe("0 B");
expect(formatBytes(-1)).toBe("0 B");
});
it("renders integer bytes below 1 KB", () => {
expect(formatBytes(1)).toBe("1 B");
expect(formatBytes(1023)).toBe("1023 B");
});
it("renders KB with one decimal", () => {
expect(formatBytes(1024)).toBe("1.0 KB");
expect(formatBytes(2048)).toBe("2.0 KB");
});
it("renders MB / GB / TB", () => {
expect(formatBytes(1024 * 1024)).toBe("1.0 MB");
expect(formatBytes(1024 * 1024 * 1024)).toBe("1.0 GB");
expect(formatBytes(1024 ** 4)).toBe("1.0 TB");
});
});