Files
outline/shared/utils/files.test.ts
T
Tom Moor 091346dfe8 chore: Migrate to vitest (#12272)
* wip

* Remove obsolete snapshots

* simplify

* chore(test): Convert mocks to TypeScript and tighten fetch mock types

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* Remove unneccessary patches

* Migrate to msw instead of custom fetch mock

* Address PR review comments

- Split chained vi.useFakeTimers().setSystemTime() into separate calls.
- Switch test setup to dynamic imports so EventEmitter.defaultMaxListeners
  assignment runs before module init (static imports were hoisted above it).
- Drop redundant NODE_ENV guard in monkeyPatchSequelizeErrorsForJest; its
  sole caller already gates on env.isTest.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-06 21:10:51 -04:00

86 lines
3.1 KiB
TypeScript

import { bytesToHumanReadable, getFileNameFromUrl } from "./files";
// Mock the browser detection with a mutable value
let mockIsMacValue = false;
vi.mock("./browser", () => ({
get isMac() {
return mockIsMacValue;
},
}));
describe("bytesToHumanReadable", () => {
describe("on macOS (decimal units)", () => {
beforeEach(() => {
mockIsMacValue = true;
});
it("outputs readable string using decimal units", () => {
expect(bytesToHumanReadable(0)).toBe("0 Bytes");
expect(bytesToHumanReadable(0.0)).toBe("0 Bytes");
expect(bytesToHumanReadable(33)).toBe("33 Bytes");
expect(bytesToHumanReadable(500)).toBe("500 Bytes");
expect(bytesToHumanReadable(1000)).toBe("1 KB");
expect(bytesToHumanReadable(15000)).toBe("15 KB");
expect(bytesToHumanReadable(12345)).toBe("12.35 KB");
expect(bytesToHumanReadable(123456)).toBe("123.46 KB");
expect(bytesToHumanReadable(1234567)).toBe("1.23 MB");
expect(bytesToHumanReadable(1234567890)).toBe("1.23 GB");
expect(bytesToHumanReadable(undefined)).toBe("0 Bytes");
});
});
describe("on Windows/other platforms (binary units)", () => {
beforeEach(() => {
mockIsMacValue = false;
});
it("outputs readable string using binary units", () => {
expect(bytesToHumanReadable(0)).toBe("0 Bytes");
expect(bytesToHumanReadable(0.0)).toBe("0 Bytes");
expect(bytesToHumanReadable(33)).toBe("33 Bytes");
expect(bytesToHumanReadable(500)).toBe("500 Bytes");
expect(bytesToHumanReadable(1000)).toBe("1000 Bytes");
expect(bytesToHumanReadable(1024)).toBe("1 KB");
expect(bytesToHumanReadable(1536)).toBe("1.5 KB");
expect(bytesToHumanReadable(15360)).toBe("15 KB");
expect(bytesToHumanReadable(12345)).toBe("12.06 KB");
expect(bytesToHumanReadable(126464)).toBe("123.5 KB");
expect(bytesToHumanReadable(1048576)).toBe("1 MB");
expect(bytesToHumanReadable(1073741824)).toBe("1 GB");
expect(bytesToHumanReadable(undefined)).toBe("0 Bytes");
});
});
describe("platform-specific behavior for issue #10085", () => {
const fileSize = 91435827; // 87.2MB in binary, ~91.44MB in decimal
it("displays correctly on macOS (decimal)", () => {
mockIsMacValue = true;
expect(bytesToHumanReadable(fileSize)).toBe("91.44 MB");
});
it("displays correctly on Windows (binary)", () => {
mockIsMacValue = false;
expect(bytesToHumanReadable(fileSize)).toBe("87.2 MB");
});
});
});
describe("getFileNameFromUrl", () => {
it("returns the filename from a URL", () => {
expect(getFileNameFromUrl("https://example.com/file")).toBe("file");
expect(getFileNameFromUrl("https://example.com/file.txt")).toBe("file.txt");
expect(
getFileNameFromUrl("https://example.com/file.txt?query=string")
).toBe("file.txt");
expect(getFileNameFromUrl("https://example.com/file.txt#hash")).toBe(
"file.txt"
);
expect(
getFileNameFromUrl("https://example.com/file.txt?query=string#hash")
).toBe("file.txt");
expect(getFileNameFromUrl("https://example.com/")).toBe("");
});
});