utils.spec.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import {ProjectFixture} from 'sentry-fixture/project';
  2. import {
  3. descopeFeatureName,
  4. escapeDoubleQuotes,
  5. explodeSlug,
  6. extractMultilineFields,
  7. parseRepo,
  8. sortProjects,
  9. valueIsEqual,
  10. } from 'sentry/utils';
  11. describe('utils.valueIsEqual', function () {
  12. it('should return true when objects are deeply equal', function () {
  13. const isEqual = valueIsEqual(
  14. {
  15. username: 'foo',
  16. teams: ['bar', 'baz'],
  17. avatar: {
  18. avatarType: 'gravatar',
  19. avatarUuid: null,
  20. },
  21. },
  22. {
  23. username: 'foo',
  24. teams: ['bar', 'baz'],
  25. avatar: {
  26. avatarType: 'gravatar',
  27. avatarUuid: null,
  28. },
  29. },
  30. true
  31. );
  32. expect(isEqual).toBe(true);
  33. });
  34. it('should return false when objects are not deeply equal', function () {
  35. const isEqual = valueIsEqual(
  36. {
  37. username: 'foo',
  38. teams: ['bar', 'baz'],
  39. avatar: {
  40. avatarType: 'gravatar',
  41. avatarUuid: null,
  42. },
  43. },
  44. {
  45. username: 'foo',
  46. teams: ['bar', 'baz'],
  47. avatar: {
  48. avatarType: 'notGravatar',
  49. avatarUuid: null,
  50. },
  51. },
  52. true
  53. );
  54. expect(isEqual).toBe(false);
  55. });
  56. it('should return true when objects are shallowly equal', function () {
  57. const isEqual = valueIsEqual(
  58. {
  59. username: 'foo',
  60. team: 'bar',
  61. avatar: 'gravatar',
  62. },
  63. {
  64. username: 'foo',
  65. team: 'bar',
  66. avatar: 'gravatar',
  67. },
  68. false
  69. );
  70. expect(isEqual).toBe(true);
  71. });
  72. it('should return false when objects are not shallowly equal', function () {
  73. const isEqual = valueIsEqual(
  74. {
  75. username: 'foo',
  76. team: 'bar',
  77. avatar: 'gravatar',
  78. },
  79. {
  80. username: 'foo',
  81. team: 'bar',
  82. avatar: 'notGravatar',
  83. },
  84. false
  85. );
  86. expect(isEqual).toBe(false);
  87. });
  88. it('should not blow up when comparing null value to an object', function () {
  89. let isEqual = valueIsEqual(null, {username: 'foo'}, true);
  90. expect(isEqual).toBe(false);
  91. isEqual = valueIsEqual(
  92. {
  93. username: 'foo',
  94. teams: ['bar', 'baz'],
  95. avatar: null,
  96. },
  97. {
  98. username: 'foo',
  99. teams: ['bar', 'baz'],
  100. avatar: {
  101. avatarType: 'notGravatar',
  102. avatarUuid: null,
  103. },
  104. },
  105. true
  106. );
  107. expect(isEqual).toBe(false);
  108. });
  109. });
  110. describe('utils.extractMultilineFields', function () {
  111. it('should work for basic, simple values', function () {
  112. expect(extractMultilineFields('one\ntwo\nthree')).toEqual(['one', 'two', 'three']);
  113. });
  114. it('should return an empty array if only whitespace', function () {
  115. expect(extractMultilineFields(' \n \n\n\n \n')).toEqual([]);
  116. });
  117. it('should trim values and ignore empty lines', function () {
  118. expect(
  119. extractMultilineFields(
  120. `one
  121. two
  122. three
  123. four
  124. five`
  125. )
  126. ).toEqual(['one', 'two', 'three', 'four', 'five']);
  127. });
  128. });
  129. describe('utils.parseRepo', function () {
  130. it('should work for simple github url', function () {
  131. expect(parseRepo('github.com/example/example')).toEqual('example/example');
  132. });
  133. it('should work for full github url', function () {
  134. expect(parseRepo('https://github.com/example/example')).toEqual('example/example');
  135. });
  136. it('should work for trailing slash', function () {
  137. expect(parseRepo('https://github.com/example/example/')).toEqual('example/example');
  138. });
  139. it('should work for simple BitBucket url', function () {
  140. expect(parseRepo('bitbucket.org/example/example')).toEqual('example/example');
  141. });
  142. it('should work for full BitBucket url', function () {
  143. expect(parseRepo('https://bitbucket.org/example/example')).toEqual('example/example');
  144. });
  145. it('should work for trailing Bitbucket slash', function () {
  146. expect(parseRepo('https://bitbucket.org/example/example/')).toEqual(
  147. 'example/example'
  148. );
  149. });
  150. it('should work for repo only', function () {
  151. expect(parseRepo('example/example')).toEqual('example/example');
  152. });
  153. it('should parse repo from url with extra info', function () {
  154. expect(parseRepo('github.com/example/example/commits/adsadsa')).toEqual(
  155. 'example/example'
  156. );
  157. });
  158. });
  159. describe('utils.explodeSlug', function () {
  160. it('replaces slug special chars with whitespace', function () {
  161. expect(explodeSlug('test--slug__replace-')).toEqual('test slug replace');
  162. });
  163. });
  164. describe('utils.projectDisplayCompare', function () {
  165. it('sorts by bookmark and project slug', function () {
  166. const projects = [
  167. ProjectFixture({isBookmarked: true, slug: 'm'}),
  168. ProjectFixture({isBookmarked: false, slug: 'm'}),
  169. ProjectFixture({isBookmarked: false, slug: 'a'}),
  170. ProjectFixture({isBookmarked: true, slug: 'a'}),
  171. ProjectFixture({isBookmarked: true, slug: 'z'}),
  172. ProjectFixture({isBookmarked: false, slug: 'z'}),
  173. ];
  174. const expected = [
  175. expect.objectContaining({isBookmarked: true, slug: 'a'}),
  176. expect.objectContaining({isBookmarked: true, slug: 'm'}),
  177. expect.objectContaining({isBookmarked: true, slug: 'z'}),
  178. expect.objectContaining({isBookmarked: false, slug: 'a'}),
  179. expect.objectContaining({isBookmarked: false, slug: 'm'}),
  180. expect.objectContaining({isBookmarked: false, slug: 'z'}),
  181. ];
  182. const sortedProjects = sortProjects(projects);
  183. expect(sortedProjects).toEqual(expected);
  184. });
  185. });
  186. describe('utils.descopeFeatureName', function () {
  187. it('descopes the feature name', () => {
  188. [
  189. ['organizations:feature', 'feature'],
  190. ['projects:feature', 'feature'],
  191. ['unknown-scope:feature', 'unknown-scope:feature'],
  192. ['', ''],
  193. ].forEach(([input, expected]) => expect(descopeFeatureName(input)).toEqual(expected));
  194. });
  195. });
  196. describe('utils.escapeDoubleQuotes', function () {
  197. // test cases from https://gist.github.com/getify/3667624
  198. it('should escape any unescaped double quotes', function () {
  199. const cases = [
  200. ['a"b', 'a\\"b'], //
  201. ['a\\"b', 'a\\"b'], //
  202. ['a\\\\"b', 'a\\\\\\"b'],
  203. ['a"b"c', 'a\\"b\\"c'],
  204. ['a""b', 'a\\"\\"b'],
  205. ['""', '\\"\\"'],
  206. ];
  207. for (const testCase of cases) {
  208. const [input, expected] = testCase;
  209. expect(escapeDoubleQuotes(input)).toBe(expected);
  210. }
  211. // should return the same input as the output
  212. const cases2 = ['ab', 'a\\"b', 'a\\\\\\"b'];
  213. for (const test of cases2) {
  214. expect(escapeDoubleQuotes(test)).toBe(test);
  215. }
  216. // don't unnecessarily escape
  217. const actual = escapeDoubleQuotes(escapeDoubleQuotes(escapeDoubleQuotes('a"b')));
  218. expect(actual).toBe('a\\"b');
  219. });
  220. });