messagingIntegrationModal.spec.tsx 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import {GitHubIntegrationProviderFixture} from 'sentry-fixture/githubIntegrationProvider';
  2. import {OrganizationFixture} from 'sentry-fixture/organization';
  3. import {ProjectFixture} from 'sentry-fixture/project';
  4. import {render, screen, waitFor} from 'sentry-test/reactTestingLibrary';
  5. import * as indicators from 'sentry/actionCreators/indicator';
  6. import {
  7. makeClosableHeader,
  8. makeCloseButton,
  9. ModalBody,
  10. ModalFooter,
  11. } from 'sentry/components/globalModal/components';
  12. import MessagingIntegrationModal from 'sentry/views/alerts/rules/issue/messagingIntegrationModal';
  13. jest.mock('sentry/actionCreators/modal');
  14. describe('MessagingIntegrationModal', function () {
  15. let project, org;
  16. const providerKeys = ['slack', 'discord', 'msteams'];
  17. const providers = [GitHubIntegrationProviderFixture()];
  18. beforeEach(function () {
  19. MockApiClient.clearMockResponses();
  20. project = ProjectFixture();
  21. org = OrganizationFixture({
  22. features: ['messaging-integration-onboarding'],
  23. });
  24. jest.clearAllMocks();
  25. });
  26. const getComponent = (closeModal?, props = {}) => (
  27. <MessagingIntegrationModal
  28. Header={makeClosableHeader(() => {})}
  29. Body={ModalBody}
  30. headerContent={<h1>Connect with a messaging tool</h1>}
  31. bodyContent={<p>Receive alerts and digests right where you work.</p>}
  32. providerKeys={providerKeys}
  33. organization={org}
  34. project={project}
  35. CloseButton={makeCloseButton(() => {})}
  36. Footer={ModalFooter}
  37. closeModal={closeModal ? closeModal : jest.fn()}
  38. {...props}
  39. />
  40. );
  41. it('renders', async function () {
  42. const mockResponses: jest.Mock<any>[] = [];
  43. providerKeys.forEach(providerKey => {
  44. mockResponses.push(
  45. MockApiClient.addMockResponse({
  46. url: `/organizations/${org.slug}/config/integrations/?provider_key=${providerKey}`,
  47. body: {providers: providers},
  48. })
  49. );
  50. });
  51. render(getComponent());
  52. mockResponses.forEach(mock => {
  53. expect(mock).toHaveBeenCalled();
  54. });
  55. const heading = await screen.findByRole('heading', {
  56. name: /connect with a messaging tool/i,
  57. });
  58. expect(heading).toBeInTheDocument();
  59. const buttons = await screen.findAllByRole('button', {name: /add integration/i});
  60. expect(buttons).toHaveLength(providerKeys.length);
  61. });
  62. it('closes on error', async function () {
  63. const closeModal = jest.fn();
  64. jest.spyOn(indicators, 'addErrorMessage');
  65. const mockResponses: jest.Mock<any>[] = [];
  66. providerKeys.forEach(value => {
  67. mockResponses.push(
  68. MockApiClient.addMockResponse({
  69. url: `/organizations/${org.slug}/config/integrations/?provider_key=${value}`,
  70. statusCode: 400,
  71. body: {error: 'internal error'},
  72. })
  73. );
  74. });
  75. render(getComponent(closeModal));
  76. mockResponses.forEach(mock => {
  77. expect(mock).toHaveBeenCalled();
  78. });
  79. await waitFor(() => {
  80. expect(closeModal).toHaveBeenCalled();
  81. expect(indicators.addErrorMessage).toHaveBeenCalled();
  82. });
  83. });
  84. });