integrationExternalMappings.spec.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import {GitHubIntegrationFixture} from 'sentry-fixture/githubIntegration';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {
  4. render,
  5. renderGlobalModal,
  6. screen,
  7. userEvent,
  8. } from 'sentry-test/reactTestingLibrary';
  9. import IntegrationExternalMappings from './integrationExternalMappings';
  10. describe('IntegrationExternalMappings', function () {
  11. const {organization, router} = initializeOrg();
  12. const onCreateMock = jest.fn();
  13. const onDeleteMock = jest.fn();
  14. const MOCK_USER_SUGGESTIONS = ['@peter', '@ned', '@mj'];
  15. const MOCK_TEAM_SUGGESTIONS = [
  16. '@getsentry/snacks',
  17. '@getsentry/sports',
  18. '@getsentry/hype',
  19. ];
  20. const MOCK_USER_MAPPINGS = [
  21. {
  22. id: '1',
  23. userId: '1',
  24. externalName: '@gwen',
  25. sentryName: 'gwen@mcu.org',
  26. },
  27. {
  28. id: '2',
  29. userId: '2',
  30. externalName: '@eddie',
  31. sentryName: 'eddie@mcu.org',
  32. },
  33. ];
  34. const MOCK_TEAM_MAPPINGS = [
  35. {
  36. id: '1',
  37. teamId: '1',
  38. externalName: '@getsentry/animals',
  39. sentryName: '#zoo',
  40. },
  41. {
  42. id: '2',
  43. teamId: '2',
  44. externalName: '@getsentry/ghosts',
  45. sentryName: '#boo',
  46. },
  47. ];
  48. const createMockSuggestions = () => {
  49. MockApiClient.addMockResponse({
  50. url: `/organizations/${organization.slug}/codeowners-associations/`,
  51. method: 'GET',
  52. body: {
  53. 'project-1': {
  54. errors: {
  55. missing_external_users: MOCK_USER_SUGGESTIONS,
  56. missing_external_teams: MOCK_TEAM_SUGGESTIONS,
  57. },
  58. },
  59. },
  60. });
  61. };
  62. it('renders empty if not mappings are provided or found', function () {
  63. MockApiClient.addMockResponse({
  64. url: `/organizations/${organization.slug}/codeowners-associations/`,
  65. method: 'GET',
  66. body: {},
  67. });
  68. const {container} = render(
  69. <IntegrationExternalMappings
  70. organization={organization}
  71. integration={GitHubIntegrationFixture()}
  72. mappings={[]}
  73. type="user"
  74. onCreate={onCreateMock}
  75. onDelete={onDeleteMock}
  76. defaultOptions={[]}
  77. dataEndpoint="/organizations/org-slug/codeowners-associations/"
  78. getBaseFormEndpoint={() => '/organizations/org-slug/codeowners-associations/'}
  79. sentryNamesMapper={data => data}
  80. />,
  81. {
  82. router,
  83. }
  84. );
  85. expect(container).toHaveTextContent('Set up External User Mappings.');
  86. });
  87. it('still renders suggestions if no mappings are provided', async function () {
  88. createMockSuggestions();
  89. render(
  90. <IntegrationExternalMappings
  91. organization={organization}
  92. integration={GitHubIntegrationFixture()}
  93. mappings={[]}
  94. type="user"
  95. onCreate={onCreateMock}
  96. onDelete={onDeleteMock}
  97. defaultOptions={[]}
  98. dataEndpoint="/organizations/org-slug/codeowners-associations/"
  99. getBaseFormEndpoint={() => '/organizations/org-slug/codeowners-associations/'}
  100. sentryNamesMapper={data => data}
  101. />,
  102. {
  103. router,
  104. }
  105. );
  106. expect(await screen.findByTestId('mapping-table')).toBeInTheDocument();
  107. for (const user of MOCK_USER_SUGGESTIONS) {
  108. expect(screen.getByText(user)).toBeInTheDocument();
  109. }
  110. expect(screen.getAllByTestId('more-information')).toHaveLength(3);
  111. });
  112. it('renders suggestions along with the provided mappings', async function () {
  113. createMockSuggestions();
  114. render(
  115. <IntegrationExternalMappings
  116. organization={organization}
  117. integration={GitHubIntegrationFixture()}
  118. mappings={MOCK_TEAM_MAPPINGS}
  119. type="team"
  120. onCreate={onCreateMock}
  121. onDelete={onDeleteMock}
  122. defaultOptions={[]}
  123. dataEndpoint="/organizations/org-slug/codeowners-associations/"
  124. getBaseFormEndpoint={() => '/organizations/org-slug/codeowners-associations/'}
  125. sentryNamesMapper={data => data}
  126. />,
  127. {
  128. router,
  129. }
  130. );
  131. expect(await screen.findByTestId('mapping-table')).toBeInTheDocument();
  132. for (const team of MOCK_TEAM_SUGGESTIONS) {
  133. expect(screen.getByText(team)).toBeInTheDocument();
  134. }
  135. expect(screen.getAllByRole('button', {name: 'Remove user mapping'})).toHaveLength(2);
  136. for (const team of MOCK_TEAM_MAPPINGS) {
  137. expect(screen.getByText(team.externalName)).toBeInTheDocument();
  138. expect(screen.getByText(team.sentryName)).toBeInTheDocument();
  139. }
  140. expect(screen.getAllByTestId('more-information')).toHaveLength(3);
  141. });
  142. it('uses the methods passed down from props appropriately', async function () {
  143. createMockSuggestions();
  144. render(
  145. <IntegrationExternalMappings
  146. organization={organization}
  147. integration={GitHubIntegrationFixture()}
  148. mappings={MOCK_USER_MAPPINGS}
  149. type="user"
  150. onCreate={onCreateMock}
  151. onDelete={onDeleteMock}
  152. defaultOptions={[]}
  153. dataEndpoint="/organizations/org-slug/codeowners-associations/"
  154. getBaseFormEndpoint={() => '/organizations/org-slug/codeowners-associations/'}
  155. sentryNamesMapper={data => data}
  156. />,
  157. {
  158. router,
  159. }
  160. );
  161. renderGlobalModal();
  162. expect(await screen.findByTestId('mapping-table')).toBeInTheDocument();
  163. await userEvent.click(screen.getByTestId('add-mapping-button'));
  164. expect(onCreateMock).toHaveBeenCalled();
  165. await userEvent.click(
  166. screen.getAllByRole('button', {name: 'Remove user mapping'})[0]
  167. );
  168. await userEvent.click(screen.getByTestId('confirm-button'));
  169. expect(onDeleteMock).toHaveBeenCalled();
  170. });
  171. });