index.spec.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import pick from 'lodash/pick';
  2. import {ConfigFixture} from 'sentry-fixture/config';
  3. import {OrganizationFixture} from 'sentry-fixture/organization';
  4. import {VercelProviderFixture} from 'sentry-fixture/vercelIntegration';
  5. import {initializeOrg} from 'sentry-test/initializeOrg';
  6. import {render, screen} from 'sentry-test/reactTestingLibrary';
  7. import selectEvent from 'sentry-test/selectEvent';
  8. import ConfigStore from 'sentry/stores/configStore';
  9. import type {Organization} from 'sentry/types/organization';
  10. import {generateOrgSlugUrl} from 'sentry/utils';
  11. import IntegrationOrganizationLink from 'sentry/views/integrationOrganizationLink';
  12. describe('IntegrationOrganizationLink', () => {
  13. let org1: Organization;
  14. let org2: Organization;
  15. let getOrgsMock: jest.Mock;
  16. beforeEach(() => {
  17. MockApiClient.clearMockResponses();
  18. window.location.assign = jest.fn();
  19. org1 = OrganizationFixture({
  20. slug: 'org1',
  21. name: 'Organization 1',
  22. });
  23. org2 = OrganizationFixture({
  24. slug: 'org2',
  25. name: 'Organization 2',
  26. });
  27. const org1Lite = pick(org1, ['slug', 'name', 'id']);
  28. const org2Lite = pick(org2, ['slug', 'name', 'id']);
  29. getOrgsMock = MockApiClient.addMockResponse({
  30. url: '/organizations/?include_feature_flags=1',
  31. body: [org1Lite, org2Lite],
  32. });
  33. });
  34. it('selecting org changes the url', async () => {
  35. const preselectedOrg = OrganizationFixture();
  36. const {routerProps} = initializeOrg({organization: preselectedOrg});
  37. window.__initialData = ConfigFixture({
  38. customerDomain: {
  39. subdomain: 'foobar',
  40. organizationUrl: 'https://foobar.sentry.io',
  41. sentryUrl: 'https://sentry.io',
  42. },
  43. links: {
  44. ...(window.__initialData?.links ?? {}),
  45. sentryUrl: 'https://sentry.io',
  46. },
  47. });
  48. ConfigStore.loadInitialData(window.__initialData);
  49. const getOrgMock = MockApiClient.addMockResponse({
  50. url: `/organizations/foobar/`,
  51. body: preselectedOrg,
  52. });
  53. MockApiClient.addMockResponse({
  54. url: `/organizations/foobar/config/integrations/?provider_key=vercel`,
  55. body: {providers: [VercelProviderFixture()]},
  56. });
  57. render(
  58. <IntegrationOrganizationLink
  59. {...routerProps}
  60. params={{integrationSlug: 'vercel'}}
  61. />
  62. );
  63. expect(getOrgsMock).toHaveBeenCalled();
  64. expect(getOrgMock).toHaveBeenCalled();
  65. // Select organization
  66. await selectEvent.select(screen.getByRole('textbox'), org2.name);
  67. expect(window.location.assign).toHaveBeenCalledWith(generateOrgSlugUrl(org2.slug));
  68. });
  69. it('Selecting the same org as the domain allows you to install', async () => {
  70. const initialData = initializeOrg({organization: org2});
  71. window.__initialData = ConfigFixture({
  72. customerDomain: {
  73. subdomain: org2.slug,
  74. organizationUrl: `https://${org2.slug}.sentry.io`,
  75. sentryUrl: 'https://sentry.io',
  76. },
  77. links: {
  78. ...(window.__initialData?.links ?? {}),
  79. sentryUrl: 'https://sentry.io',
  80. },
  81. });
  82. ConfigStore.loadInitialData(window.__initialData);
  83. const getProviderMock = MockApiClient.addMockResponse({
  84. url: `/organizations/${org2.slug}/config/integrations/?provider_key=vercel`,
  85. body: {providers: [VercelProviderFixture()]},
  86. });
  87. const getOrgMock = MockApiClient.addMockResponse({
  88. url: `/organizations/${org2.slug}/`,
  89. body: org2,
  90. });
  91. render(
  92. <IntegrationOrganizationLink
  93. {...initialData.routerProps}
  94. params={{integrationSlug: 'vercel'}}
  95. />,
  96. {
  97. router: initialData.router,
  98. }
  99. );
  100. // Select the same organization as the domain
  101. await selectEvent.select(screen.getByRole('textbox'), org2.name);
  102. expect(window.location.assign).not.toHaveBeenCalled();
  103. expect(screen.getByRole('button', {name: 'Install Vercel'})).toBeEnabled();
  104. expect(getProviderMock).toHaveBeenCalled();
  105. expect(getOrgMock).toHaveBeenCalled();
  106. });
  107. });