file.test.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { describe, it, expect } from 'vitest';
  2. import { formatFileSize } from '../../utils/file';
  3. describe('formatFileSize', () => {
  4. it('returns "0 B" for 0 bytes', () => {
  5. expect(formatFileSize(0)).toBe('0 B');
  6. });
  7. it('returns bytes without decimals for values under 1 KB', () => {
  8. expect(formatFileSize(1)).toBe('1 B');
  9. expect(formatFileSize(500)).toBe('500 B');
  10. expect(formatFileSize(1023)).toBe('1023 B');
  11. });
  12. it('returns KB with 1 decimal for values under 1 MB', () => {
  13. expect(formatFileSize(1024)).toBe('1.0 KB');
  14. expect(formatFileSize(1536)).toBe('1.5 KB');
  15. expect(formatFileSize(10240)).toBe('10.0 KB');
  16. });
  17. it('returns MB with 1 decimal for values under 1 GB', () => {
  18. expect(formatFileSize(1048576)).toBe('1.0 MB');
  19. expect(formatFileSize(1572864)).toBe('1.5 MB');
  20. expect(formatFileSize(10485760)).toBe('10.0 MB');
  21. });
  22. it('returns GB with 1 decimal for values under 1 TB', () => {
  23. expect(formatFileSize(1073741824)).toBe('1.0 GB');
  24. expect(formatFileSize(1610612736)).toBe('1.5 GB');
  25. });
  26. it('returns TB with 1 decimal for very large values', () => {
  27. expect(formatFileSize(1099511627776)).toBe('1.0 TB');
  28. expect(formatFileSize(1649267441664)).toBe('1.5 TB');
  29. });
  30. it('handles edge cases at unit boundaries', () => {
  31. expect(formatFileSize(1023)).toBe('1023 B');
  32. expect(formatFileSize(1024)).toBe('1.0 KB');
  33. expect(formatFileSize(1048575)).toBe('1024.0 KB');
  34. expect(formatFileSize(1048576)).toBe('1.0 MB');
  35. });
  36. });