formatters.spec.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import {
  2. formatMetricsUsingUnitAndOp,
  3. formatMetricUsingFixedUnit,
  4. formattingSupportedMetricUnits,
  5. } from 'sentry/utils/metrics/formatters';
  6. describe('formatMetricsUsingUnitAndOp', () => {
  7. it('should format the value according to the unit', () => {
  8. // Test cases for different units
  9. expect(formatMetricsUsingUnitAndOp(123456, 'millisecond')).toEqual('2.06min');
  10. expect(formatMetricsUsingUnitAndOp(5000, 'second')).toEqual('1.39hr');
  11. expect(formatMetricsUsingUnitAndOp(600, 'byte')).toEqual('600 B');
  12. expect(formatMetricsUsingUnitAndOp(4096, 'kibibyte')).toEqual('4.0 MiB');
  13. expect(formatMetricsUsingUnitAndOp(3145728, 'megabyte')).toEqual('3.15 TB');
  14. expect(formatMetricsUsingUnitAndOp(3145728, 'megabytes')).toEqual('3.15 TB');
  15. expect(formatMetricsUsingUnitAndOp(0.99, 'ratio')).toEqual('99%');
  16. expect(formatMetricsUsingUnitAndOp(99, 'percent')).toEqual('99%');
  17. });
  18. it('should handle value as null', () => {
  19. expect(formatMetricsUsingUnitAndOp(null, 'millisecond')).toEqual('—');
  20. expect(formatMetricsUsingUnitAndOp(null, 'byte')).toEqual('—');
  21. expect(formatMetricsUsingUnitAndOp(null, 'megabyte')).toEqual('—');
  22. });
  23. it('should format count operation as a number', () => {
  24. expect(formatMetricsUsingUnitAndOp(99, 'none', 'count')).toEqual('99');
  25. expect(formatMetricsUsingUnitAndOp(null, 'none', 'count')).toEqual('');
  26. });
  27. });
  28. describe('formatMetricUsingFixedUnit', () => {
  29. it('should return the formatted value with the short form of the given unit', () => {
  30. expect(formatMetricUsingFixedUnit(123456, 'millisecond')).toBe('123,456ms');
  31. expect(formatMetricUsingFixedUnit(2.1231245, 'kibibyte')).toBe('2.12KiB');
  32. expect(formatMetricUsingFixedUnit(1222.1231245, 'megabyte')).toBe('1,222.12MB');
  33. });
  34. it.each(formattingSupportedMetricUnits.filter(unit => unit !== 'none'))(
  35. 'appends a unit for every supported one (except none)',
  36. unit => {
  37. expect(formatMetricUsingFixedUnit(1234.56, unit)).toMatch(/1,234\.56.+/);
  38. }
  39. );
  40. it('should not append a unit for unsupported units and "none"', () => {
  41. expect(formatMetricUsingFixedUnit(1234.56, 'randomunitname')).toBe('1,234.56');
  42. expect(formatMetricUsingFixedUnit(1234.56, 'none')).toBe('1,234.56');
  43. });
  44. it.each(['sum', 'count_unique', 'avg', 'max', 'p50', 'p75', 'p95', 'p99'])(
  45. 'should append a unit for every operation (except count)',
  46. op => {
  47. expect(formatMetricUsingFixedUnit(1234.56, 'second', op)).toMatch(/1,234\.56s/);
  48. }
  49. );
  50. it('should not append a unit for count operation', () => {
  51. expect(formatMetricUsingFixedUnit(1234.56, 'second', 'count')).toBe('1,234.56');
  52. });
  53. });