index.spec.jsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  2. import {openModal} from 'sentry/actionCreators/modal';
  3. import ProjectOwnership from 'sentry/views/settings/project/projectOwnership';
  4. jest.mock('sentry/actionCreators/modal');
  5. describe('Project Ownership', () => {
  6. let org = TestStubs.Organization();
  7. const project = TestStubs.ProjectDetails();
  8. beforeEach(() => {
  9. MockApiClient.addMockResponse({
  10. url: `/projects/${org.slug}/${project.slug}/ownership/`,
  11. method: 'GET',
  12. body: {
  13. fallthrough: false,
  14. autoAssignment: 'Auto Assign to Suspect Commits',
  15. codeownersAutoSync: false,
  16. },
  17. });
  18. MockApiClient.addMockResponse({
  19. url: `/organizations/${org.slug}/code-mappings/`,
  20. query: {project: project.id},
  21. method: 'GET',
  22. body: [],
  23. });
  24. MockApiClient.addMockResponse({
  25. url: `/organizations/${org.slug}/integrations/`,
  26. query: {features: 'codeowners'},
  27. method: 'GET',
  28. body: [TestStubs.GitHubIntegrationConfig()],
  29. });
  30. MockApiClient.addMockResponse({
  31. url: `/projects/${org.slug}/${project.slug}/codeowners/`,
  32. features: {expand: 'codeMapping'},
  33. method: 'GET',
  34. body: [],
  35. });
  36. });
  37. afterEach(() => {
  38. MockApiClient.clearMockResponses();
  39. });
  40. describe('without codeowners', () => {
  41. it('renders', () => {
  42. const wrapper = render(
  43. <ProjectOwnership
  44. params={{projectId: project.slug}}
  45. organization={org}
  46. project={project}
  47. />
  48. );
  49. expect(wrapper.container).toSnapshot();
  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', () => {
  56. render(
  57. <ProjectOwnership
  58. params={{projectId: project.slug}}
  59. organization={org}
  60. project={project}
  61. />,
  62. {organization: TestStubs.Organization({access: ['project:read']})}
  63. );
  64. expect(screen.queryByRole('button', {name: 'Edit'})).toBeEnabled();
  65. expect(screen.getByTestId('project-permission-alert')).toHaveTextContent(
  66. 'These settings can only be edited by users with the organization owner, manager, or admin role.'
  67. );
  68. // eslint-disable-next-line jest-dom/prefer-in-document
  69. expect(screen.getAllByTestId('project-permission-alert')).toHaveLength(1);
  70. });
  71. });
  72. describe('with codeowners', () => {
  73. it('codeowners button opens modal', async () => {
  74. org = TestStubs.Organization({
  75. features: ['integrations-codeowners'],
  76. access: ['org:integrations'],
  77. });
  78. render(
  79. <ProjectOwnership
  80. params={{projectId: project.slug}}
  81. organization={org}
  82. project={project}
  83. />,
  84. {context: TestStubs.routerContext([{organization: org}])}
  85. );
  86. // Renders button
  87. expect(screen.getByRole('button', {name: 'Import CODEOWNERS'})).toBeInTheDocument();
  88. // Opens modal
  89. await userEvent.click(screen.getByRole('button', {name: 'Import CODEOWNERS'}));
  90. expect(openModal).toHaveBeenCalled();
  91. });
  92. });
  93. describe('issue owners settings', () => {
  94. it('should set autoAssignment with commit-context string', async () => {
  95. const updateOwnership = MockApiClient.addMockResponse({
  96. url: `/projects/${org.slug}/${project.slug}/ownership/`,
  97. method: 'PUT',
  98. body: {
  99. fallthrough: false,
  100. autoAssignment: 'Assign To Issue Owner',
  101. codeownersAutoSync: false,
  102. },
  103. });
  104. render(
  105. <ProjectOwnership
  106. params={{projectId: project.slug}}
  107. organization={org}
  108. project={project}
  109. />
  110. );
  111. // Switch to Assign To Issue Owner
  112. await userEvent.click(screen.getByText('Auto-assign to suspect commits'));
  113. await userEvent.click(screen.getByText('Auto-assign to issue owner'));
  114. await waitFor(() => {
  115. expect(updateOwnership).toHaveBeenCalledWith(
  116. expect.anything(),
  117. expect.objectContaining({
  118. data: {
  119. autoAssignment: 'Auto Assign to Issue Owner',
  120. },
  121. })
  122. );
  123. });
  124. });
  125. it('should hide issue owners for issue-alert-fallback-targeting flag', () => {
  126. const organization = {...org, features: ['issue-alert-fallback-targeting']};
  127. render(
  128. <ProjectOwnership
  129. params={{orgId: organization.slug, projectId: project.slug}}
  130. organization={organization}
  131. project={project}
  132. />
  133. );
  134. expect(screen.getByText('Prioritize Auto Assignment')).toBeInTheDocument();
  135. expect(
  136. screen.queryByText('Send alert to project members if there’s no assigned owner')
  137. ).not.toBeInTheDocument();
  138. });
  139. });
  140. });