|
| 1 | +import { fileSize } from "../fileSize"; |
| 2 | + |
| 3 | +describe("fileSize", () => { |
| 4 | + it("should return '0B' for size 0", () => { |
| 5 | + expect(fileSize(0)).toBe("0 B"); |
| 6 | + }); |
| 7 | + |
| 8 | + it("should return '1B' for size 1", () => { |
| 9 | + expect(fileSize(1)).toBe("1 B"); |
| 10 | + }); |
| 11 | + |
| 12 | + it("should return '1KB' for size 1024", () => { |
| 13 | + expect(fileSize(1024)).toBe("1 KB"); |
| 14 | + }); |
| 15 | + |
| 16 | + it("should return '1MB' for size 1048576 (1024 * 1024)", () => { |
| 17 | + expect(fileSize(1024 * 1024)).toBe("1 MB"); |
| 18 | + }); |
| 19 | + |
| 20 | + it("should return '1GB' for size 1073741824 (1024 * 1024 * 1024)", () => { |
| 21 | + expect(fileSize(1024 * 1024 * 1024)).toBe("1 GB"); |
| 22 | + }); |
| 23 | + |
| 24 | + it("should handle large sizes correctly", () => { |
| 25 | + expect(fileSize(1024 ** 5.0001)).toBe("1 PB"); |
| 26 | + }); |
| 27 | + |
| 28 | + it("empty for negative sizes", () => { |
| 29 | + expect(fileSize(-1)).toBe(""); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should return one decimal digit for sizes less than 100", () => { |
| 33 | + expect(fileSize(64.06 * 1024)).toBe("64.1 KB"); |
| 34 | + expect(fileSize(85.4 * 1024)).toBe("85.4 KB"); |
| 35 | + expect(fileSize(99.9 * 1024)).toBe("99.9 KB"); |
| 36 | + }); |
| 37 | + |
| 38 | + it("should return two decimal digits for sizes less than 10", () => { |
| 39 | + expect(fileSize(5.009 * 1024)).toBe("5.01 KB"); |
| 40 | + expect(fileSize(9.11 * 1024)).toBe("9.11 KB"); |
| 41 | + expect(fileSize(1.91 * 1024)).toBe("1.91 KB"); |
| 42 | + |
| 43 | + expect(fileSize(100.91 * 1024)).toBe("100 KB"); |
| 44 | + }); |
| 45 | + |
| 46 | + it("should return the round sizes for sizes below the thresholds", () => { |
| 47 | + expect(fileSize(85.03 * 1024)).toBe("85 KB"); |
| 48 | + expect(fileSize(7.001 * 1024)).toBe("7 KB"); |
| 49 | + expect(fileSize(70268742)).toBe("67 MB"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should return the correct unit for sizes just below the next threshold", () => { |
| 53 | + expect(fileSize(1023)).toBe("1023 B"); |
| 54 | + expect(fileSize(1024 * 1024 - 1)).toBe("1023 KB"); |
| 55 | + }); |
| 56 | +}); |
0 commit comments