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.

testMocks.ts 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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 { InjectedRouter } from 'react-router';
  21. import { Location } from 'history';
  22. import { ParsedAnalysis } from '../apps/projectActivity/utils';
  23. import { Profile } from '../apps/quality-profiles/types';
  24. export function mockAlmApplication(overrides: Partial<T.AlmApplication> = {}): T.AlmApplication {
  25. return {
  26. backgroundColor: '#444444',
  27. iconPath: '/images/sonarcloud/github-white.svg',
  28. installationUrl: 'https://github.com/apps/greg-sonarcloud/installations/new',
  29. key: 'github',
  30. name: 'GitHub',
  31. ...overrides
  32. };
  33. }
  34. export function mockAlmOrganization(overrides: Partial<T.AlmOrganization> = {}): T.AlmOrganization {
  35. return {
  36. avatar: 'http://example.com/avatar',
  37. almUrl: 'https://github.com/foo',
  38. description: 'description-foo',
  39. key: 'foo',
  40. name: 'foo',
  41. personal: false,
  42. privateRepos: 0,
  43. publicRepos: 3,
  44. url: 'http://example.com/foo',
  45. ...overrides
  46. };
  47. }
  48. export function mockParsedAnalysis(overrides: Partial<ParsedAnalysis> = {}): ParsedAnalysis {
  49. return {
  50. date: new Date('2017-03-01T09:36:01+0100'),
  51. events: [],
  52. key: 'foo',
  53. projectVersion: '1.0',
  54. ...overrides
  55. };
  56. }
  57. export function mockAnalysisEvent(overrides: Partial<T.AnalysisEvent> = {}): T.AnalysisEvent {
  58. return {
  59. category: 'QUALITY_GATE',
  60. key: 'E11',
  61. description: 'Lorem ipsum dolor sit amet',
  62. name: 'Lorem ipsum',
  63. qualityGate: {
  64. status: 'ERROR',
  65. stillFailing: true,
  66. failing: [
  67. {
  68. key: 'foo',
  69. name: 'Foo',
  70. branch: 'master'
  71. },
  72. {
  73. key: 'bar',
  74. name: 'Bar',
  75. branch: 'feature/bar'
  76. }
  77. ]
  78. },
  79. ...overrides
  80. };
  81. }
  82. export function mockAppState(overrides: Partial<T.AppState> = {}): T.AppState {
  83. return {
  84. defaultOrganization: 'foo',
  85. edition: 'community',
  86. productionDatabase: true,
  87. qualifiers: ['TRK'],
  88. settings: {},
  89. version: '1.0',
  90. ...overrides
  91. };
  92. }
  93. export function mockComponent(overrides: Partial<T.Component> = {}): T.Component {
  94. return {
  95. breadcrumbs: [],
  96. key: 'my-project',
  97. name: 'MyProject',
  98. organization: 'foo',
  99. qualifier: 'TRK',
  100. qualityGate: { isDefault: true, key: '30', name: 'Sonar way' },
  101. qualityProfiles: [
  102. {
  103. deleted: false,
  104. key: 'my-qp',
  105. language: 'ts',
  106. name: 'Sonar way'
  107. }
  108. ],
  109. tags: [],
  110. ...overrides
  111. };
  112. }
  113. export function mockQualityGateStatusCondition(
  114. overrides: Partial<T.QualityGateStatusCondition> = {}
  115. ): T.QualityGateStatusCondition {
  116. return {
  117. actual: '10',
  118. error: '0',
  119. level: 'ERROR',
  120. metric: 'foo',
  121. op: 'GT',
  122. ...overrides
  123. };
  124. }
  125. export function mockCurrentUser(overrides: Partial<T.LoggedInUser> = {}): T.LoggedInUser {
  126. return {
  127. groups: [],
  128. isLoggedIn: true,
  129. login: 'luke',
  130. name: 'Skywalker',
  131. scmAccounts: [],
  132. ...overrides
  133. };
  134. }
  135. export function mockEvent(overrides = {}) {
  136. return {
  137. target: { blur() {} },
  138. currentTarget: { blur() {} },
  139. preventDefault() {},
  140. stopPropagation() {},
  141. ...overrides
  142. } as any;
  143. }
  144. export function mockIssue(withLocations = false, overrides: Partial<T.Issue> = {}) {
  145. const issue: T.Issue = {
  146. actions: [],
  147. component: 'main.js',
  148. componentLongName: 'main.js',
  149. componentQualifier: 'FIL',
  150. componentUuid: 'foo1234',
  151. creationDate: '2017-03-01T09:36:01+0100',
  152. flows: [],
  153. fromHotspot: false,
  154. key: 'AVsae-CQS-9G3txfbFN2',
  155. line: 25,
  156. message: 'Reduce the number of conditional operators (4) used in the expression',
  157. organization: 'myorg',
  158. project: 'myproject',
  159. projectKey: 'foo',
  160. projectName: 'Foo',
  161. projectOrganization: 'org',
  162. rule: 'javascript:S1067',
  163. ruleName: 'foo',
  164. secondaryLocations: [],
  165. severity: 'MAJOR',
  166. status: 'OPEN',
  167. textRange: { startLine: 25, endLine: 26, startOffset: 0, endOffset: 15 },
  168. transitions: [],
  169. type: 'BUG'
  170. };
  171. function loc(): T.FlowLocation {
  172. return {
  173. component: 'main.js',
  174. textRange: { startLine: 1, startOffset: 1, endLine: 2, endOffset: 2 }
  175. };
  176. }
  177. if (withLocations) {
  178. issue.flows = [[loc(), loc(), loc()], [loc(), loc()]];
  179. issue.secondaryLocations = [loc(), loc()];
  180. }
  181. return {
  182. ...issue,
  183. ...overrides
  184. };
  185. }
  186. export function mockLocation(overrides: Partial<Location> = {}): Location {
  187. return {
  188. action: 'PUSH',
  189. key: 'key',
  190. pathname: '/path',
  191. query: {},
  192. search: '',
  193. state: {},
  194. ...overrides
  195. };
  196. }
  197. export function mockMeasure(overrides: Partial<T.Measure> = {}): T.Measure {
  198. return {
  199. bestValue: true,
  200. metric: 'bugs',
  201. periods: [
  202. {
  203. bestValue: true,
  204. index: 1,
  205. value: '1.0'
  206. }
  207. ],
  208. value: '1.0',
  209. ...overrides
  210. };
  211. }
  212. export function mockOrganization(overrides: Partial<T.Organization> = {}): T.Organization {
  213. return { key: 'foo', name: 'Foo', ...overrides };
  214. }
  215. export function mockOrganizationWithAdminActions(
  216. overrides: Partial<T.Organization> = {},
  217. actionsOverrides: Partial<T.Organization['actions']> = {}
  218. ) {
  219. return mockOrganization({ actions: { admin: true, ...actionsOverrides }, ...overrides });
  220. }
  221. export function mockOrganizationWithAlm(
  222. overrides: Partial<T.Organization> = {},
  223. almOverrides: Partial<T.Organization['alm']> = {}
  224. ): T.Organization {
  225. return mockOrganization({
  226. alm: { key: 'github', membersSync: false, url: 'https://github.com/foo', ...almOverrides },
  227. ...overrides
  228. });
  229. }
  230. export function mockQualityGate(overrides: Partial<T.QualityGate> = {}): T.QualityGate {
  231. return {
  232. id: 1,
  233. name: 'qualitygate',
  234. ...overrides
  235. };
  236. }
  237. export function mockPullRequest(overrides: Partial<T.PullRequest> = {}): T.PullRequest {
  238. return {
  239. analysisDate: '2018-01-01',
  240. base: 'master',
  241. branch: 'feature/foo/bar',
  242. key: '1001',
  243. title: 'Foo Bar feature',
  244. ...overrides
  245. };
  246. }
  247. export function mockQualityProfile(overrides: Partial<Profile> = {}): Profile {
  248. return {
  249. activeDeprecatedRuleCount: 2,
  250. activeRuleCount: 10,
  251. childrenCount: 0,
  252. depth: 1,
  253. isBuiltIn: false,
  254. isDefault: false,
  255. isInherited: false,
  256. key: 'key',
  257. language: 'js',
  258. languageName: 'JavaScript',
  259. name: 'name',
  260. projectCount: 3,
  261. organization: 'foo',
  262. ...overrides
  263. };
  264. }
  265. export function mockQualityGateProjectStatus(
  266. overrides: Partial<T.QualityGateProjectStatus['projectStatus']> = {}
  267. ): T.QualityGateProjectStatus {
  268. return {
  269. projectStatus: {
  270. conditions: [
  271. {
  272. actualValue: '0',
  273. comparator: 'GT',
  274. errorThreshold: '1.0',
  275. metricKey: 'new_bugs',
  276. periodIndex: 1,
  277. status: 'OK'
  278. }
  279. ],
  280. ignoredConditions: false,
  281. status: 'OK',
  282. ...overrides
  283. }
  284. };
  285. }
  286. export function mockRouter(overrides: { push?: Function; replace?: Function } = {}) {
  287. return {
  288. createHref: jest.fn(),
  289. createPath: jest.fn(),
  290. go: jest.fn(),
  291. goBack: jest.fn(),
  292. goForward: jest.fn(),
  293. isActive: jest.fn(),
  294. push: jest.fn(),
  295. replace: jest.fn(),
  296. setRouteLeaveHook: jest.fn(),
  297. ...overrides
  298. } as InjectedRouter;
  299. }
  300. export function mockRule(overrides: Partial<T.Rule> = {}): T.Rule {
  301. return {
  302. key: 'javascript:S1067',
  303. lang: 'js',
  304. langName: 'JavaScript',
  305. name: 'Use foo',
  306. severity: 'MAJOR',
  307. status: 'READY',
  308. sysTags: ['a', 'b'],
  309. tags: ['x'],
  310. type: 'CODE_SMELL',
  311. ...overrides
  312. } as T.Rule;
  313. }
  314. export function mockShortLivingBranch(
  315. overrides: Partial<T.ShortLivingBranch> = {}
  316. ): T.ShortLivingBranch {
  317. return {
  318. analysisDate: '2018-01-01',
  319. isMain: false,
  320. name: 'release-1.0',
  321. mergeBranch: 'master',
  322. type: 'SHORT',
  323. ...overrides
  324. };
  325. }
  326. export function mockLongLivingBranch(
  327. overrides: Partial<T.LongLivingBranch> = {}
  328. ): T.LongLivingBranch {
  329. return {
  330. analysisDate: '2018-01-01',
  331. isMain: false,
  332. name: 'master',
  333. type: 'LONG',
  334. ...overrides
  335. };
  336. }