integrationExternalMappingForm.spec.jsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import {GitHubIntegration} from 'fixtures/js-stubs/gitHubIntegration';
  2. import {act, render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  3. import IntegrationExternalMappingForm from 'sentry/components/integrationExternalMappingForm';
  4. describe('IntegrationExternalMappingForm', function () {
  5. const dataEndpoint = '/test/dataEndpoint/';
  6. const baseProps = {
  7. integration: GitHubIntegration(),
  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', function () {
  51. render(<IntegrationExternalMappingForm type="user" {...baseProps} />);
  52. expect(screen.getByPlaceholderText('@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. await act(tick);
  59. expect(screen.queryByPlaceholderText('@username')).not.toBeInTheDocument();
  60. expect(screen.getByText('Select Sentry User')).toBeInTheDocument();
  61. expect(screen.queryByTestId('form-submit')).not.toBeInTheDocument();
  62. });
  63. // Full mapping provided (e.g. Update an existing mapping)
  64. it('renders with a full mapping provided as a form', async function () {
  65. render(
  66. <IntegrationExternalMappingForm
  67. type="user"
  68. mapping={MOCK_USER_MAPPING}
  69. {...baseProps}
  70. />
  71. );
  72. await act(tick);
  73. expect(screen.getByDisplayValue(MOCK_USER_MAPPING.externalName)).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. await act(tick);
  87. expect(
  88. screen.queryByDisplayValue(MOCK_USER_MAPPING.externalName)
  89. ).not.toBeInTheDocument();
  90. expect(screen.getByText(`option${MOCK_USER_MAPPING.userId}`)).toBeInTheDocument();
  91. expect(screen.queryByTestId('form-submit')).not.toBeInTheDocument();
  92. });
  93. // Suggested mapping provided (e.g. Create new mapping from suggested external name)
  94. it('renders with a suggested mapping provided as a form', function () {
  95. render(
  96. <IntegrationExternalMappingForm
  97. type="team"
  98. mapping={{externalName: MOCK_TEAM_MAPPING.externalName}}
  99. {...baseProps}
  100. />
  101. );
  102. expect(screen.getByDisplayValue(MOCK_TEAM_MAPPING.externalName)).toBeInTheDocument();
  103. expect(screen.getByText('Select Sentry Team')).toBeInTheDocument();
  104. expect(screen.getByTestId('form-submit')).toBeInTheDocument();
  105. });
  106. it('renders with a suggested mapping provided as an inline field', function () {
  107. render(
  108. <IntegrationExternalMappingForm
  109. isInline
  110. type="team"
  111. mapping={{externalName: MOCK_TEAM_MAPPING.externalName}}
  112. {...baseProps}
  113. />
  114. );
  115. expect(
  116. screen.queryByDisplayValue(MOCK_TEAM_MAPPING.externalName)
  117. ).not.toBeInTheDocument();
  118. expect(screen.getByText('Select Sentry Team')).toBeInTheDocument();
  119. expect(screen.queryByTestId('form-submit')).not.toBeInTheDocument();
  120. });
  121. it('updates the model when submitting', async function () {
  122. render(
  123. <IntegrationExternalMappingForm
  124. type="user"
  125. mapping={{externalName: MOCK_USER_MAPPING.externalName}}
  126. {...baseProps}
  127. />
  128. );
  129. expect(baseProps.getBaseFormEndpoint).not.toHaveBeenCalled();
  130. expect(postResponse).not.toHaveBeenCalled();
  131. userEvent.type(screen.getByText('Select Sentry User'), 'option2');
  132. await act(tick);
  133. userEvent.click(screen.getAllByText('option2')[1]);
  134. userEvent.click(screen.getByTestId('form-submit'));
  135. expect(baseProps.getBaseFormEndpoint).toHaveBeenCalledWith({
  136. externalName: MOCK_USER_MAPPING.externalName,
  137. integrationId: baseProps.integration.id,
  138. provider: baseProps.integration.provider.name.toLowerCase(),
  139. // From option2 selection
  140. userId: '2',
  141. });
  142. expect(postResponse).toHaveBeenCalled();
  143. expect(putResponse).not.toHaveBeenCalled();
  144. });
  145. it('submits on blur when used as an inline field', async function () {
  146. render(
  147. <IntegrationExternalMappingForm
  148. isInline
  149. type="team"
  150. mapping={MOCK_TEAM_MAPPING}
  151. {...baseProps}
  152. />
  153. );
  154. await act(tick);
  155. expect(baseProps.getBaseFormEndpoint).not.toHaveBeenCalled();
  156. expect(putResponse).not.toHaveBeenCalled();
  157. userEvent.type(screen.getByRole('textbox'), 'option3');
  158. await act(tick);
  159. userEvent.click(screen.getAllByText('option3')[1]);
  160. expect(baseProps.getBaseFormEndpoint).toHaveBeenCalledWith({
  161. ...MOCK_TEAM_MAPPING,
  162. integrationId: baseProps.integration.id,
  163. provider: baseProps.integration.provider.name.toLowerCase(),
  164. // From option3 selection
  165. teamId: '3',
  166. });
  167. await act(tick);
  168. expect(putResponse).toHaveBeenCalled();
  169. expect(postResponse).not.toHaveBeenCalled();
  170. });
  171. it('allows defaultOptions to be provided', async function () {
  172. render(
  173. <IntegrationExternalMappingForm
  174. type="user"
  175. mapping={MOCK_USER_MAPPING}
  176. defaultOptions={DEFAULT_OPTIONS.map(({id, name}) => ({value: id, label: name}))}
  177. {...baseProps}
  178. />
  179. );
  180. const sentryNameField = screen.getByText(`option${MOCK_USER_MAPPING.userId}`);
  181. // Don't query for results on load
  182. expect(sentryNameField).toBeInTheDocument();
  183. await act(tick);
  184. expect(getResponse).not.toHaveBeenCalled();
  185. // Now that the user types, query for results
  186. userEvent.type(sentryNameField, 'option2');
  187. await act(tick);
  188. userEvent.click(screen.getAllByText('option2')[1]);
  189. expect(getResponse).toHaveBeenCalled();
  190. });
  191. });