queryString.spec.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import utils from 'sentry/utils/queryString';
  2. describe('addQueryParamsToExistingUrl', function () {
  3. it('adds new query params to existing query params', function () {
  4. const url = 'https://example.com?value=3';
  5. const newParams = {
  6. id: 4,
  7. };
  8. expect(utils.addQueryParamsToExistingUrl(url, newParams)).toBe(
  9. 'https://example.com/?id=4&value=3'
  10. );
  11. });
  12. it('adds new query params without existing query params', function () {
  13. const url = 'https://example.com';
  14. const newParams = {
  15. id: 4,
  16. };
  17. expect(utils.addQueryParamsToExistingUrl(url, newParams)).toBe(
  18. 'https://example.com/?id=4'
  19. );
  20. });
  21. it('returns empty string no url is passed', function () {
  22. let url;
  23. const newParams = {
  24. id: 4,
  25. };
  26. expect(utils.addQueryParamsToExistingUrl(url, newParams)).toBe('');
  27. });
  28. });
  29. describe('appendTagCondition', function () {
  30. it('adds simple values', function () {
  31. const result = utils.appendTagCondition('error+text', 'color', 'red');
  32. expect(result).toEqual('error+text color:red');
  33. });
  34. it('handles array current value', function () {
  35. const result = utils.appendTagCondition(['', 'thing'], 'color', 'red');
  36. expect(result).toEqual('thing color:red');
  37. });
  38. it('handles empty string current value', function () {
  39. const result = utils.appendTagCondition('', 'color', 'red');
  40. expect(result).toEqual('color:red');
  41. });
  42. it('handles null current value', function () {
  43. const result = utils.appendTagCondition(null, 'color', 'red');
  44. expect(result).toEqual('color:red');
  45. });
  46. it('wraps values with spaces', function () {
  47. const result = utils.appendTagCondition(null, 'color', 'purple red');
  48. expect(result).toEqual('color:"purple red"');
  49. });
  50. it('wraps values with colon', function () {
  51. const result = utils.appendTagCondition(null, 'color', 'id:red');
  52. expect(result).toEqual('color:"id:red"');
  53. });
  54. it('handles user tag values', function () {
  55. let result = utils.appendTagCondition('', 'user', 'something');
  56. expect(result).toEqual('user:something');
  57. result = utils.appendTagCondition('', 'user', 'id:1');
  58. expect(result).toEqual('user:"id:1"');
  59. result = utils.appendTagCondition('', 'user', 'email:foo@example.com');
  60. expect(result).toEqual('user:"email:foo@example.com"');
  61. result = utils.appendTagCondition('', 'user', 'name:jill jones');
  62. expect(result).toEqual('user:"name:jill jones"');
  63. });
  64. });
  65. describe('decodeScalar()', function () {
  66. it('unwraps array values', function () {
  67. expect(utils.decodeScalar(['one', 'two'])).toEqual('one');
  68. });
  69. it('handles strings', function () {
  70. expect(utils.decodeScalar('one')).toEqual('one');
  71. });
  72. it('handles falsey values', function () {
  73. expect(utils.decodeScalar(undefined)).toBeUndefined();
  74. // @ts-expect-error
  75. expect(utils.decodeScalar(false)).toBeUndefined();
  76. expect(utils.decodeScalar('')).toBeUndefined();
  77. });
  78. it('uses fallback values', function () {
  79. expect(utils.decodeScalar('value', 'default')).toEqual('value');
  80. expect(utils.decodeScalar('', 'default')).toEqual('default');
  81. expect(utils.decodeScalar(null, 'default')).toEqual('default');
  82. expect(utils.decodeScalar(undefined, 'default')).toEqual('default');
  83. expect(utils.decodeScalar([], 'default')).toEqual('default');
  84. });
  85. });
  86. describe('decodeList()', function () {
  87. it('wraps string values', function () {
  88. expect(utils.decodeList('one')).toEqual(['one']);
  89. });
  90. it('handles arrays', function () {
  91. expect(utils.decodeList(['one', 'two'])).toEqual(['one', 'two']);
  92. });
  93. it('handles falsey values', function () {
  94. expect(utils.decodeList(undefined)).toEqual([]);
  95. // @ts-expect-error
  96. expect(utils.decodeList(false)).toEqual([]);
  97. expect(utils.decodeList('')).toEqual([]);
  98. });
  99. });
  100. describe('decodeInteger()', function () {
  101. it('handles integer strings', function () {
  102. expect(utils.decodeInteger('1')).toEqual(1);
  103. expect(utils.decodeInteger('1.2')).toEqual(1);
  104. expect(utils.decodeInteger('1.9')).toEqual(1);
  105. expect(utils.decodeInteger('foo')).toEqual(undefined);
  106. expect(utils.decodeInteger('foo', 2020)).toEqual(2020);
  107. });
  108. it('handles arrays', function () {
  109. expect(utils.decodeInteger(['1', 'foo'])).toEqual(1);
  110. expect(utils.decodeInteger(['1.2', 'foo'])).toEqual(1);
  111. expect(utils.decodeInteger(['1.9', 'foo'])).toEqual(1);
  112. expect(utils.decodeInteger(['foo', '1'])).toEqual(undefined);
  113. expect(utils.decodeInteger(['foo'], 2020)).toEqual(2020);
  114. });
  115. it('handles falsey values', function () {
  116. expect(utils.decodeInteger(undefined, 2020)).toEqual(2020);
  117. // @ts-expect-error
  118. expect(utils.decodeInteger(false, 2020)).toEqual(2020);
  119. expect(utils.decodeInteger('', 2020)).toEqual(2020);
  120. });
  121. });