index.spec.tsx 3.8 KB

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