index.spec.tsx 5.6 KB

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