utils.spec.jsx 5.7 KB

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