sentryAppRuleModal.spec.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import styled from '@emotion/styled';
  2. import {SentryAppFixture} from 'sentry-fixture/sentryApp';
  3. import {SentryAppInstallationFixture} from 'sentry-fixture/sentryAppInstallation';
  4. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  5. import {makeCloseButton} from 'sentry/components/globalModal/components';
  6. import SentryAppRuleModal from 'sentry/views/alerts/rules/issue/sentryAppRuleModal';
  7. import type {
  8. FieldFromSchema,
  9. SchemaFormConfig,
  10. } from 'sentry/views/settings/organizationIntegrations/sentryAppExternalForm';
  11. describe('SentryAppRuleModal', function () {
  12. const modalElements = {
  13. Header: p => p.children,
  14. Body: p => p.children,
  15. Footer: p => p.children,
  16. };
  17. let sentryApp;
  18. let sentryAppInstallation;
  19. beforeEach(function () {
  20. sentryApp = SentryAppFixture();
  21. sentryAppInstallation = SentryAppInstallationFixture();
  22. });
  23. const _submit = async () => {
  24. await userEvent.click(screen.getByText('Save Changes'));
  25. return screen.queryAllByText('Field is required');
  26. };
  27. const submitSuccess = async () => {
  28. const errors = await _submit();
  29. expect(errors).toHaveLength(0);
  30. };
  31. const defaultConfig: SchemaFormConfig = {
  32. uri: '/integration/test/',
  33. description: '',
  34. required_fields: [
  35. {
  36. type: 'text',
  37. label: 'Alert Title',
  38. name: 'title',
  39. },
  40. {
  41. type: 'textarea',
  42. label: 'Alert Description',
  43. name: 'description',
  44. },
  45. {
  46. type: 'select',
  47. label: 'Team Channel',
  48. name: 'channel',
  49. choices: [
  50. ['valor', 'valor'],
  51. ['mystic', 'mystic'],
  52. ['instinct', 'instinct'],
  53. ],
  54. },
  55. ],
  56. optional_fields: [
  57. {
  58. type: 'text',
  59. label: 'Extra Details',
  60. name: 'extra',
  61. },
  62. {
  63. type: 'select',
  64. label: 'Assignee',
  65. name: 'assignee',
  66. uri: '/link/assignee/',
  67. },
  68. {
  69. type: 'select',
  70. label: 'Workspace',
  71. name: 'workspace',
  72. uri: '/link/workspace/',
  73. },
  74. ],
  75. };
  76. const resetValues = {
  77. settings: [
  78. {
  79. name: 'extra',
  80. value: 'saved details from last edit',
  81. },
  82. {
  83. name: 'assignee',
  84. value: 'edna-mode',
  85. label: 'Edna Mode',
  86. },
  87. ],
  88. };
  89. const createWrapper = (props = {}) => {
  90. const styledWrapper = styled(c => c.children);
  91. return render(
  92. <SentryAppRuleModal
  93. {...modalElements}
  94. sentryAppInstallationUuid={sentryAppInstallation.uuid}
  95. appName={sentryApp.name}
  96. config={defaultConfig}
  97. onSubmitSuccess={() => {}}
  98. resetValues={resetValues}
  99. closeModal={jest.fn()}
  100. CloseButton={makeCloseButton(() => {})}
  101. Body={styledWrapper()}
  102. Footer={styledWrapper()}
  103. {...props}
  104. />
  105. );
  106. };
  107. describe('Create UI Alert Rule', function () {
  108. it('should render the Alert Rule modal with the config fields', function () {
  109. createWrapper();
  110. const {required_fields, optional_fields} = defaultConfig;
  111. const allFields = [...required_fields!, ...optional_fields!];
  112. allFields.forEach((field: FieldFromSchema) => {
  113. if (typeof field.label === 'string') {
  114. expect(screen.getByText(field.label)).toBeInTheDocument();
  115. }
  116. });
  117. });
  118. it('submit button shall be disabled if form is incomplete', async function () {
  119. createWrapper();
  120. expect(screen.getByRole('button', {name: 'Save Changes'})).toBeDisabled();
  121. await userEvent.hover(screen.getByRole('button', {name: 'Save Changes'}));
  122. expect(
  123. await screen.findByText('Required fields must be filled out')
  124. ).toBeInTheDocument();
  125. });
  126. it('should submit when "Save Changes" is clicked with valid data', async function () {
  127. createWrapper();
  128. const titleInput = screen.getByTestId('title');
  129. await userEvent.type(titleInput, 'some title');
  130. const descriptionInput = screen.getByTestId('description');
  131. await userEvent.type(descriptionInput, 'some description');
  132. const channelInput = screen.getAllByText('Type to search')[0];
  133. await userEvent.type(channelInput, '{keyDown}');
  134. await userEvent.click(screen.getByText('valor'));
  135. // Ensure text fields are persisted on edit
  136. const savedExtraDetailsInput = screen.getByDisplayValue(
  137. resetValues.settings[0].value
  138. );
  139. expect(savedExtraDetailsInput).toBeInTheDocument();
  140. // Ensure select fields are persisted with labels on edit
  141. const savedAssigneeInput = screen.getByText(resetValues.settings[1].label!);
  142. expect(savedAssigneeInput).toBeInTheDocument();
  143. // Ensure async select fields filter correctly
  144. const workspaceChoices = [
  145. ['WS0', 'Primary Workspace'],
  146. ['WS1', 'Secondary Workspace'],
  147. ];
  148. const workspaceResponse = MockApiClient.addMockResponse({
  149. url: `/sentry-app-installations/${sentryAppInstallation.uuid}/external-requests/`,
  150. body: {choices: workspaceChoices},
  151. });
  152. const workspaceInput = screen.getByText('Type to search');
  153. // Search by value
  154. await userEvent.type(workspaceInput, workspaceChoices[1][0]);
  155. await waitFor(() => expect(workspaceResponse).toHaveBeenCalled());
  156. // Select by label
  157. await userEvent.click(screen.getByText(workspaceChoices[1][1]));
  158. await submitSuccess();
  159. });
  160. it('should load complexity options from backend when column has a default value', async function () {
  161. const mockApi = MockApiClient.addMockResponse({
  162. url: `/sentry-app-installations/${sentryAppInstallation.uuid}/external-requests/`,
  163. body: {
  164. choices: [
  165. ['low', 'Low'],
  166. ['medium', 'Medium'],
  167. ['high', 'High'],
  168. ],
  169. },
  170. });
  171. const schema: SchemaFormConfig = {
  172. uri: '/api/sentry/issue-link/create/',
  173. required_fields: [
  174. {
  175. type: 'text',
  176. label: 'Task Name',
  177. name: 'title',
  178. default: 'issue.title',
  179. },
  180. {
  181. type: 'select',
  182. label: "What's the status of this task?",
  183. name: 'column',
  184. uri: '/api/sentry/options/status/',
  185. defaultValue: 'ongoing',
  186. choices: [
  187. ['ongoing', 'ongoing'],
  188. ['completed', 'completed'],
  189. ['pending', 'pending'],
  190. ['cancelled', 'cancelled'],
  191. ],
  192. },
  193. ],
  194. optional_fields: [
  195. {
  196. type: 'select',
  197. label: 'What is the estimated complexity?',
  198. name: 'complexity',
  199. depends_on: ['column'],
  200. skip_load_on_open: true,
  201. uri: '/api/sentry/options/complexity-options/',
  202. choices: [],
  203. },
  204. ],
  205. };
  206. createWrapper({config: schema});
  207. // Wait for component to mount and state to update
  208. await waitFor(() => expect(mockApi).toHaveBeenCalled());
  209. // Check if complexity options are loaded
  210. const complexityInput = screen.getByLabelText('What is the estimated complexity?', {
  211. selector: 'input#complexity',
  212. });
  213. await userEvent.click(complexityInput);
  214. expect(screen.getByText('Low')).toBeInTheDocument();
  215. expect(screen.getByText('Medium')).toBeInTheDocument();
  216. expect(screen.getByText('High')).toBeInTheDocument();
  217. });
  218. });
  219. });