Open History to see the last commit
1import { afterEach, describe, expect, it, vi } from "vite-plus/test";
2
3import { formatBytes } from "./formatBytes";
4
5const runtime = vi.hoisted(() => ({ formattingLocale: "en" as string | undefined }));
6vi.mock("../i18n/runtime", () => ({ getFormattingLocale: () => runtime.formattingLocale }));
7
8afterEach(() => {
9 runtime.formattingLocale = "en";
10});
11
12describe("formatBytes", () => {
13 it.each([
14 [0, "0 B"],
15 [1_023, "1023 B"],
16 [1_024, "1.00 KB"],
17 [1_536, "1.50 KB"],
18 [10_235, "10.0 KB"],
19 [102_350, "100 KB"],
20 [1_047_552, "1023 KB"],
21 [1_048_576, "1.00 MB"],
22 [5 * 1_024 ** 4, "5.00 TB"],
23 [2_048 * 1_024 ** 4, "2048 TB"],
24 ])("formats %d bytes as %s", (value, label) => {
25 expect(formatBytes(value)).toBe(label);
26 });
27
28 it.each([
29 [1_023.6, "1.00 KB"],
30 [1_048_100, "1.00 MB"],
31 [1_073_217_536, "1.00 GB"],
32 ])("moves %d bytes to the next unit when rounding reaches 1024", (value, label) => {
33 expect(formatBytes(value)).toBe(label);
34 });
35
36 it.each([
37 [1_288_490_189, "1,20 GB"],
38 [1_048_100, "1,00 MB"],
39 [2_048 * 1_024 ** 4, "2048 TB"],
40 ])("formats %d bytes as %s in German", (value, label) => {
41 runtime.formattingLocale = "de";
42 expect(formatBytes(value)).toBe(label);
43 });
44});