repositoryRow.spec.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import {Organization} from 'fixtures/js-stubs/organization';
  2. import {Repository} from 'fixtures/js-stubs/repository';
  3. import {routerContext} from 'fixtures/js-stubs/routerContext';
  4. import {
  5. render,
  6. renderGlobalModal,
  7. screen,
  8. userEvent,
  9. } from 'sentry-test/reactTestingLibrary';
  10. import {Client} from 'sentry/api';
  11. import RepositoryRow from 'sentry/components/repositoryRow';
  12. describe('RepositoryRow', function () {
  13. beforeEach(function () {
  14. MockApiClient.clearMockResponses();
  15. });
  16. const repository = Repository();
  17. const pendingRepo = Repository({
  18. status: 'pending_deletion',
  19. });
  20. const customRepo = Repository({
  21. provider: {
  22. id: 'integrations:custom_scm',
  23. },
  24. });
  25. const customPendingRepo = Repository({
  26. provider: {
  27. id: 'integrations:custom_scm',
  28. },
  29. status: 'pending_deletion',
  30. });
  31. const api = new Client();
  32. describe('rendering with access', function () {
  33. const organization = Organization({
  34. access: ['org:integrations'],
  35. });
  36. const routerContext = routerContext([{organization}]);
  37. it('displays provider information', function () {
  38. render(
  39. <RepositoryRow
  40. repository={repository}
  41. api={api}
  42. orgId={organization.slug}
  43. organization={organization}
  44. />,
  45. {context: routerContext}
  46. );
  47. expect(screen.getByText(repository.name)).toBeInTheDocument();
  48. expect(screen.getByText('github.com/example/repo-name')).toBeInTheDocument();
  49. // Trash button should display enabled
  50. expect(screen.getByRole('button', {name: 'delete'})).toBeEnabled();
  51. // No cancel button
  52. expect(screen.queryByRole('button', {name: 'Cancel'})).not.toBeInTheDocument();
  53. });
  54. it('displays cancel pending button', function () {
  55. render(
  56. <RepositoryRow
  57. repository={pendingRepo}
  58. api={api}
  59. orgId={organization.slug}
  60. organization={organization}
  61. />,
  62. {context: routerContext}
  63. );
  64. // Trash button should be disabled
  65. expect(screen.getByRole('button', {name: 'delete'})).toBeDisabled();
  66. // Cancel button active
  67. expect(screen.getByRole('button', {name: 'Cancel'})).toBeInTheDocument();
  68. expect(screen.getByRole('button', {name: 'Cancel'})).toBeEnabled();
  69. });
  70. });
  71. describe('rendering without access', function () {
  72. const organization = Organization({
  73. access: ['org:write'],
  74. });
  75. const routerContext = routerContext([{organization}]);
  76. it('displays disabled trash', function () {
  77. render(
  78. <RepositoryRow
  79. repository={repository}
  80. api={api}
  81. orgId={organization.slug}
  82. organization={organization}
  83. />,
  84. {context: routerContext}
  85. );
  86. // Trash button should be disabled
  87. expect(screen.getByRole('button', {name: 'delete'})).toBeDisabled();
  88. });
  89. it('displays disabled cancel', function () {
  90. render(
  91. <RepositoryRow
  92. repository={pendingRepo}
  93. api={api}
  94. orgId={organization.slug}
  95. organization={organization}
  96. />,
  97. {context: routerContext}
  98. );
  99. // Cancel should be disabled
  100. expect(screen.getByRole('button', {name: 'Cancel'})).toBeDisabled();
  101. });
  102. });
  103. describe('deletion', function () {
  104. const organization = Organization({
  105. access: ['org:integrations'],
  106. });
  107. const routerContext = routerContext([{organization}]);
  108. it('sends api request on delete', function () {
  109. const deleteRepo = MockApiClient.addMockResponse({
  110. url: `/organizations/${organization.slug}/repos/${repository.id}/`,
  111. method: 'DELETE',
  112. statusCode: 204,
  113. body: {},
  114. });
  115. render(
  116. <RepositoryRow
  117. repository={repository}
  118. api={api}
  119. orgId={organization.slug}
  120. organization={organization}
  121. />,
  122. {context: routerContext}
  123. );
  124. renderGlobalModal();
  125. userEvent.click(screen.getByRole('button', {name: 'delete'}));
  126. // Confirm modal
  127. userEvent.click(screen.getByRole('button', {name: 'Confirm'}));
  128. expect(deleteRepo).toHaveBeenCalled();
  129. });
  130. });
  131. describe('cancel deletion', function () {
  132. const organization = Organization({
  133. access: ['org:integrations'],
  134. });
  135. const routerContext = routerContext([{organization}]);
  136. it('sends api request to cancel', function () {
  137. const cancel = MockApiClient.addMockResponse({
  138. url: `/organizations/${organization.slug}/repos/${pendingRepo.id}/`,
  139. method: 'PUT',
  140. statusCode: 204,
  141. body: {},
  142. });
  143. render(
  144. <RepositoryRow
  145. repository={pendingRepo}
  146. api={api}
  147. orgId={organization.slug}
  148. organization={organization}
  149. />,
  150. {context: routerContext}
  151. );
  152. userEvent.click(screen.getByRole('button', {name: 'Cancel'}));
  153. expect(cancel).toHaveBeenCalled();
  154. });
  155. });
  156. describe('renders custom_scm repo', function () {
  157. const organization = Organization({
  158. access: ['org:integrations'],
  159. features: ['integrations-custom-scm'],
  160. });
  161. const routerContext = routerContext([{organization}]);
  162. it('displays edit button', function () {
  163. render(
  164. <RepositoryRow
  165. repository={customRepo}
  166. api={api}
  167. orgId={organization.slug}
  168. organization={organization}
  169. />,
  170. {context: routerContext}
  171. );
  172. // Trash button should display enabled
  173. expect(screen.getByRole('button', {name: 'delete'})).toBeEnabled();
  174. // No cancel button
  175. expect(screen.queryByRole('button', {name: 'Cancel'})).not.toBeInTheDocument();
  176. // Edit button should display enabled
  177. expect(screen.getByRole('button', {name: 'edit'})).toBeEnabled();
  178. });
  179. it('disables edit button when cancel pending', function () {
  180. render(
  181. <RepositoryRow
  182. repository={customPendingRepo}
  183. api={api}
  184. orgId={organization.slug}
  185. organization={organization}
  186. />,
  187. {context: routerContext}
  188. );
  189. // Trash button should be disabled
  190. expect(screen.getByRole('button', {name: 'delete'})).toBeDisabled();
  191. // Edit button should be disabled
  192. expect(screen.getByRole('button', {name: 'edit'})).toBeDisabled();
  193. // Cancel button active
  194. expect(screen.queryByRole('button', {name: 'Cancel'})).toBeEnabled();
  195. });
  196. });
  197. });