ticketRuleModal.spec.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import styled from '@emotion/styled';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  4. import selectEvent from 'sentry-test/selectEvent';
  5. import {addSuccessMessage} from 'sentry/actionCreators/indicator';
  6. import {makeCloseButton} from 'sentry/components/globalModal/components';
  7. import type {IssueConfigField} from 'sentry/types';
  8. import type {IssueAlertRuleAction} from 'sentry/types/alerts';
  9. import TicketRuleModal from 'sentry/views/alerts/rules/issue/ticketRuleModal';
  10. jest.unmock('sentry/utils/recreateRoute');
  11. jest.mock('sentry/actionCreators/indicator');
  12. jest.mock('sentry/actionCreators/onboardingTasks');
  13. describe('ProjectAlerts -> TicketRuleModal', function () {
  14. const closeModal = jest.fn();
  15. const modalElements = {
  16. Header: p => p.children,
  17. Body: p => p.children,
  18. Footer: p => p.children,
  19. };
  20. afterEach(function () {
  21. closeModal.mockReset();
  22. MockApiClient.clearMockResponses();
  23. });
  24. const doSubmit = async () =>
  25. await userEvent.click(screen.getByRole('button', {name: 'Apply Changes'}));
  26. const submitSuccess = async () => {
  27. await doSubmit();
  28. expect(addSuccessMessage).toHaveBeenCalled();
  29. expect(closeModal).toHaveBeenCalled();
  30. };
  31. const submitErrors = async errorCount => {
  32. await doSubmit();
  33. expect(screen.getAllByText('Field is required')).toHaveLength(errorCount);
  34. expect(closeModal).toHaveBeenCalledTimes(0);
  35. };
  36. const addMockConfigsAPICall = (otherField = {}) => {
  37. return MockApiClient.addMockResponse({
  38. url: '/organizations/org-slug/integrations/1/?ignored=Sprint',
  39. method: 'GET',
  40. body: {
  41. createIssueConfig: [
  42. {
  43. name: 'project',
  44. label: 'Jira Project',
  45. choices: [['10000', 'TEST']],
  46. default: '10000',
  47. type: 'select',
  48. updatesForm: true,
  49. },
  50. {
  51. name: 'issuetype',
  52. label: 'Issue Type',
  53. default: '10001',
  54. type: 'select',
  55. choices: [
  56. ['10001', 'Improvement'],
  57. ['10002', 'Task'],
  58. ['10003', 'Sub-task'],
  59. ['10004', 'New Feature'],
  60. ['10005', 'Bug'],
  61. ['10000', 'Epic'],
  62. ],
  63. updatesForm: true,
  64. required: true,
  65. },
  66. otherField,
  67. ],
  68. },
  69. });
  70. };
  71. const renderComponent = (
  72. props: Partial<IssueAlertRuleAction> = {},
  73. otherField: IssueConfigField = {
  74. label: 'Reporter',
  75. required: true,
  76. choices: [['a', 'a']],
  77. type: 'select',
  78. name: 'reporter',
  79. }
  80. ) => {
  81. const {organization, router} = initializeOrg();
  82. addMockConfigsAPICall(otherField);
  83. const body = styled(c => c.children);
  84. return render(
  85. <TicketRuleModal
  86. {...modalElements}
  87. CloseButton={makeCloseButton(() => {})}
  88. closeModal={closeModal}
  89. Body={body()}
  90. Footer={body()}
  91. formFields={{}}
  92. link=""
  93. ticketType=""
  94. instance={{...(props.data || {}), integration: 1}}
  95. index={0}
  96. onSubmitAction={() => {}}
  97. organization={organization}
  98. />,
  99. {router}
  100. );
  101. };
  102. describe('Create Rule', function () {
  103. it('should render the Ticket Rule modal', function () {
  104. renderComponent();
  105. expect(screen.getByRole('button', {name: 'Apply Changes'})).toBeInTheDocument();
  106. expect(screen.getByRole('textbox', {name: 'Title'})).toBeInTheDocument();
  107. expect(screen.getByRole('textbox', {name: 'Description'})).toBeInTheDocument();
  108. });
  109. it('should save the modal data when "Apply Changes" is clicked with valid data', async function () {
  110. renderComponent();
  111. await selectEvent.select(screen.getByRole('textbox', {name: 'Reporter'}), 'a');
  112. await submitSuccess();
  113. });
  114. it('should raise validation errors when "Apply Changes" is clicked with invalid data', async function () {
  115. // This doesn't test anything TicketRules specific but I'm leaving it here as an example.
  116. renderComponent();
  117. await submitErrors(1);
  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 get async options from URL', async function () {
  192. renderComponent();
  193. addMockConfigsAPICall({
  194. label: 'Assignee',
  195. required: true,
  196. url: 'http://example.com',
  197. type: 'select',
  198. name: 'assignee',
  199. });
  200. await selectEvent.select(screen.getByRole('textbox', {name: 'Issue Type'}), 'Epic');
  201. // Component makes 1 request per character typed.
  202. let txt = '';
  203. for (const char of 'Joe') {
  204. txt += char;
  205. MockApiClient.addMockResponse({
  206. url: `http://example.com?field=assignee&issuetype=10001&project=10000&query=${txt}`,
  207. method: 'GET',
  208. body: [{label: 'Joe', value: 'Joe'}],
  209. });
  210. }
  211. const menu = screen.getByRole('textbox', {name: 'Assignee'});
  212. await selectEvent.openMenu(menu);
  213. await userEvent.type(menu, 'Joe{Escape}');
  214. await selectEvent.select(menu, 'Joe');
  215. await submitSuccess();
  216. });
  217. });
  218. });