addIntegration.spec.tsx 2.6 KB

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