utils.spec.jsx 6.1 KB

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