index.spec.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import {GitHubIntegrationConfigFixture} from 'sentry-fixture/integrationListDirectory';
  2. import {OrganizationFixture} from 'sentry-fixture/organization';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  5. import {openModal} from 'sentry/actionCreators/modal';
  6. import ProjectOwnership from 'sentry/views/settings/project/projectOwnership';
  7. jest.mock('sentry/actionCreators/modal');
  8. describe('Project Ownership', () => {
  9. const {organization, project, routerProps} = initializeOrg();
  10. beforeEach(() => {
  11. MockApiClient.addMockResponse({
  12. url: `/projects/${organization.slug}/${project.slug}/ownership/`,
  13. method: 'GET',
  14. body: {
  15. fallthrough: false,
  16. autoAssignment: 'Auto Assign to Suspect Commits',
  17. codeownersAutoSync: false,
  18. },
  19. });
  20. MockApiClient.addMockResponse({
  21. url: `/organizations/${organization.slug}/code-mappings/?project=${project.id}`,
  22. method: 'GET',
  23. body: [],
  24. });
  25. MockApiClient.addMockResponse({
  26. url: `/organizations/${organization.slug}/integrations/?features=codeowners`,
  27. method: 'GET',
  28. body: [GitHubIntegrationConfigFixture()],
  29. });
  30. MockApiClient.addMockResponse({
  31. url: `/projects/${organization.slug}/${project.slug}/codeowners/`,
  32. method: 'GET',
  33. body: [],
  34. });
  35. });
  36. afterEach(() => {
  37. MockApiClient.clearMockResponses();
  38. });
  39. describe('without codeowners', () => {
  40. it('renders', async () => {
  41. render(
  42. <ProjectOwnership
  43. {...routerProps}
  44. params={{projectId: project.slug}}
  45. organization={organization}
  46. project={project}
  47. />
  48. );
  49. expect(await screen.findByText('No ownership rules found')).toBeInTheDocument();
  50. // Does not render codeowners for orgs without 'integrations-codeowners' feature
  51. expect(
  52. screen.queryByRole('button', {name: 'Add CODEOWNERS'})
  53. ).not.toBeInTheDocument();
  54. });
  55. it('renders allows users to edit ownership rules', async () => {
  56. render(
  57. <ProjectOwnership
  58. {...routerProps}
  59. params={{projectId: project.slug}}
  60. organization={organization}
  61. project={project}
  62. />,
  63. {organization: OrganizationFixture({access: ['project:read']})}
  64. );
  65. expect(await screen.findByTestId('project-permission-alert')).toBeInTheDocument();
  66. expect(screen.queryByRole('button', {name: 'Edit Rules'})).toBeEnabled();
  67. // eslint-disable-next-line jest-dom/prefer-in-document
  68. expect(screen.getAllByTestId('project-permission-alert')).toHaveLength(1);
  69. });
  70. });
  71. describe('with codeowners', () => {
  72. it('codeowners button opens modal', async () => {
  73. const org = OrganizationFixture({
  74. features: ['integrations-codeowners'],
  75. access: ['org:integrations'],
  76. });
  77. render(
  78. <ProjectOwnership
  79. {...routerProps}
  80. params={{projectId: project.slug}}
  81. organization={org}
  82. project={project}
  83. />
  84. );
  85. // Renders button
  86. expect(
  87. await screen.findByRole('button', {name: 'Import CODEOWNERS'})
  88. ).toBeInTheDocument();
  89. // Opens modal
  90. await userEvent.click(screen.getByRole('button', {name: 'Import CODEOWNERS'}));
  91. expect(openModal).toHaveBeenCalled();
  92. });
  93. });
  94. describe('issue owners settings', () => {
  95. it('should set autoAssignment with commit-context string', async () => {
  96. const updateOwnership = MockApiClient.addMockResponse({
  97. url: `/projects/${organization.slug}/${project.slug}/ownership/`,
  98. method: 'PUT',
  99. body: {
  100. fallthrough: false,
  101. autoAssignment: 'Assign To Issue Owner',
  102. codeownersAutoSync: false,
  103. },
  104. });
  105. render(
  106. <ProjectOwnership
  107. {...routerProps}
  108. params={{projectId: project.slug}}
  109. organization={organization}
  110. project={project}
  111. />
  112. );
  113. // Switch to Assign To Issue Owner
  114. await userEvent.click(await screen.findByText('Auto-assign to suspect commits'));
  115. await userEvent.click(screen.getByText('Auto-assign to issue owner'));
  116. await waitFor(() => {
  117. expect(updateOwnership).toHaveBeenCalledWith(
  118. expect.anything(),
  119. expect.objectContaining({
  120. data: {
  121. autoAssignment: 'Auto Assign to Issue Owner',
  122. },
  123. })
  124. );
  125. });
  126. });
  127. });
  128. });