integrationExternalMappings.spec.jsx 4.6 KB

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