aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/api/mocks/FixIssueServiceMock.ts
blob: 56520b2c0403d14316989916932c139e8df9dd0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * SonarQube
 * Copyright (C) 2009-2024 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
import { cloneDeep } from 'lodash';
import {
  checkSuggestionServiceStatus,
  FixParam,
  getFixSuggestionsIssues,
  getSuggestions,
  SuggestionServiceStatus,
  SuggestionServiceStatusCheckResponse,
} from '../fix-suggestions';
import { ISSUE_101, ISSUE_1101 } from './data/ids';

jest.mock('../fix-suggestions');

export type MockSuggestionServiceStatus = SuggestionServiceStatus | 'WTF' | undefined;

export default class FixIssueServiceMock {
  fixSuggestion = {
    id: '70b14d4c-d302-4979-9121-06ac7d563c5c',
    issueId: 'AYsVhClEbjXItrbcN71J',
    explanation:
      "Replaced 'require' statements with 'import' statements to comply with ECMAScript 2015 module management standards.",
    changes: [
      {
        startLine: 6,
        endLine: 7,
        newCode: "import { glob } from 'glob';\nimport fs from 'fs';",
      },
    ],
  };

  serviceStatus: MockSuggestionServiceStatus = 'SUCCESS';

  constructor() {
    jest.mocked(getSuggestions).mockImplementation(this.handleGetFixSuggestion);
    jest.mocked(getFixSuggestionsIssues).mockImplementation(this.handleGetFixSuggestionsIssues);
    jest.mocked(checkSuggestionServiceStatus).mockImplementation(this.handleCheckService);
  }

  handleGetFixSuggestionsIssues = (data: FixParam) => {
    if (data.issueId === ISSUE_1101) {
      return this.reply({ aiSuggestion: 'NOT_AVAILABLE_FILE_LEVEL_ISSUE', id: 'id1' } as const);
    }
    return this.reply({ aiSuggestion: 'AVAILABLE', id: 'id1' } as const);
  };

  handleGetFixSuggestion = (data: FixParam) => {
    if (data.issueId === ISSUE_101) {
      return Promise.reject({ error: { msg: 'Invalid issue' } });
    }
    return this.reply(this.fixSuggestion);
  };

  handleCheckService = () => {
    if (this.serviceStatus) {
      return this.reply({ status: this.serviceStatus } as SuggestionServiceStatusCheckResponse);
    }
    return Promise.reject({ error: { msg: 'Error' } });
  };

  reply<T>(response: T): Promise<T> {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(cloneDeep(response));
      }, 10);
    });
  }

  setServiceStatus(status: MockSuggestionServiceStatus) {
    this.serviceStatus = status;
  }
}