addIntegration.spec.tsx 2.2 KB

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