You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

format.spec.ts 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { downloadFile, 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.00 bytes')
  28. })
  29. test('should format kilobytes correctly', () => {
  30. expect(formatFileSize(1500)).toBe('1.46 KB')
  31. })
  32. test('should format megabytes correctly', () => {
  33. expect(formatFileSize(1500000)).toBe('1.43 MB')
  34. })
  35. test('should format gigabytes correctly', () => {
  36. expect(formatFileSize(1500000000)).toBe('1.40 GB')
  37. })
  38. test('should format terabytes correctly', () => {
  39. expect(formatFileSize(1500000000000)).toBe('1.36 TB')
  40. })
  41. test('should format petabytes correctly', () => {
  42. expect(formatFileSize(1500000000000000)).toBe('1.33 PB')
  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. })
  62. describe('downloadFile', () => {
  63. test('should create a link and trigger a download correctly', () => {
  64. // Mock data
  65. const blob = new Blob(['test content'], { type: 'text/plain' })
  66. const fileName = 'test-file.txt'
  67. const mockUrl = 'blob:mockUrl'
  68. // Mock URL.createObjectURL
  69. const createObjectURLMock = jest.fn().mockReturnValue(mockUrl)
  70. const revokeObjectURLMock = jest.fn()
  71. Object.defineProperty(window.URL, 'createObjectURL', { value: createObjectURLMock })
  72. Object.defineProperty(window.URL, 'revokeObjectURL', { value: revokeObjectURLMock })
  73. // Mock createElement and appendChild
  74. const mockLink = {
  75. href: '',
  76. download: '',
  77. click: jest.fn(),
  78. remove: jest.fn(),
  79. }
  80. const createElementMock = jest.spyOn(document, 'createElement').mockReturnValue(mockLink as any)
  81. const appendChildMock = jest.spyOn(document.body, 'appendChild').mockImplementation((node: Node) => {
  82. return node
  83. })
  84. // Call the function
  85. downloadFile({ data: blob, fileName })
  86. // Assertions
  87. expect(createObjectURLMock).toHaveBeenCalledWith(blob)
  88. expect(createElementMock).toHaveBeenCalledWith('a')
  89. expect(mockLink.href).toBe(mockUrl)
  90. expect(mockLink.download).toBe(fileName)
  91. expect(appendChildMock).toHaveBeenCalledWith(mockLink)
  92. expect(mockLink.click).toHaveBeenCalled()
  93. expect(mockLink.remove).toHaveBeenCalled()
  94. expect(revokeObjectURLMock).toHaveBeenCalledWith(mockUrl)
  95. // Clean up mocks
  96. jest.restoreAllMocks()
  97. })
  98. })