repositoryRow.spec.tsx 6.0 KB

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