addIntegration.spec.tsx 2.1 KB

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