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.

MessagesServiceMock.ts 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 { cloneDeep } from 'lodash';
  21. import {
  22. checkMessageDismissed,
  23. MessageDismissParams,
  24. MessageTypes,
  25. setMessageDismissed,
  26. } from '../messages';
  27. jest.mock('../messages');
  28. interface Dismissed {
  29. dismissed: boolean;
  30. }
  31. interface ProjectDismissed {
  32. [projectKey: string]: Dismissed;
  33. }
  34. export default class MessagesServiceMock {
  35. #messageResponse: {
  36. [key in MessageTypes]?: ProjectDismissed | Dismissed;
  37. };
  38. constructor() {
  39. this.#messageResponse = {};
  40. jest.mocked(checkMessageDismissed).mockImplementation(this.handleCheckMessageDismissed);
  41. jest.mocked(setMessageDismissed).mockImplementation(this.handleSetMessageDismissed);
  42. }
  43. handleCheckMessageDismissed = (data: MessageDismissParams) => {
  44. const result = this.getMessageDismissed(data);
  45. return this.reply(result as Dismissed);
  46. };
  47. handleSetMessageDismissed = (data: MessageDismissParams) => {
  48. this.setMessageDismissed(data);
  49. return Promise.resolve();
  50. };
  51. setMessageDismissed = ({ projectKey, messageType }: MessageDismissParams) => {
  52. if (projectKey) {
  53. this.#messageResponse[messageType] ||= {
  54. ...this.#messageResponse[messageType],
  55. [projectKey]: {
  56. dismissed: true,
  57. },
  58. };
  59. } else {
  60. this.#messageResponse[messageType] = {
  61. ...this.#messageResponse[messageType],
  62. dismissed: true,
  63. };
  64. }
  65. };
  66. getMessageDismissed = ({ projectKey, messageType }: MessageDismissParams) => {
  67. const dismissed = projectKey
  68. ? (this.#messageResponse[messageType] as ProjectDismissed)?.[projectKey]
  69. : this.#messageResponse[messageType];
  70. return dismissed || { dismissed: false };
  71. };
  72. reply<T>(response: T): Promise<T> {
  73. return Promise.resolve(cloneDeep(response));
  74. }
  75. reset = () => {
  76. this.#messageResponse = {};
  77. };
  78. }