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.

ProjectsServiceMock.tsx 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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 { MeasuresForProjects } from '../../types/measures';
  21. import { Dict } from '../../types/types';
  22. import { ComponentRaw, Facet, getScannableProjects, searchProjects } from '../components';
  23. import { addFavorite } from '../favorites';
  24. import { getMeasuresForProjects } from '../measures';
  25. import { mockFacets, mockProjectMeasures, mockProjects } from './data/projects';
  26. export class ProjectsServiceMock {
  27. projects: ComponentRaw[];
  28. measuresByProjectByMetric: Dict<Dict<MeasuresForProjects>> = {};
  29. facets: Facet[];
  30. constructor() {
  31. const { projects, measures, facets } = this.initData();
  32. this.projects = projects;
  33. this.measuresByProjectByMetric = measures;
  34. this.facets = facets;
  35. jest.mocked(searchProjects).mockImplementation(this.handleSearchProjects);
  36. jest.mocked(getMeasuresForProjects).mockImplementation(this.handleGetMeasuresForProjects);
  37. jest.mocked(getScannableProjects).mockImplementation(this.handleGetScannableProjects);
  38. jest.mocked(addFavorite).mockImplementation(this.handleAddFavorite);
  39. }
  40. nclocSort = (a: ComponentRaw, b: ComponentRaw) => {
  41. const { value: va = '0' } = (this.measuresByProjectByMetric[a.key] ?? { ncloc: { value: 0 } })
  42. .ncloc;
  43. const { value: vb = '0' } = (this.measuresByProjectByMetric[b.key] ?? { ncloc: { value: 0 } })
  44. .ncloc;
  45. return parseInt(va, 10) - parseInt(vb, 10);
  46. };
  47. handleSearchProjects = ({
  48. ps,
  49. facets = '',
  50. filter = '',
  51. s,
  52. }: {
  53. ps: number;
  54. facets: string;
  55. filter: string;
  56. s?: string;
  57. }) => {
  58. /*
  59. * Parse params
  60. */
  61. const facetKeys = facets.split(',');
  62. const filters = filter.split('and');
  63. const favorite = filters.includes('isFavorite');
  64. /*
  65. * Build response
  66. */
  67. const results = this.projects.filter((p) => {
  68. return !favorite || p.isFavorite === favorite;
  69. });
  70. const sorted = s === 'ncloc' ? results.sort(this.nclocSort) : results;
  71. return Promise.resolve({
  72. components: sorted.slice(0, ps),
  73. facets: this.facets.filter(({ property }) => {
  74. return facetKeys.includes(property);
  75. }),
  76. paging: {
  77. pageIndex: 1,
  78. pageSize: ps,
  79. total: sorted.length,
  80. },
  81. });
  82. };
  83. handleGetMeasuresForProjects = (projectKeys: string[] = [], metricKeys: string[] = []) => {
  84. const measures = projectKeys.flatMap((projectKey) => {
  85. const projectMeasures = this.measuresByProjectByMetric[projectKey] ?? {};
  86. return metricKeys.map((key) => projectMeasures[key]).filter((v) => v !== undefined);
  87. });
  88. return Promise.resolve(measures);
  89. };
  90. handleGetScannableProjects = () => {
  91. return Promise.resolve({
  92. projects: [],
  93. });
  94. };
  95. handleAddFavorite = (componentKey: string) => {
  96. const project = this.projects.find(({ key }) => componentKey === key);
  97. if (project) {
  98. project.isFavorite = true;
  99. return Promise.resolve();
  100. }
  101. return Promise.reject('project not found');
  102. };
  103. initData = () => {
  104. const projects = mockProjects();
  105. const measures = mockProjectMeasures();
  106. const facets = mockFacets();
  107. return { projects, measures, facets };
  108. };
  109. reset = () => {
  110. this.initData();
  111. };
  112. }