parsePercent.spec.tsx 884 B

12345678910111213141516171819202122232425262728
  1. import {parsePercent} from 'sentry/views/settings/dynamicSampling/utils/parsePercent';
  2. describe('parsePercent', () => {
  3. it('parses a valid percent', () => {
  4. expect(parsePercent('50')).toEqual(0.5);
  5. expect(parsePercent('100')).toEqual(1);
  6. expect(parsePercent('0')).toEqual(0);
  7. expect(parsePercent('50.5')).toEqual(0.505);
  8. expect(parsePercent('1.5%')).toEqual(0.015);
  9. });
  10. it('falls back to default value', () => {
  11. expect(parsePercent(undefined, 0.1)).toEqual(0.1);
  12. expect(parsePercent(null, 0.2)).toEqual(0.2);
  13. expect(parsePercent('', 0.3)).toEqual(0.3);
  14. expect(parsePercent('invalid', 0.4)).toEqual(0.4);
  15. });
  16. it('clamps to 0-1 range', () => {
  17. expect(parsePercent('-1')).toEqual(0);
  18. expect(parsePercent('101')).toEqual(1);
  19. });
  20. it('return 0 as default fallback', () => {
  21. expect(parsePercent(null)).toEqual(0);
  22. });
  23. });