ruleNode.spec.jsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import selectEvent from 'react-select-event';
  2. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  3. import ModalStore from 'sentry/stores/modalStore';
  4. import RuleNode from 'sentry/views/alerts/rules/issue/ruleNode';
  5. describe('RuleNode', () => {
  6. const project = TestStubs.Project();
  7. const organization = TestStubs.Organization({projects: [project]});
  8. const index = 0;
  9. const onDelete = jest.fn();
  10. const onReset = jest.fn();
  11. const onPropertyChange = jest.fn();
  12. const simpleNode = {
  13. id: 'sentry.rules.simple_mock',
  14. label: '(mock) A new issue is created',
  15. enabled: true,
  16. };
  17. const formNode = label => ({
  18. label,
  19. id: 'sentry.rules.form_mock',
  20. enabled: true,
  21. formFields: {
  22. exampleStringField: {
  23. type: 'string',
  24. placeholder: 'placeholder',
  25. },
  26. exampleNumberField: {
  27. type: 'number',
  28. placeholder: 100,
  29. },
  30. exampleStringChoiceField: {
  31. type: 'choice',
  32. choices: [
  33. ['value1', 'label1'],
  34. ['value2', 'label2'],
  35. ['value3', 'label3'],
  36. ],
  37. },
  38. exampleResetStringChoiceField: {
  39. type: 'choice',
  40. choices: [
  41. ['value1', 'label1'],
  42. ['value2', 'label2'],
  43. ['value3', 'label3'],
  44. ],
  45. resetsForm: true,
  46. },
  47. exampleNumberChoiceField: {
  48. type: 'choice',
  49. initial: 2,
  50. choices: [
  51. [1, 'label1'],
  52. [2, 'label2'],
  53. [3, 'label3'],
  54. ],
  55. },
  56. exampleMailActionField: {
  57. type: 'mailAction',
  58. choices: [
  59. ['IssueOwners', 'Issue Owners'],
  60. ['Team', 'Team'],
  61. ['Member', 'Member'],
  62. ],
  63. },
  64. exampleAssigneeField: {
  65. type: 'assignee',
  66. choices: [
  67. ['Unassigned', 'Unassigned'],
  68. ['Team', 'Team'],
  69. ['Member', 'Member'],
  70. ],
  71. },
  72. },
  73. });
  74. // TODO: Add this node and test if it implements correctly (e.g. Jira Tickets)
  75. // const ticketNode = {actionType: 'ticket'};
  76. const sentryAppNode = {
  77. id: 'sentry.rules.schema_form_mock',
  78. label: 'Configure SentryApp with these',
  79. enabled: true,
  80. actionType: 'sentryapp',
  81. sentryAppInstallationUuid: '1027',
  82. formFields: {
  83. exampleStringField: {
  84. type: 'string',
  85. placeholder: 'placeholder',
  86. },
  87. exampleNumberField: {
  88. type: 'number',
  89. placeholder: 100,
  90. },
  91. exampleStringChoiceField: {
  92. type: 'choice',
  93. choices: [
  94. ['value1', 'label1'],
  95. ['value2', 'label2'],
  96. ['value3', 'label3'],
  97. ],
  98. },
  99. },
  100. };
  101. const renderRuleNode = (node, data) => {
  102. return render(
  103. <RuleNode
  104. index={index}
  105. node={node}
  106. data={{
  107. id: 'sentry.rules.mock',
  108. name: '(mock) A new issue is created',
  109. ...data,
  110. }}
  111. organization={organization}
  112. project={project}
  113. onDelete={onDelete}
  114. onPropertyChange={onPropertyChange}
  115. onReset={onReset}
  116. />
  117. );
  118. };
  119. const labelReplacer = (label, values) => {
  120. return label.replace(/{\w+}/gm, placeholder => values[placeholder]);
  121. };
  122. afterEach(() => {
  123. jest.resetAllMocks();
  124. });
  125. it('handles being deleted', () => {
  126. renderRuleNode(simpleNode);
  127. expect(screen.getByText(simpleNode.label)).toBeInTheDocument();
  128. expect(screen.getByRole('button', {name: 'Delete Node'})).toBeInTheDocument();
  129. userEvent.click(screen.getByRole('button', {name: 'Delete Node'}));
  130. expect(onDelete).toHaveBeenCalledWith(index);
  131. });
  132. it('renders choice string choice fields correctly', async () => {
  133. const fieldName = 'exampleStringChoiceField';
  134. const label = `Here is a string choice field {${fieldName}}`;
  135. renderRuleNode(formNode(label));
  136. // Should render the first option if no initial is provided
  137. expect(
  138. screen.getByText('Here is a string choice field').parentElement
  139. ).toHaveTextContent(labelReplacer(label, {[`{${fieldName}}`]: 'label1'}));
  140. await selectEvent.select(screen.getByText('label1'), 'label3');
  141. expect(onPropertyChange).toHaveBeenCalledWith(index, fieldName, 'value3');
  142. });
  143. it('resets choice fields with resetsForm', async () => {
  144. const fieldName = 'exampleResetStringChoiceField';
  145. const label = `Here is a string reset choice field {${fieldName}}`;
  146. renderRuleNode(formNode(label));
  147. // Should render the first option if no initial is provided
  148. expect(screen.getByText('Here is a string reset choice field')).toBeInTheDocument();
  149. await selectEvent.select(screen.getByText('label1'), 'label3');
  150. expect(onReset).toHaveBeenCalledWith(index, fieldName, 'value3');
  151. });
  152. it('renders choice number choice fields correctly', () => {
  153. const fieldName = 'exampleNumberChoiceField';
  154. const label = `Here is a number choice field {${fieldName}}`;
  155. renderRuleNode(formNode(label));
  156. // Should render the initial value if one is provided
  157. expect(
  158. screen.getByText('Here is a number choice field').parentElement
  159. ).toHaveTextContent(labelReplacer(label, {[`{${fieldName}}`]: 'label2'}));
  160. selectEvent.openMenu(screen.getByText('label2'));
  161. userEvent.click(screen.getByText('label3'));
  162. expect(onPropertyChange).toHaveBeenCalledWith(index, fieldName, '3');
  163. });
  164. it('renders number fields correctly', () => {
  165. const fieldName = 'exampleNumberField';
  166. const label = `Here is a number field {${fieldName}}`;
  167. renderRuleNode(formNode(label));
  168. expect(screen.getByText('Here is a number field')).toBeInTheDocument();
  169. expect(screen.getByPlaceholderText('100')).toBeInTheDocument();
  170. userEvent.type(screen.getByPlaceholderText('100'), '721');
  171. expect(onPropertyChange).toHaveBeenCalledWith(index, fieldName, '721');
  172. });
  173. it('sets some number fields by default', () => {
  174. const fieldName = 'exampleNumberField';
  175. const label = `Here is a number field {${fieldName}}`;
  176. renderRuleNode(formNode(label), {
  177. id: 'sentry.rules.filters.issue_occurrences.IssueOccurrencesFilter',
  178. });
  179. expect(onPropertyChange).toHaveBeenCalledTimes(1);
  180. expect(onPropertyChange).toHaveBeenCalledWith(index, fieldName, '100');
  181. });
  182. it('renders text fields correctly', () => {
  183. const fieldName = 'exampleStringField';
  184. const label = `Here is a text field {${fieldName}}`;
  185. renderRuleNode(formNode(label));
  186. expect(screen.getByText('Here is a text field')).toBeInTheDocument();
  187. const input = screen.getByPlaceholderText('placeholder');
  188. expect(input).toBeInTheDocument();
  189. userEvent.paste(input, 'some text');
  190. expect(onPropertyChange).toHaveBeenCalledWith(index, fieldName, 'some text');
  191. });
  192. it('renders sentry apps with schema forms correctly', () => {
  193. renderRuleNode(sentryAppNode);
  194. const openModal = jest.spyOn(ModalStore, 'openModal');
  195. expect(screen.getByText(sentryAppNode.label)).toBeInTheDocument();
  196. const settingsButton = screen.getByLabelText('Settings');
  197. expect(settingsButton).toBeInTheDocument();
  198. userEvent.click(settingsButton);
  199. expect(openModal).toHaveBeenCalled();
  200. });
  201. it('renders mail action field', async () => {
  202. const fieldName = 'exampleMailActionField';
  203. const label = `Send a notification to {${fieldName}}`;
  204. renderRuleNode(formNode(label), {targetType: 'IssueOwners'});
  205. expect(screen.getByText('Send a notification to')).toBeInTheDocument();
  206. await selectEvent.select(screen.getByText('Issue Owners'), 'Team');
  207. expect(onPropertyChange).toHaveBeenCalledTimes(2);
  208. expect(onPropertyChange).toHaveBeenCalledWith(index, 'targetType', 'Team');
  209. expect(onPropertyChange).toHaveBeenCalledWith(index, 'targetIdentifier', '');
  210. });
  211. it('renders assignee field', async () => {
  212. const fieldName = 'exampleAssigneeField';
  213. const label = `The issue is assigned to {${fieldName}}`;
  214. renderRuleNode(formNode(label), {targetType: 'Unassigned'});
  215. expect(screen.getByText('The issue is assigned to')).toBeInTheDocument();
  216. await selectEvent.select(screen.getByText('No One'), 'Team');
  217. expect(onPropertyChange).toHaveBeenCalledTimes(2);
  218. expect(onPropertyChange).toHaveBeenCalledWith(index, 'targetType', 'Team');
  219. expect(onPropertyChange).toHaveBeenCalledWith(index, 'targetIdentifier', '');
  220. });
  221. });