addIntegration.spec.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import {GitHubIntegrationFixture} from 'sentry-fixture/githubIntegration';
  2. import {GitHubIntegrationProviderFixture} from 'sentry-fixture/githubIntegrationProvider';
  3. import {OrganizationFixture} from 'sentry-fixture/organization';
  4. import {render, waitFor} from 'sentry-test/reactTestingLibrary';
  5. import type {Config} from 'sentry/types/system';
  6. import AddIntegration from 'sentry/views/settings/organizationIntegrations/addIntegration';
  7. describe('AddIntegration', function () {
  8. const provider = GitHubIntegrationProviderFixture();
  9. const integration = GitHubIntegrationFixture();
  10. function interceptMessageEvent(event: MessageEvent) {
  11. if (event.origin === '') {
  12. event.stopImmediatePropagation();
  13. const eventWithOrigin = new MessageEvent('message', {
  14. data: event.data,
  15. origin: 'https://foobar.sentry.io',
  16. });
  17. window.dispatchEvent(eventWithOrigin);
  18. }
  19. }
  20. beforeEach(function () {
  21. window.__initialData = {
  22. customerDomain: {
  23. subdomain: 'foobar',
  24. organizationUrl: 'https://foobar.sentry.io',
  25. sentryUrl: 'https://sentry.io',
  26. },
  27. links: {
  28. organizationUrl: 'https://foobar.sentry.io',
  29. regionUrl: 'https://us.sentry.io',
  30. sentryUrl: 'https://sentry.io',
  31. },
  32. } as Config;
  33. window.location.assign('https://foobar.sentry.io');
  34. window.addEventListener('message', interceptMessageEvent);
  35. });
  36. afterEach(function () {
  37. window.removeEventListener('message', interceptMessageEvent);
  38. });
  39. it('Adds an integration on dialog completion', async function () {
  40. const onAdd = jest.fn();
  41. const focus = jest.fn();
  42. const open = jest.fn().mockReturnValue({focus});
  43. global.open = open;
  44. render(
  45. <AddIntegration
  46. organization={OrganizationFixture()}
  47. provider={provider}
  48. onInstall={onAdd}
  49. >
  50. {openDialog => (
  51. <a href="#" onClick={() => openDialog()}>
  52. Click
  53. </a>
  54. )}
  55. </AddIntegration>
  56. );
  57. const newIntegration = {
  58. success: true,
  59. data: Object.assign({}, integration, {
  60. id: '2',
  61. domain_name: 'new-integration.github.com',
  62. icon: 'http://example.com/new-integration-icon.png',
  63. name: 'New Integration',
  64. }),
  65. };
  66. window.postMessage(newIntegration, '*');
  67. await waitFor(() => expect(onAdd).toHaveBeenCalledWith(newIntegration.data));
  68. });
  69. });