addIntegrationRow.spec.tsx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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, waitFor} from 'sentry-test/reactTestingLibrary';
  5. import AddIntegrationRow from 'sentry/views/alerts/rules/issue/addIntegrationRow';
  6. jest.mock('sentry/actionCreators/modal');
  7. describe('AddIntegrationRow', function () {
  8. let project, org;
  9. const integrationSlug = 'github';
  10. const providers = [GitHubIntegrationProviderFixture()];
  11. beforeEach(function () {
  12. MockApiClient.clearMockResponses();
  13. project = ProjectFixture();
  14. org = OrganizationFixture();
  15. jest.clearAllMocks();
  16. });
  17. const getComponent = () => (
  18. <AddIntegrationRow
  19. providerKey={integrationSlug}
  20. organization={org}
  21. project={project}
  22. onClickHandler={jest.fn()}
  23. setHasError={jest.fn()}
  24. />
  25. );
  26. it('renders', async () => {
  27. MockApiClient.addMockResponse({
  28. url: `/organizations/${org.slug}/config/integrations/?provider_key=${integrationSlug}`,
  29. body: {
  30. providers: providers,
  31. },
  32. });
  33. render(getComponent());
  34. const button = await screen.findByRole('button', {name: /add integration/i});
  35. expect(button).toBeInTheDocument();
  36. });
  37. it('opens the setup dialog on click', async () => {
  38. const focus = jest.fn();
  39. const open = jest.fn().mockReturnValue({focus, close: jest.fn()});
  40. // any is needed here because getSentry has different types for global
  41. (global as any).open = open;
  42. const mock1 = MockApiClient.addMockResponse({
  43. url: `/organizations/${org.slug}/config/integrations/?provider_key=${integrationSlug}`,
  44. body: {
  45. providers: providers,
  46. },
  47. });
  48. render(getComponent());
  49. expect(mock1).toHaveBeenCalled();
  50. const button = await screen.findByRole('button', {name: /add integration/i});
  51. await userEvent.click(button);
  52. expect(open.mock.calls).toHaveLength(1);
  53. expect(focus.mock.calls).toHaveLength(1);
  54. expect(open.mock.calls[0][2]).toBe(
  55. 'scrollbars=yes,width=100,height=100,top=334,left=462'
  56. );
  57. });
  58. it('handles API error', async () => {
  59. const setHasError = jest.fn();
  60. MockApiClient.addMockResponse({
  61. url: `/organizations/${org.slug}/config/integrations/?provider_key=${integrationSlug}`,
  62. statusCode: 400,
  63. body: {error: 'internal error'},
  64. });
  65. render(
  66. <AddIntegrationRow
  67. providerKey={integrationSlug}
  68. organization={org}
  69. project={project}
  70. onClickHandler={jest.fn()}
  71. setHasError={setHasError}
  72. />
  73. );
  74. await waitFor(() => {
  75. expect(setHasError).toHaveBeenCalled();
  76. });
  77. });
  78. });