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.

IssuesNewStatusAndTransitionGuide-test.tsx 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 { act } from '@testing-library/react';
  21. import userEvent from '@testing-library/user-event';
  22. import React from 'react';
  23. import IssuesServiceMock from '../../../../api/mocks/IssuesServiceMock';
  24. import UsersServiceMock from '../../../../api/mocks/UsersServiceMock';
  25. import CurrentUserContextProvider from '../../../../app/components/current-user/CurrentUserContextProvider';
  26. import IssueTransitionComponent from '../../../../components/issue/components/IssueTransition';
  27. import { mockCurrentUser, mockIssue } from '../../../../helpers/testMocks';
  28. import { renderComponent } from '../../../../helpers/testReactTestingUtils';
  29. import { IssueTransition } from '../../../../types/issues';
  30. import { Issue } from '../../../../types/types';
  31. import { NoticeType } from '../../../../types/users';
  32. import { ui } from '../../test-utils';
  33. import IssueNewStatusAndTransitionGuide from '../IssueNewStatusAndTransitionGuide';
  34. const usersHandler = new UsersServiceMock();
  35. const issuesHandler = new IssuesServiceMock(usersHandler);
  36. beforeEach(() => {
  37. usersHandler.reset();
  38. issuesHandler.reset();
  39. });
  40. it('should display status guide', async () => {
  41. const user = userEvent.setup();
  42. renderIssueNewStatusGuide();
  43. expect(await ui.guidePopup.find()).toBeInTheDocument();
  44. expect(ui.guidePopup.get()).toHaveTextContent('guiding.issue_accept.1.title');
  45. await act(async () => {
  46. await user.click(ui.guidePopup.byRole('button', { name: 'next' }).get());
  47. });
  48. expect(ui.guidePopup.get()).toHaveTextContent('guiding.issue_accept.2.title');
  49. await act(async () => {
  50. await user.click(ui.guidePopup.byRole('button', { name: 'go_back' }).get());
  51. });
  52. expect(ui.guidePopup.get()).toHaveTextContent('guiding.issue_accept.1.title');
  53. await act(async () => {
  54. await user.click(ui.guidePopup.byRole('button', { name: 'next' }).get());
  55. });
  56. await act(async () => {
  57. await user.click(ui.guidePopup.byRole('button', { name: 'next' }).get());
  58. });
  59. expect(ui.guidePopup.get()).toHaveTextContent('guiding.issue_accept.3.title');
  60. expect(ui.guidePopup.byRole('button', { name: 'Next' }).query()).not.toBeInTheDocument();
  61. await act(async () => {
  62. await user.click(ui.guidePopup.byRole('button', { name: 'close' }).get());
  63. });
  64. expect(ui.guidePopup.query()).not.toBeInTheDocument();
  65. });
  66. it('should not show guide for those who dismissed it', () => {
  67. renderIssueNewStatusGuide(
  68. mockCurrentUser({
  69. isLoggedIn: true,
  70. dismissedNotices: {
  71. [NoticeType.ISSUE_GUIDE]: true,
  72. [NoticeType.ISSUE_NEW_STATUS_AND_TRANSITION_GUIDE]: true,
  73. },
  74. }),
  75. );
  76. expect(ui.guidePopup.query()).not.toBeInTheDocument();
  77. });
  78. it('should skip guide', async () => {
  79. const user = userEvent.setup();
  80. renderIssueNewStatusGuide();
  81. expect(await ui.guidePopup.find()).toBeInTheDocument();
  82. expect(ui.guidePopup.get()).toHaveTextContent('guiding.issue_accept.1.title');
  83. await user.click(ui.guidePopup.byRole('button', { name: 'skip' }).get());
  84. expect(ui.guidePopup.query()).not.toBeInTheDocument();
  85. });
  86. it('should not show guide if user is not logged in', () => {
  87. renderIssueNewStatusGuide(mockCurrentUser({ isLoggedIn: false }));
  88. expect(ui.guidePopup.query()).not.toBeInTheDocument();
  89. });
  90. it('should not show guide if there are no issues', () => {
  91. renderIssueNewStatusGuide(mockCurrentUser({ isLoggedIn: true }), []);
  92. expect(ui.guidePopup.query()).not.toBeInTheDocument();
  93. });
  94. it('should not show guide if CCT guide is shown', () => {
  95. renderIssueNewStatusGuide(
  96. mockCurrentUser({ isLoggedIn: true, dismissedNotices: { [NoticeType.ISSUE_GUIDE]: false } }),
  97. [],
  98. );
  99. expect(ui.guidePopup.query()).not.toBeInTheDocument();
  100. });
  101. function IssueNewStatusGuide({ issues }: { issues: Issue[] }) {
  102. const [open, setOpen] = React.useState(false);
  103. const issue = mockIssue(false, {
  104. transitions: [
  105. IssueTransition.Accept,
  106. IssueTransition.Confirm,
  107. IssueTransition.Resolve,
  108. IssueTransition.FalsePositive,
  109. IssueTransition.WontFix,
  110. ],
  111. });
  112. return (
  113. <div data-guiding-id={`issue-transition-${issue.key}`}>
  114. <div data-guiding-id="issue-accept-transition">/</div>
  115. <IssueTransitionComponent
  116. isOpen={open}
  117. togglePopup={() => setOpen(!open)}
  118. issue={issue}
  119. onChange={jest.fn()}
  120. />
  121. <IssueNewStatusAndTransitionGuide
  122. togglePopup={(_, __, show) => setOpen(Boolean(show))}
  123. run
  124. issues={issues}
  125. />
  126. </div>
  127. );
  128. }
  129. function renderIssueNewStatusGuide(
  130. currentUser = mockCurrentUser({
  131. isLoggedIn: true,
  132. dismissedNotices: { [NoticeType.ISSUE_GUIDE]: true },
  133. }),
  134. issues = [
  135. mockIssue(false, {
  136. transitions: [
  137. IssueTransition.Accept,
  138. IssueTransition.Confirm,
  139. IssueTransition.Resolve,
  140. IssueTransition.FalsePositive,
  141. IssueTransition.WontFix,
  142. ],
  143. }),
  144. ],
  145. ) {
  146. return renderComponent(
  147. <CurrentUserContextProvider currentUser={currentUser}>
  148. <IssueNewStatusGuide issues={issues} />
  149. </CurrentUserContextProvider>,
  150. );
  151. }