utils.test.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { fixedZero, isUrl } from './utils';
  2. describe('fixedZero tests', () => {
  3. it('should not pad large numbers', () => {
  4. expect(fixedZero(10)).toEqual(10);
  5. expect(fixedZero(11)).toEqual(11);
  6. expect(fixedZero(15)).toEqual(15);
  7. expect(fixedZero(20)).toEqual(20);
  8. expect(fixedZero(100)).toEqual(100);
  9. expect(fixedZero(1000)).toEqual(1000);
  10. });
  11. it('should pad single digit numbers and return them as string', () => {
  12. expect(fixedZero(0)).toEqual('00');
  13. expect(fixedZero(1)).toEqual('01');
  14. expect(fixedZero(2)).toEqual('02');
  15. expect(fixedZero(3)).toEqual('03');
  16. expect(fixedZero(4)).toEqual('04');
  17. expect(fixedZero(5)).toEqual('05');
  18. expect(fixedZero(6)).toEqual('06');
  19. expect(fixedZero(7)).toEqual('07');
  20. expect(fixedZero(8)).toEqual('08');
  21. expect(fixedZero(9)).toEqual('09');
  22. });
  23. });
  24. describe('isUrl tests', () => {
  25. it('should return false for invalid and corner case inputs', () => {
  26. expect(isUrl([])).toBeFalsy();
  27. expect(isUrl({})).toBeFalsy();
  28. expect(isUrl(false)).toBeFalsy();
  29. expect(isUrl(true)).toBeFalsy();
  30. expect(isUrl(NaN)).toBeFalsy();
  31. expect(isUrl(null)).toBeFalsy();
  32. expect(isUrl(undefined)).toBeFalsy();
  33. expect(isUrl()).toBeFalsy();
  34. expect(isUrl('')).toBeFalsy();
  35. });
  36. it('should return false for invalid URLs', () => {
  37. expect(isUrl('foo')).toBeFalsy();
  38. expect(isUrl('bar')).toBeFalsy();
  39. expect(isUrl('bar/test')).toBeFalsy();
  40. expect(isUrl('http:/example.com/')).toBeFalsy();
  41. expect(isUrl('ttp://example.com/')).toBeFalsy();
  42. });
  43. it('should return true for valid URLs', () => {
  44. expect(isUrl('http://example.com/')).toBeTruthy();
  45. expect(isUrl('https://example.com/')).toBeTruthy();
  46. expect(isUrl('http://example.com/test/123')).toBeTruthy();
  47. expect(isUrl('https://example.com/test/123')).toBeTruthy();
  48. expect(isUrl('http://example.com/test/123?foo=bar')).toBeTruthy();
  49. expect(isUrl('https://example.com/test/123?foo=bar')).toBeTruthy();
  50. expect(isUrl('http://www.example.com/')).toBeTruthy();
  51. expect(isUrl('https://www.example.com/')).toBeTruthy();
  52. expect(isUrl('http://www.example.com/test/123')).toBeTruthy();
  53. expect(isUrl('https://www.example.com/test/123')).toBeTruthy();
  54. expect(isUrl('http://www.example.com/test/123?foo=bar')).toBeTruthy();
  55. expect(isUrl('https://www.example.com/test/123?foo=bar')).toBeTruthy();
  56. });
  57. });