addIntegration.spec.jsx 1.9 KB

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