setupMessagingIntegrationButton.spec.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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, userEvent} from 'sentry-test/reactTestingLibrary';
  5. import {openModal} from 'sentry/actionCreators/modal';
  6. import HookStore from 'sentry/stores/hookStore';
  7. import SetupMessagingIntegrationButton, {
  8. MessagingIntegrationAnalyticsView,
  9. } from 'sentry/views/alerts/rules/issue/setupMessagingIntegrationButton';
  10. jest.mock('sentry/actionCreators/modal');
  11. describe('SetupAlertIntegrationButton', function () {
  12. const organization = OrganizationFixture({
  13. features: ['messaging-integration-onboarding'],
  14. });
  15. const project = ProjectFixture();
  16. const providers = (providerKey: string) => [
  17. GitHubIntegrationProviderFixture({key: providerKey}),
  18. ];
  19. const providerKey = 'slack';
  20. const getComponent = () => (
  21. <SetupMessagingIntegrationButton
  22. projectSlug={project.slug}
  23. refetchConfigs={jest.fn()}
  24. analyticsParams={{view: MessagingIntegrationAnalyticsView.ALERT_RULE_CREATION}}
  25. />
  26. );
  27. beforeEach(function () {
  28. MockApiClient.clearMockResponses();
  29. MockApiClient.addMockResponse({
  30. url: `/organizations/${organization.slug}/config/integrations/?provider_key=${providerKey}`,
  31. body: {providers: providers(providerKey)},
  32. });
  33. jest.clearAllMocks();
  34. });
  35. it('renders when no integration is installed', async function () {
  36. MockApiClient.addMockResponse({
  37. url: `/projects/${organization.slug}/${project.slug}/`,
  38. body: {
  39. ...project,
  40. hasAlertIntegrationInstalled: false,
  41. },
  42. });
  43. render(getComponent(), {organization: organization});
  44. await screen.findByRole('button', {name: /connect to messaging/i});
  45. });
  46. it('does not render button if alert integration installed', function () {
  47. MockApiClient.addMockResponse({
  48. url: `/projects/${organization.slug}/${project.slug}/`,
  49. body: {
  50. ...project,
  51. hasAlertIntegrationInstalled: true,
  52. },
  53. });
  54. render(getComponent(), {organization: organization});
  55. expect(screen.queryByRole('button')).not.toBeInTheDocument();
  56. });
  57. it('opens modal when clicked', async function () {
  58. MockApiClient.addMockResponse({
  59. url: `/projects/${organization.slug}/${project.slug}/`,
  60. body: {
  61. ...project,
  62. hasAlertIntegrationInstalled: false,
  63. },
  64. });
  65. render(getComponent(), {organization: organization});
  66. const button = await screen.findByRole('button', {name: /connect to messaging/i});
  67. await userEvent.click(button);
  68. expect(openModal).toHaveBeenCalled();
  69. });
  70. it('does not render button if project is loading', function () {
  71. MockApiClient.addMockResponse({
  72. url: `/projects/${organization.slug}/${project.slug}/`,
  73. statusCode: 400,
  74. body: {error: 'internal error'},
  75. });
  76. render(getComponent(), {organization: organization});
  77. expect(screen.queryByRole('button')).not.toBeInTheDocument();
  78. });
  79. it('disables button if user does not have integration feature', async function () {
  80. MockApiClient.addMockResponse({
  81. url: `/projects/${organization.slug}/${project.slug}/`,
  82. body: {
  83. ...project,
  84. hasAlertIntegrationInstalled: false,
  85. },
  86. });
  87. HookStore.add('integrations:feature-gates', () => {
  88. return {
  89. IntegrationFeatures: p =>
  90. p.children({
  91. disabled: true,
  92. disabledReason: 'some reason',
  93. ungatedFeatures: p.features,
  94. gatedFeatureGroups: [],
  95. }),
  96. FeatureList: p => (p.provider = providers[0]),
  97. };
  98. });
  99. render(getComponent(), {organization: organization});
  100. await screen.findByRole('button', {name: /connect to messaging/i});
  101. expect(screen.getByRole('button')).toBeDisabled();
  102. });
  103. });