index.spec.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import selectEvent from 'react-select-event';
  2. import pick from 'lodash/pick';
  3. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  4. import {textWithMarkupMatcher} from 'sentry-test/utils';
  5. import type {Organization} from 'sentry/types';
  6. import SentryAppExternalInstallation from 'sentry/views/sentryAppExternalInstallation';
  7. describe('SentryAppExternalInstallation', () => {
  8. let sentryApp: ReturnType<typeof TestStubs.SentryApp>,
  9. getOrgsMock: ReturnType<typeof MockApiClient.addMockResponse>,
  10. getOrgMock: ReturnType<typeof MockApiClient.addMockResponse>,
  11. getAppMock: ReturnType<typeof MockApiClient.addMockResponse>,
  12. getInstallationsMock: ReturnType<typeof MockApiClient.addMockResponse>,
  13. getFeaturesMock: ReturnType<typeof MockApiClient.addMockResponse>,
  14. org1: Organization,
  15. org1Lite: Pick<Organization, 'slug' | 'name' | 'id'>,
  16. org2: Organization,
  17. org2Lite: Pick<Organization, 'slug' | 'name' | 'id'>;
  18. beforeEach(() => {
  19. MockApiClient.clearMockResponses();
  20. org1 = TestStubs.Organization({
  21. slug: 'org1',
  22. name: 'Organization 1',
  23. });
  24. org2 = TestStubs.Organization({
  25. slug: 'org2',
  26. name: 'Organization 2',
  27. });
  28. org1Lite = pick(org1, ['slug', 'name', 'id']);
  29. org2Lite = pick(org2, ['slug', 'name', 'id']);
  30. sentryApp = TestStubs.SentryApp({
  31. status: 'published',
  32. redirectUrl: 'https://google.com',
  33. });
  34. getAppMock = MockApiClient.addMockResponse({
  35. url: `/sentry-apps/${sentryApp.slug}/`,
  36. body: sentryApp,
  37. });
  38. getFeaturesMock = MockApiClient.addMockResponse({
  39. url: `/sentry-apps/${sentryApp.slug}/features/`,
  40. body: [],
  41. });
  42. MockApiClient.addMockResponse({
  43. url: `/sentry-apps/${sentryApp.slug}/interaction/`,
  44. method: 'POST',
  45. statusCode: 200,
  46. body: {},
  47. });
  48. });
  49. describe('single organization', () => {
  50. beforeEach(() => {
  51. getOrgsMock = MockApiClient.addMockResponse({
  52. url: '/organizations/',
  53. body: [org1Lite],
  54. });
  55. getOrgMock = MockApiClient.addMockResponse({
  56. url: `/organizations/${org1.slug}/`,
  57. body: org1,
  58. });
  59. getInstallationsMock = MockApiClient.addMockResponse({
  60. url: `/organizations/${org1.slug}/sentry-app-installations/`,
  61. body: [],
  62. });
  63. });
  64. it('sets the org automatically', () => {
  65. render(
  66. <SentryAppExternalInstallation
  67. {...TestStubs.routeComponentProps()}
  68. params={{sentryAppSlug: sentryApp.slug}}
  69. />
  70. );
  71. expect(getAppMock).toHaveBeenCalled();
  72. expect(getOrgsMock).toHaveBeenCalled();
  73. expect(getOrgMock).toHaveBeenCalled();
  74. expect(getInstallationsMock).toHaveBeenCalled();
  75. expect(
  76. screen.getByText(
  77. textWithMarkupMatcher(
  78. 'You are installing Sample App for organization Organization 1'
  79. )
  80. )
  81. ).toBeInTheDocument();
  82. expect(screen.queryByText('Select an organization')).not.toBeInTheDocument();
  83. });
  84. it('installs and redirects', async () => {
  85. const installUrl = `/organizations/${org1.slug}/sentry-app-installations/`;
  86. const install = {
  87. uuid: 'fake-id',
  88. code: 'some-code',
  89. };
  90. const installMock = MockApiClient.addMockResponse({
  91. url: installUrl,
  92. method: 'POST',
  93. body: install,
  94. });
  95. render(
  96. <SentryAppExternalInstallation
  97. {...TestStubs.routeComponentProps()}
  98. params={{sentryAppSlug: sentryApp.slug}}
  99. />
  100. );
  101. await userEvent.click(await screen.findByTestId('install'));
  102. expect(installMock).toHaveBeenCalledWith(
  103. installUrl,
  104. expect.objectContaining({
  105. data: {slug: sentryApp.slug},
  106. })
  107. );
  108. await waitFor(() => {
  109. expect(window.location.assign).toHaveBeenCalledWith(
  110. `https://google.com/?code=${install.code}&installationId=${install.uuid}&orgSlug=${org1.slug}`
  111. );
  112. });
  113. (window.location.assign as jest.Mock).mockClear();
  114. });
  115. });
  116. describe('multiple organizations', () => {
  117. beforeEach(() => {
  118. getOrgsMock = MockApiClient.addMockResponse({
  119. url: '/organizations/',
  120. body: [org1Lite, org2Lite],
  121. });
  122. });
  123. it('renders org dropdown', () => {
  124. render(
  125. <SentryAppExternalInstallation
  126. {...TestStubs.routeComponentProps()}
  127. params={{sentryAppSlug: sentryApp.slug}}
  128. />
  129. );
  130. expect(getAppMock).toHaveBeenCalled();
  131. expect(getOrgsMock).toHaveBeenCalled();
  132. expect(screen.getByText('Select an organization')).toBeInTheDocument();
  133. });
  134. it('selecting org from dropdown loads the org through the API', async () => {
  135. getOrgMock = MockApiClient.addMockResponse({
  136. url: `/organizations/${org2.slug}/`,
  137. body: org2,
  138. });
  139. getInstallationsMock = MockApiClient.addMockResponse({
  140. url: `/organizations/${org2.slug}/sentry-app-installations/`,
  141. body: [],
  142. });
  143. render(
  144. <SentryAppExternalInstallation
  145. {...TestStubs.routeComponentProps()}
  146. params={{sentryAppSlug: sentryApp.slug}}
  147. />
  148. );
  149. await selectEvent.select(screen.getByText('Select an organization'), 'org2');
  150. expect(getOrgMock).toHaveBeenCalledTimes(1);
  151. expect(getOrgMock).toHaveBeenLastCalledWith(
  152. '/organizations/org2/',
  153. expect.anything()
  154. );
  155. expect(getInstallationsMock).toHaveBeenCalled();
  156. expect(getFeaturesMock).toHaveBeenCalled();
  157. await waitFor(() => expect(screen.getByTestId('install')).toBeEnabled());
  158. });
  159. });
  160. });