integrationExternalMappingForm.spec.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import {GitHubIntegrationFixture} from 'sentry-fixture/githubIntegration';
  2. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  3. import IntegrationExternalMappingForm from './integrationExternalMappingForm';
  4. describe('IntegrationExternalMappingForm', function () {
  5. const dataEndpoint = '/test/dataEndpoint/';
  6. const baseProps = {
  7. integration: GitHubIntegrationFixture(),
  8. dataEndpoint,
  9. getBaseFormEndpoint: jest.fn(_mapping => dataEndpoint),
  10. sentryNamesMapper: mappings => mappings,
  11. };
  12. const MOCK_USER_MAPPING = {
  13. id: '1',
  14. userId: '1',
  15. externalName: '@gwen',
  16. sentryName: 'gwen@mcu.org',
  17. };
  18. const MOCK_TEAM_MAPPING = {
  19. id: '1',
  20. teamId: '1',
  21. externalName: '@getsentry/animals',
  22. sentryName: '#zoo',
  23. };
  24. const DEFAULT_OPTIONS = [
  25. {id: '1', name: 'option1'},
  26. {id: '2', name: 'option2'},
  27. {id: '3', name: 'option3'},
  28. ];
  29. let getResponse, postResponse, putResponse;
  30. beforeEach(() => {
  31. jest.clearAllMocks();
  32. MockApiClient.clearMockResponses();
  33. getResponse = MockApiClient.addMockResponse({
  34. url: dataEndpoint,
  35. method: 'GET',
  36. body: DEFAULT_OPTIONS,
  37. });
  38. postResponse = MockApiClient.addMockResponse({
  39. url: dataEndpoint,
  40. method: 'POST',
  41. body: {},
  42. });
  43. putResponse = MockApiClient.addMockResponse({
  44. url: `${dataEndpoint}1/`,
  45. method: 'PUT',
  46. body: {},
  47. });
  48. });
  49. // No mapping provided (e.g. Create a new mapping)
  50. it('renders with no mapping provided as a form', async function () {
  51. render(<IntegrationExternalMappingForm type="user" {...baseProps} />);
  52. expect(await screen.findByPlaceholderText('@username')).toBeInTheDocument();
  53. expect(screen.getByText('Select Sentry User')).toBeInTheDocument();
  54. expect(screen.getByTestId('form-submit')).toBeInTheDocument();
  55. });
  56. it('renders with no mapping as an inline field', async function () {
  57. render(<IntegrationExternalMappingForm isInline type="user" {...baseProps} />);
  58. expect(await screen.findByText('Select Sentry User')).toBeInTheDocument();
  59. expect(screen.queryByPlaceholderText('@username')).not.toBeInTheDocument();
  60. expect(screen.queryByTestId('form-submit')).not.toBeInTheDocument();
  61. });
  62. // Full mapping provided (e.g. Update an existing mapping)
  63. it('renders with a full mapping provided as a form', async function () {
  64. render(
  65. <IntegrationExternalMappingForm
  66. type="user"
  67. mapping={MOCK_USER_MAPPING}
  68. {...baseProps}
  69. />
  70. );
  71. expect(
  72. await screen.findByDisplayValue(MOCK_USER_MAPPING.externalName)
  73. ).toBeInTheDocument();
  74. expect(screen.getByText(`option${MOCK_USER_MAPPING.userId}`)).toBeInTheDocument();
  75. expect(screen.getByTestId('form-submit')).toBeInTheDocument();
  76. });
  77. it('renders with a full mapping provided as an inline field', async function () {
  78. render(
  79. <IntegrationExternalMappingForm
  80. isInline
  81. type="user"
  82. mapping={MOCK_USER_MAPPING}
  83. {...baseProps}
  84. />
  85. );
  86. expect(
  87. await screen.findByText(`option${MOCK_USER_MAPPING.userId}`)
  88. ).toBeInTheDocument();
  89. expect(
  90. screen.queryByDisplayValue(MOCK_USER_MAPPING.externalName)
  91. ).not.toBeInTheDocument();
  92. expect(screen.queryByTestId('form-submit')).not.toBeInTheDocument();
  93. });
  94. // Suggested mapping provided (e.g. Create new mapping from suggested external name)
  95. it('renders with a suggested mapping provided as a form', async function () {
  96. render(
  97. <IntegrationExternalMappingForm
  98. type="team"
  99. mapping={{externalName: MOCK_TEAM_MAPPING.externalName}}
  100. {...baseProps}
  101. />
  102. );
  103. expect(
  104. await screen.findByDisplayValue(MOCK_TEAM_MAPPING.externalName)
  105. ).toBeInTheDocument();
  106. expect(screen.getByText('Select Sentry Team')).toBeInTheDocument();
  107. expect(screen.getByTestId('form-submit')).toBeInTheDocument();
  108. });
  109. it('renders with a suggested mapping provided as an inline field', async function () {
  110. render(
  111. <IntegrationExternalMappingForm
  112. isInline
  113. type="team"
  114. mapping={{externalName: MOCK_TEAM_MAPPING.externalName}}
  115. {...baseProps}
  116. />
  117. );
  118. expect(await screen.findByText('Select Sentry Team')).toBeInTheDocument();
  119. expect(
  120. screen.queryByDisplayValue(MOCK_TEAM_MAPPING.externalName)
  121. ).not.toBeInTheDocument();
  122. expect(screen.queryByTestId('form-submit')).not.toBeInTheDocument();
  123. });
  124. it('updates the model when submitting', async function () {
  125. render(
  126. <IntegrationExternalMappingForm
  127. type="user"
  128. mapping={{externalName: MOCK_USER_MAPPING.externalName}}
  129. {...baseProps}
  130. />
  131. );
  132. expect(baseProps.getBaseFormEndpoint).not.toHaveBeenCalled();
  133. expect(postResponse).not.toHaveBeenCalled();
  134. await userEvent.type(screen.getByText('Select Sentry User'), 'option2');
  135. await userEvent.click(screen.getAllByText('option2')[1]);
  136. await userEvent.click(screen.getByTestId('form-submit'));
  137. await waitFor(() => {
  138. expect(baseProps.getBaseFormEndpoint).toHaveBeenCalledWith({
  139. externalName: MOCK_USER_MAPPING.externalName,
  140. integrationId: baseProps.integration.id,
  141. provider: baseProps.integration.provider.name.toLowerCase(),
  142. // From option2 selection
  143. userId: '2',
  144. });
  145. });
  146. expect(postResponse).toHaveBeenCalled();
  147. expect(putResponse).not.toHaveBeenCalled();
  148. });
  149. it('submits on blur when used as an inline field', async function () {
  150. render(
  151. <IntegrationExternalMappingForm
  152. isInline
  153. type="team"
  154. mapping={MOCK_TEAM_MAPPING}
  155. {...baseProps}
  156. />
  157. );
  158. expect(await screen.findByText('option1')).toBeInTheDocument();
  159. expect(baseProps.getBaseFormEndpoint).not.toHaveBeenCalled();
  160. expect(putResponse).not.toHaveBeenCalled();
  161. await userEvent.type(screen.getByRole('textbox'), 'option3');
  162. expect(await screen.findAllByText('option3')).toHaveLength(2);
  163. await userEvent.click(screen.getAllByText('option3')[1]);
  164. await waitFor(() => {
  165. expect(baseProps.getBaseFormEndpoint).toHaveBeenCalledWith({
  166. ...MOCK_TEAM_MAPPING,
  167. integrationId: baseProps.integration.id,
  168. provider: baseProps.integration.provider.name.toLowerCase(),
  169. // From option3 selection
  170. teamId: '3',
  171. });
  172. });
  173. expect(putResponse).toHaveBeenCalled();
  174. expect(postResponse).not.toHaveBeenCalled();
  175. });
  176. it('allows defaultOptions to be provided', async function () {
  177. render(
  178. <IntegrationExternalMappingForm
  179. type="user"
  180. mapping={MOCK_USER_MAPPING}
  181. defaultOptions={DEFAULT_OPTIONS.map(({id, name}) => ({value: id, label: name}))}
  182. {...baseProps}
  183. />
  184. );
  185. const sentryNameField = screen.getByText(`option${MOCK_USER_MAPPING.userId}`);
  186. // Don't query for results on load
  187. expect(await screen.findByText('option1')).toBeInTheDocument();
  188. expect(sentryNameField).toBeInTheDocument();
  189. expect(getResponse).not.toHaveBeenCalled();
  190. // Now that the user types, query for results
  191. await userEvent.type(sentryNameField, 'option2');
  192. await userEvent.click(screen.getAllByText('option2')[1]);
  193. await waitFor(() => expect(getResponse).toHaveBeenCalled());
  194. });
  195. });