format.spec.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { formatFileSize, formatNumber, formatTime } from './format'
  2. describe('formatNumber', () => {
  3. test('should correctly format integers', () => {
  4. expect(formatNumber(1234567)).toBe('1,234,567')
  5. })
  6. test('should correctly format decimals', () => {
  7. expect(formatNumber(1234567.89)).toBe('1,234,567.89')
  8. })
  9. test('should correctly handle string input', () => {
  10. expect(formatNumber('1234567')).toBe('1,234,567')
  11. })
  12. test('should correctly handle zero', () => {
  13. expect(formatNumber(0)).toBe(0)
  14. })
  15. test('should correctly handle negative numbers', () => {
  16. expect(formatNumber(-1234567)).toBe('-1,234,567')
  17. })
  18. test('should correctly handle empty input', () => {
  19. expect(formatNumber('')).toBe('')
  20. })
  21. })
  22. describe('formatFileSize', () => {
  23. test('should return the input if it is falsy', () => {
  24. expect(formatFileSize(0)).toBe(0)
  25. })
  26. test('should format bytes correctly', () => {
  27. expect(formatFileSize(500)).toBe('500.00B')
  28. })
  29. test('should format kilobytes correctly', () => {
  30. expect(formatFileSize(1500)).toBe('1.46KB')
  31. })
  32. test('should format megabytes correctly', () => {
  33. expect(formatFileSize(1500000)).toBe('1.43MB')
  34. })
  35. test('should format gigabytes correctly', () => {
  36. expect(formatFileSize(1500000000)).toBe('1.40GB')
  37. })
  38. test('should format terabytes correctly', () => {
  39. expect(formatFileSize(1500000000000)).toBe('1.36TB')
  40. })
  41. test('should format petabytes correctly', () => {
  42. expect(formatFileSize(1500000000000000)).toBe('1.33PB')
  43. })
  44. })
  45. describe('formatTime', () => {
  46. test('should return the input if it is falsy', () => {
  47. expect(formatTime(0)).toBe(0)
  48. })
  49. test('should format seconds correctly', () => {
  50. expect(formatTime(30)).toBe('30.00 sec')
  51. })
  52. test('should format minutes correctly', () => {
  53. expect(formatTime(90)).toBe('1.50 min')
  54. })
  55. test('should format hours correctly', () => {
  56. expect(formatTime(3600)).toBe('1.00 h')
  57. })
  58. test('should handle large numbers', () => {
  59. expect(formatTime(7200)).toBe('2.00 h')
  60. })
  61. })