addIntegration.spec.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import {GitHubIntegration as GitHubIntegrationFixture} from 'sentry-fixture/githubIntegration';
  2. import {GitHubIntegrationProvider} from 'sentry-fixture/githubIntegrationProvider';
  3. import {Organization} from 'sentry-fixture/organization';
  4. import {render, waitFor} from 'sentry-test/reactTestingLibrary';
  5. import {Config} from 'sentry/types';
  6. import AddIntegration from 'sentry/views/settings/organizationIntegrations/addIntegration';
  7. describe('AddIntegration', function () {
  8. const provider = GitHubIntegrationProvider();
  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 organization={Organization()} provider={provider} onInstall={onAdd}>
  46. {openDialog => (
  47. <a href="#" onClick={() => openDialog()}>
  48. Click
  49. </a>
  50. )}
  51. </AddIntegration>
  52. );
  53. const newIntegration = {
  54. success: true,
  55. data: Object.assign({}, integration, {
  56. id: '2',
  57. domain_name: 'new-integration.github.com',
  58. icon: 'http://example.com/new-integration-icon.png',
  59. name: 'New Integration',
  60. }),
  61. };
  62. window.postMessage(newIntegration, '*');
  63. await waitFor(() => expect(onAdd).toHaveBeenCalledWith(newIntegration.data));
  64. });
  65. });