string-utils-test.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import moment from 'moment';
  2. describe('StringUtils', () => {
  3. const StringUtils = require('../string-utils');
  4. describe('formatMetric', () => {
  5. const f = StringUtils.formatMetric;
  6. it('it should render 0', () => {
  7. expect(f(0)).toBe('0.00');
  8. });
  9. it('it should render get rid of trailing zeros', () => {
  10. expect(f(2104)).toBe('2.104k');
  11. expect(f(21100)).toBe('21.1k');
  12. expect(f(2120001)).toBe('2.12M');
  13. });
  14. });
  15. describe('longestCommonPrefix', () => {
  16. const f = StringUtils.longestCommonPrefix;
  17. it('it should return the longest common prefix', () => {
  18. expect(f(['interspecies', 'interstellar'])).toBe('inters');
  19. expect(f(['space', 'space'])).toBe('space');
  20. expect(f([''])).toBe('');
  21. expect(f(['prefix', 'suffix'])).toBe('');
  22. });
  23. });
  24. describe('ipToPaddedString', () => {
  25. const f = StringUtils.ipToPaddedString;
  26. it('it should return the formatted IP', () => {
  27. expect(f('10.244.253.4')).toBe('010.244.253.004');
  28. expect(f('0.24.3.4')).toBe('000.024.003.004');
  29. });
  30. });
  31. describe('humanizedRoundedDownDuration', () => {
  32. const f = StringUtils.humanizedRoundedDownDuration;
  33. it('it should return the humanized duration', () => {
  34. expect(f(moment.duration(0))).toBe('now');
  35. expect(f(moment.duration(0.9 * 1000))).toBe('now');
  36. expect(f(moment.duration(1 * 1000))).toBe('1 second');
  37. expect(f(moment.duration(8.62 * 60 * 1000))).toBe('8 minutes');
  38. expect(f(moment.duration(14.99 * 60 * 60 * 1000))).toBe('14 hours');
  39. expect(f(moment.duration(5.2 * 24 * 60 * 60 * 1000))).toBe('5 days');
  40. expect(f(moment.duration(11.8 * 30 * 24 * 60 * 60 * 1000))).toBe('11 months');
  41. expect(f(moment.duration(12.8 * 30 * 24 * 60 * 60 * 1000))).toBe('1 year');
  42. expect(f(moment.duration(9.4 * 12 * 30 * 24 * 60 * 60 * 1000))).toBe('9 years');
  43. });
  44. });
  45. });