You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils-test.ts 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { ComponentQualifier } from '../../../types/component';
  21. import { MetricKey } from '../../../types/metrics';
  22. import * as utils from '../utils';
  23. const MEASURES = [
  24. {
  25. metric: {
  26. id: '1',
  27. key: MetricKey.lines_to_cover,
  28. type: 'INT',
  29. name: 'Lines to Cover',
  30. domain: 'Coverage'
  31. },
  32. value: '431',
  33. period: { index: 1, value: '70' },
  34. leak: '70'
  35. },
  36. {
  37. metric: {
  38. id: '2',
  39. key: MetricKey.coverage,
  40. type: 'PERCENT',
  41. name: 'Coverage',
  42. domain: 'Coverage'
  43. },
  44. value: '99.3',
  45. period: { index: 1, value: '0.0999999999999943' },
  46. leak: '0.0999999999999943'
  47. },
  48. {
  49. metric: {
  50. id: '3',
  51. key: MetricKey.duplicated_lines_density,
  52. type: 'PERCENT',
  53. name: 'Duplicated Lines (%)',
  54. domain: 'Duplications'
  55. },
  56. value: '3.2',
  57. period: { index: 1, value: '0.0' },
  58. leak: '0.0'
  59. }
  60. ];
  61. describe('filterMeasures', () => {
  62. it('should exclude banned measures', () => {
  63. expect(
  64. utils.filterMeasures([
  65. { metric: { id: '1', key: MetricKey.bugs, name: 'Bugs', type: 'INT' } },
  66. {
  67. metric: {
  68. id: '2',
  69. key: MetricKey.critical_violations,
  70. name: 'Critical Violations',
  71. type: 'INT'
  72. }
  73. }
  74. ])
  75. ).toHaveLength(1);
  76. });
  77. });
  78. describe('sortMeasures', () => {
  79. it('should sort based on the config', () => {
  80. expect(
  81. utils.sortMeasures('Reliability', [
  82. {
  83. metric: {
  84. id: '1',
  85. key: MetricKey.reliability_remediation_effort,
  86. name: 'new_bugs',
  87. type: 'INT'
  88. }
  89. },
  90. {
  91. metric: {
  92. id: '2',
  93. key: MetricKey.new_reliability_remediation_effort,
  94. name: 'bugs',
  95. type: 'INT'
  96. }
  97. },
  98. { metric: { id: '3', key: MetricKey.new_bugs, name: 'new_bugs', type: 'INT' } },
  99. { metric: { id: '4', key: MetricKey.bugs, name: 'bugs', type: 'INT' } },
  100. 'overall_category'
  101. ])
  102. ).toMatchSnapshot();
  103. });
  104. });
  105. describe('groupByDomains', () => {
  106. it('should correctly group by domains', () => {
  107. expect(utils.groupByDomains(MEASURES)).toMatchSnapshot();
  108. });
  109. it('should be memoized', () => {
  110. expect(utils.groupByDomains(MEASURES)).toBe(utils.groupByDomains(MEASURES));
  111. });
  112. });
  113. describe('parseQuery', () => {
  114. it('should correctly parse the url query', () => {
  115. expect(utils.parseQuery({})).toEqual({
  116. metric: utils.DEFAULT_METRIC,
  117. selected: '',
  118. view: utils.DEFAULT_VIEW
  119. });
  120. expect(utils.parseQuery({ metric: 'foo', selected: 'bar', view: 'tree' })).toEqual({
  121. metric: 'foo',
  122. selected: 'bar',
  123. view: 'tree'
  124. });
  125. });
  126. it('should be memoized', () => {
  127. const query = { metric: 'foo', selected: 'bar', view: 'tree' };
  128. expect(utils.parseQuery(query)).toBe(utils.parseQuery(query));
  129. });
  130. });
  131. describe('serializeQuery', () => {
  132. it('should correctly serialize the query', () => {
  133. expect(utils.serializeQuery({ metric: '', selected: '', view: 'list' })).toEqual({
  134. view: 'list'
  135. });
  136. expect(utils.serializeQuery({ metric: 'foo', selected: 'bar', view: 'tree' })).toEqual({
  137. metric: 'foo',
  138. selected: 'bar'
  139. });
  140. });
  141. it('should be memoized', () => {
  142. const query: utils.Query = { metric: 'foo', selected: 'bar', view: 'tree' };
  143. expect(utils.serializeQuery(query)).toBe(utils.serializeQuery(query));
  144. });
  145. });
  146. describe('extract measure', () => {
  147. const componentBuilder = (qual: ComponentQualifier): T.ComponentMeasure => {
  148. return {
  149. qualifier: qual,
  150. key: '1',
  151. name: 'TEST',
  152. measures: [
  153. {
  154. metric: 'alert_status',
  155. value: '3.2',
  156. period: { index: 1, value: '0.0' }
  157. },
  158. {
  159. metric: 'releasability_rating',
  160. value: '3.2',
  161. period: { index: 1, value: '0.0' }
  162. },
  163. {
  164. metric: 'releasability_effort',
  165. value: '3.2',
  166. period: { index: 1, value: '0.0' }
  167. }
  168. ]
  169. };
  170. };
  171. it('should ban quality gate for app', () => {
  172. const measure = utils.banQualityGateMeasure(componentBuilder(ComponentQualifier.Application));
  173. expect(measure).toHaveLength(0);
  174. });
  175. it('should ban quality gate for portfolio', () => {
  176. const measure = utils.banQualityGateMeasure(componentBuilder(ComponentQualifier.Portfolio));
  177. expect(measure).toHaveLength(3);
  178. });
  179. it('should ban quality gate for file', () => {
  180. const measure = utils.banQualityGateMeasure(componentBuilder(ComponentQualifier.File));
  181. expect(measure).toHaveLength(2);
  182. measure.forEach(({ metric }) => {
  183. expect(['releasability_rating', 'releasability_effort']).toContain(metric);
  184. });
  185. });
  186. });