sentryAppRuleModal.spec.jsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import SentryAppRuleModal from 'sentry/views/alerts/rules/issue/sentryAppRuleModal';
  3. describe('SentryAppRuleModal', function () {
  4. const modalElements = {
  5. Header: p => p.children,
  6. Body: p => p.children,
  7. Footer: p => p.children,
  8. };
  9. let sentryApp;
  10. let sentryAppInstallation;
  11. beforeEach(function () {
  12. sentryApp = TestStubs.SentryApp();
  13. sentryAppInstallation = TestStubs.SentryAppInstallation({sentryApp});
  14. });
  15. function openSelectMenu(text) {
  16. const placeholder = screen.getByText(text);
  17. userEvent.type(placeholder, '{keyDown}');
  18. }
  19. const _submit = () => {
  20. userEvent.click(screen.getByText('Save Changes'));
  21. return screen.queryAllByText('Field is required');
  22. };
  23. const submitSuccess = () => {
  24. const errors = _submit();
  25. expect(errors).toHaveLength(0);
  26. };
  27. const submitErrors = errorCount => {
  28. const errors = _submit();
  29. expect(errors).toHaveLength(errorCount);
  30. };
  31. const defaultConfig = {
  32. uri: '/integration/test/',
  33. required_fields: [
  34. {
  35. type: 'text',
  36. label: 'Alert Title',
  37. name: 'title',
  38. },
  39. {
  40. type: 'textarea',
  41. label: 'Alert Description',
  42. name: 'description',
  43. },
  44. {
  45. type: 'select',
  46. label: 'Team Channel',
  47. name: 'channel',
  48. choices: [
  49. ['valor', 'valor'],
  50. ['mystic', 'mystic'],
  51. ['instinct', 'instinct'],
  52. ],
  53. },
  54. ],
  55. optional_fields: [
  56. {
  57. type: 'text',
  58. label: 'Extra Details',
  59. name: 'extra',
  60. },
  61. ],
  62. };
  63. const createWrapper = (props = {}) => {
  64. return render(
  65. <SentryAppRuleModal
  66. {...modalElements}
  67. sentryAppInstallationUuid={sentryAppInstallation.uuid}
  68. appName={sentryApp.name}
  69. config={defaultConfig}
  70. action="create"
  71. onSubmitSuccess={() => {}}
  72. {...props}
  73. />
  74. );
  75. };
  76. describe('Create UI Alert Rule', function () {
  77. it('should render the Alert Rule modal with the config fields', function () {
  78. createWrapper();
  79. const {required_fields, optional_fields} = defaultConfig;
  80. const allFields = [...required_fields, ...optional_fields];
  81. allFields.forEach(field => {
  82. expect(screen.getByText(field.label)).toBeInTheDocument();
  83. });
  84. });
  85. it('should raise validation errors when "Save Changes" is clicked with invalid data', function () {
  86. createWrapper();
  87. submitErrors(3);
  88. });
  89. it('should submit when "Save Changes" is clicked with valid data', function () {
  90. createWrapper();
  91. const titleInput = screen.getByTestId('title');
  92. const descriptionInput = screen.getByTestId('description');
  93. userEvent.type(titleInput, 'v');
  94. userEvent.type(descriptionInput, 'v');
  95. openSelectMenu('--');
  96. userEvent.click(screen.getByText('valor'));
  97. submitSuccess();
  98. });
  99. });
  100. });