prettierBytes.test.js 894 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. const prettierBytes = require('./prettierBytes')
  2. const testData = [
  3. [2, '2 B'],
  4. [9, '9 B'],
  5. [25, '25 B'],
  6. [235, '235 B'],
  7. [2335, '2.3 KB'],
  8. [23552, '23 KB'],
  9. [235520, '230 KB'],
  10. [2355520, '2.2 MB'],
  11. [23555520, '22 MB'],
  12. [235555520, '225 MB'],
  13. [2355555520, '2.2 GB'],
  14. [23555555520, '22 GB'],
  15. [235556555520, '219 GB'],
  16. [2355556655520, '2.1 TB'],
  17. [23555566655520, '21 TB'],
  18. [235555566665520, '214 TB'],
  19. ]
  20. describe('prettierBytes', () => {
  21. it('should convert the specified number of bytes to a human-readable string like 236 MB', () => {
  22. testData.forEach(function (data) {
  23. expect(prettierBytes(data[0])).toEqual(data[1])
  24. })
  25. })
  26. it('throws on non-number', () => {
  27. expect(() => {
  28. prettierBytes('this is a string')
  29. }).toThrow()
  30. })
  31. it('throws on NaN', () => {
  32. expect(() => {
  33. prettierBytes(NaN)
  34. }).toThrow()
  35. })
  36. })