ticketRuleModal.spec.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import type {PropsWithChildren, ReactElement} from 'react';
  2. import styled from '@emotion/styled';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  5. import selectEvent from 'sentry-test/selectEvent';
  6. import {addSuccessMessage} from 'sentry/actionCreators/indicator';
  7. import {makeCloseButton} from 'sentry/components/globalModal/components';
  8. import type {IssueAlertRuleAction} from 'sentry/types/alerts';
  9. import type {IssueConfigField} from 'sentry/types/integrations';
  10. import TicketRuleModal from 'sentry/views/alerts/rules/issue/ticketRuleModal';
  11. jest.unmock('sentry/utils/recreateRoute');
  12. jest.mock('sentry/actionCreators/indicator');
  13. jest.mock('sentry/actionCreators/onboardingTasks');
  14. describe('ProjectAlerts -> TicketRuleModal', function () {
  15. const closeModal = jest.fn();
  16. const modalElements = {
  17. Header: (p: PropsWithChildren) => p.children as ReactElement,
  18. Body: (p: PropsWithChildren) => p.children,
  19. Footer: (p: PropsWithChildren) => p.children,
  20. };
  21. afterEach(function () {
  22. closeModal.mockReset();
  23. MockApiClient.clearMockResponses();
  24. });
  25. const doSubmit = async () =>
  26. await userEvent.click(screen.getByRole('button', {name: 'Apply Changes'}));
  27. const submitSuccess = async () => {
  28. await doSubmit();
  29. expect(addSuccessMessage).toHaveBeenCalled();
  30. expect(closeModal).toHaveBeenCalled();
  31. };
  32. const addMockConfigsAPICall = (otherField = {}) => {
  33. return MockApiClient.addMockResponse({
  34. url: '/organizations/org-slug/integrations/1/?ignored=Sprint',
  35. method: 'GET',
  36. body: {
  37. createIssueConfig: [
  38. {
  39. name: 'project',
  40. label: 'Jira Project',
  41. choices: [['10000', 'TEST']],
  42. default: '10000',
  43. type: 'select',
  44. updatesForm: true,
  45. },
  46. {
  47. name: 'issuetype',
  48. label: 'Issue Type',
  49. default: '10001',
  50. type: 'select',
  51. choices: [
  52. ['10001', 'Improvement'],
  53. ['10002', 'Task'],
  54. ['10003', 'Sub-task'],
  55. ['10004', 'New Feature'],
  56. ['10005', 'Bug'],
  57. ['10000', 'Epic'],
  58. ],
  59. updatesForm: true,
  60. required: true,
  61. },
  62. otherField,
  63. ],
  64. },
  65. });
  66. };
  67. const renderComponent = (
  68. props: Partial<IssueAlertRuleAction> = {},
  69. otherField: IssueConfigField = {
  70. label: 'Reporter',
  71. required: true,
  72. choices: [['a', 'a']],
  73. type: 'select',
  74. name: 'reporter',
  75. }
  76. ) => {
  77. const {organization, router} = initializeOrg();
  78. addMockConfigsAPICall(otherField);
  79. const body = styled((c: PropsWithChildren) => c.children);
  80. return render(
  81. <TicketRuleModal
  82. {...modalElements}
  83. CloseButton={makeCloseButton(() => {})}
  84. closeModal={closeModal}
  85. Body={body()}
  86. Footer={body()}
  87. formFields={{}}
  88. link=""
  89. ticketType=""
  90. instance={{...(props.data || {}), integration: 1}}
  91. index={0}
  92. onSubmitAction={() => {}}
  93. organization={organization}
  94. />,
  95. {router}
  96. );
  97. };
  98. describe('Create Rule', function () {
  99. it('should render the Ticket Rule modal', function () {
  100. renderComponent();
  101. expect(screen.getByRole('button', {name: 'Apply Changes'})).toBeInTheDocument();
  102. expect(screen.getByRole('textbox', {name: 'Title'})).toBeInTheDocument();
  103. expect(screen.getByRole('textbox', {name: 'Description'})).toBeInTheDocument();
  104. });
  105. it('should save the modal data when "Apply Changes" is clicked with valid data', async function () {
  106. renderComponent();
  107. await selectEvent.select(screen.getByRole('textbox', {name: 'Reporter'}), 'a');
  108. await submitSuccess();
  109. });
  110. it('submit button shall be disabled if form is incomplete', async function () {
  111. // This doesn't test anything TicketRules specific but I'm leaving it here as an example.
  112. renderComponent();
  113. expect(screen.getByRole('button', {name: 'Apply Changes'})).toBeDisabled();
  114. await userEvent.hover(screen.getByRole('button', {name: 'Apply Changes'}));
  115. expect(
  116. await screen.findByText('Required fields must be filled out')
  117. ).toBeInTheDocument();
  118. });
  119. it('should reload fields when an "updatesForm" field changes', async function () {
  120. renderComponent();
  121. await selectEvent.select(screen.getByRole('textbox', {name: 'Reporter'}), 'a');
  122. addMockConfigsAPICall({
  123. label: 'Assignee',
  124. required: true,
  125. choices: [['b', 'b']],
  126. type: 'select',
  127. name: 'assignee',
  128. });
  129. await selectEvent.select(screen.getByRole('textbox', {name: 'Issue Type'}), 'Epic');
  130. await selectEvent.select(screen.getByRole('textbox', {name: 'Assignee'}), 'b');
  131. await submitSuccess();
  132. });
  133. it('should ignore error checking when default is empty array', async function () {
  134. renderComponent(undefined, {
  135. label: 'Labels',
  136. required: false,
  137. choices: [['bug', `bug`]],
  138. default: undefined,
  139. type: 'select',
  140. multiple: true,
  141. name: 'labels',
  142. });
  143. expect(
  144. screen.queryAllByText(`Could not fetch saved option for Labels. Please reselect.`)
  145. ).toHaveLength(0);
  146. await selectEvent.select(screen.getByRole('textbox', {name: 'Issue Type'}), 'Epic');
  147. await selectEvent.select(screen.getByRole('textbox', {name: 'Labels'}), 'bug');
  148. await submitSuccess();
  149. });
  150. it('should persist single select values when the modal is reopened', async function () {
  151. renderComponent({data: {reporter: 'a'}});
  152. await submitSuccess();
  153. });
  154. it('should persist multi select values when the modal is reopened', async function () {
  155. renderComponent(
  156. {data: {components: ['a', 'c']}},
  157. {
  158. name: 'components',
  159. label: 'Components',
  160. default: undefined,
  161. type: 'select',
  162. multiple: true,
  163. required: true,
  164. choices: [
  165. ['a', 'a'],
  166. ['b', 'b'],
  167. ['c', 'c'],
  168. ],
  169. }
  170. );
  171. await submitSuccess();
  172. });
  173. it('should not persist value when unavailable in new choices', async function () {
  174. renderComponent({data: {reporter: 'a'}});
  175. addMockConfigsAPICall({
  176. label: 'Reporter',
  177. required: true,
  178. choices: [['b', 'b']],
  179. type: 'select',
  180. name: 'reporter',
  181. ignorePriorChoices: true,
  182. });
  183. // Switch Issue Type so we refetch the config and update Reporter choices
  184. await selectEvent.select(screen.getByRole('textbox', {name: 'Issue Type'}), 'Epic');
  185. await expect(
  186. selectEvent.select(screen.getByRole('textbox', {name: 'Reporter'}), 'a')
  187. ).rejects.toThrow();
  188. await selectEvent.select(screen.getByRole('textbox', {name: 'Reporter'}), 'b');
  189. await submitSuccess();
  190. });
  191. it('should persist non-choice value when the modal is reopened', async function () {
  192. const textField: IssueConfigField = {
  193. label: 'Text Field',
  194. required: true,
  195. type: 'string',
  196. name: 'textField',
  197. };
  198. renderComponent({data: {textField: 'foo'}}, textField);
  199. expect(screen.getByRole('textbox', {name: 'Text Field'})).toHaveValue('foo');
  200. await submitSuccess();
  201. });
  202. it('should get async options from URL', async function () {
  203. renderComponent();
  204. addMockConfigsAPICall({
  205. label: 'Assignee',
  206. required: true,
  207. url: 'http://example.com',
  208. type: 'select',
  209. name: 'assignee',
  210. });
  211. await selectEvent.select(screen.getByRole('textbox', {name: 'Issue Type'}), 'Epic');
  212. // Component makes 1 request per character typed.
  213. let txt = '';
  214. for (const char of 'Joe') {
  215. txt += char;
  216. MockApiClient.addMockResponse({
  217. url: `http://example.com?field=assignee&issuetype=10001&project=10000&query=${txt}`,
  218. method: 'GET',
  219. body: [{label: 'Joe', value: 'Joe'}],
  220. });
  221. }
  222. const menu = screen.getByRole('textbox', {name: 'Assignee'});
  223. await selectEvent.openMenu(menu);
  224. await userEvent.type(menu, 'Joe{Escape}');
  225. await selectEvent.select(menu, 'Joe');
  226. await submitSuccess();
  227. });
  228. });
  229. });