integrationExternalMappingForm.spec.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import {GitHubIntegrationFixture} from 'sentry-fixture/githubIntegration';
  2. import {act, render, screen, userEvent} 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. await act(tick);
  53. expect(screen.getByPlaceholderText('@username')).toBeInTheDocument();
  54. expect(screen.getByText('Select Sentry User')).toBeInTheDocument();
  55. expect(screen.getByTestId('form-submit')).toBeInTheDocument();
  56. });
  57. it('renders with no mapping as an inline field', async function () {
  58. render(<IntegrationExternalMappingForm isInline type="user" {...baseProps} />);
  59. await act(tick);
  60. expect(screen.queryByPlaceholderText('@username')).not.toBeInTheDocument();
  61. expect(screen.getByText('Select Sentry User')).toBeInTheDocument();
  62. expect(screen.queryByTestId('form-submit')).not.toBeInTheDocument();
  63. });
  64. // Full mapping provided (e.g. Update an existing mapping)
  65. it('renders with a full mapping provided as a form', async function () {
  66. render(
  67. <IntegrationExternalMappingForm
  68. type="user"
  69. mapping={MOCK_USER_MAPPING}
  70. {...baseProps}
  71. />
  72. );
  73. await act(tick);
  74. expect(screen.getByDisplayValue(MOCK_USER_MAPPING.externalName)).toBeInTheDocument();
  75. expect(screen.getByText(`option${MOCK_USER_MAPPING.userId}`)).toBeInTheDocument();
  76. expect(screen.getByTestId('form-submit')).toBeInTheDocument();
  77. });
  78. it('renders with a full mapping provided as an inline field', async function () {
  79. render(
  80. <IntegrationExternalMappingForm
  81. isInline
  82. type="user"
  83. mapping={MOCK_USER_MAPPING}
  84. {...baseProps}
  85. />
  86. );
  87. await act(tick);
  88. expect(
  89. screen.queryByDisplayValue(MOCK_USER_MAPPING.externalName)
  90. ).not.toBeInTheDocument();
  91. expect(screen.getByText(`option${MOCK_USER_MAPPING.userId}`)).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. await act(tick);
  104. expect(screen.getByDisplayValue(MOCK_TEAM_MAPPING.externalName)).toBeInTheDocument();
  105. expect(screen.getByText('Select Sentry Team')).toBeInTheDocument();
  106. expect(screen.getByTestId('form-submit')).toBeInTheDocument();
  107. });
  108. it('renders with a suggested mapping provided as an inline field', async function () {
  109. render(
  110. <IntegrationExternalMappingForm
  111. isInline
  112. type="team"
  113. mapping={{externalName: MOCK_TEAM_MAPPING.externalName}}
  114. {...baseProps}
  115. />
  116. );
  117. await act(tick);
  118. expect(
  119. screen.queryByDisplayValue(MOCK_TEAM_MAPPING.externalName)
  120. ).not.toBeInTheDocument();
  121. expect(screen.getByText('Select Sentry Team')).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 act(tick);
  136. await userEvent.click(screen.getAllByText('option2')[1]);
  137. await userEvent.click(screen.getByTestId('form-submit'));
  138. await act(tick);
  139. expect(baseProps.getBaseFormEndpoint).toHaveBeenCalledWith({
  140. externalName: MOCK_USER_MAPPING.externalName,
  141. integrationId: baseProps.integration.id,
  142. provider: baseProps.integration.provider.name.toLowerCase(),
  143. // From option2 selection
  144. userId: '2',
  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. await act(tick);
  159. expect(baseProps.getBaseFormEndpoint).not.toHaveBeenCalled();
  160. expect(putResponse).not.toHaveBeenCalled();
  161. await userEvent.type(screen.getByRole('textbox'), 'option3');
  162. await act(tick);
  163. await userEvent.click(screen.getAllByText('option3')[1]);
  164. expect(baseProps.getBaseFormEndpoint).toHaveBeenCalledWith({
  165. ...MOCK_TEAM_MAPPING,
  166. integrationId: baseProps.integration.id,
  167. provider: baseProps.integration.provider.name.toLowerCase(),
  168. // From option3 selection
  169. teamId: '3',
  170. });
  171. await act(tick);
  172. expect(putResponse).toHaveBeenCalled();
  173. expect(postResponse).not.toHaveBeenCalled();
  174. });
  175. it('allows defaultOptions to be provided', async function () {
  176. render(
  177. <IntegrationExternalMappingForm
  178. type="user"
  179. mapping={MOCK_USER_MAPPING}
  180. defaultOptions={DEFAULT_OPTIONS.map(({id, name}) => ({value: id, label: name}))}
  181. {...baseProps}
  182. />
  183. );
  184. const sentryNameField = screen.getByText(`option${MOCK_USER_MAPPING.userId}`);
  185. // Don't query for results on load
  186. expect(sentryNameField).toBeInTheDocument();
  187. await act(tick);
  188. expect(getResponse).not.toHaveBeenCalled();
  189. // Now that the user types, query for results
  190. await userEvent.type(sentryNameField, 'option2');
  191. await act(tick);
  192. await userEvent.click(screen.getAllByText('option2')[1]);
  193. expect(getResponse).toHaveBeenCalled();
  194. });
  195. });