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 {t} from 'sentry/locale';
  13. import MessagingIntegrationModal from 'sentry/views/alerts/rules/issue/messagingIntegrationModal';
  14. jest.mock('sentry/actionCreators/modal');
  15. describe('MessagingIntegrationModal', function () {
  16. let project, org;
  17. const providerKeys = ['slack', 'discord', 'msteams'];
  18. const providers = (providerKey: string) => [
  19. GitHubIntegrationProviderFixture({key: providerKey}),
  20. ];
  21. beforeEach(function () {
  22. MockApiClient.clearMockResponses();
  23. project = ProjectFixture();
  24. org = OrganizationFixture();
  25. jest.clearAllMocks();
  26. });
  27. const getComponent = (closeModal = jest.fn(), props = {}) => (
  28. <MessagingIntegrationModal
  29. closeModal={closeModal}
  30. Header={makeClosableHeader(() => {})}
  31. Body={ModalBody}
  32. headerContent={t('Connect with a messaging tool')}
  33. bodyContent={t('Receive alerts and digests right where you work.')}
  34. providerKeys={providerKeys}
  35. project={project}
  36. CloseButton={makeCloseButton(() => {})}
  37. Footer={ModalFooter}
  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(providerKey)},
  48. })
  49. );
  50. });
  51. render(getComponent(), {organization: org});
  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), {organization: org});
  76. mockResponses.forEach(mock => {
  77. expect(mock).toHaveBeenCalled();
  78. });
  79. await waitFor(() => {
  80. expect(closeModal).toHaveBeenCalled();
  81. expect(indicators.addErrorMessage).toHaveBeenCalled();
  82. });
  83. });
  84. });