index.spec.jsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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')).toBeInTheDocument();
  66. // eslint-disable-next-line jest-dom/prefer-in-document
  67. expect(screen.getAllByTestId('project-permission-alert')).toHaveLength(1);
  68. });
  69. });
  70. describe('with codeowners', () => {
  71. it('codeowners button opens modal', async () => {
  72. org = TestStubs.Organization({
  73. features: ['integrations-codeowners'],
  74. access: ['org:integrations'],
  75. });
  76. render(
  77. <ProjectOwnership
  78. params={{projectId: project.slug}}
  79. organization={org}
  80. project={project}
  81. />,
  82. {context: TestStubs.routerContext([{organization: org}])}
  83. );
  84. // Renders button
  85. expect(screen.getByRole('button', {name: 'Import CODEOWNERS'})).toBeInTheDocument();
  86. // Opens modal
  87. await userEvent.click(screen.getByRole('button', {name: 'Import CODEOWNERS'}));
  88. expect(openModal).toHaveBeenCalled();
  89. });
  90. });
  91. describe('issue owners settings', () => {
  92. it('should set autoAssignment with commit-context string', async () => {
  93. const updateOwnership = MockApiClient.addMockResponse({
  94. url: `/projects/${org.slug}/${project.slug}/ownership/`,
  95. method: 'PUT',
  96. body: {
  97. fallthrough: false,
  98. autoAssignment: 'Assign To Issue Owner',
  99. codeownersAutoSync: false,
  100. },
  101. });
  102. render(
  103. <ProjectOwnership
  104. params={{projectId: project.slug}}
  105. organization={org}
  106. project={project}
  107. />
  108. );
  109. // Switch to Assign To Issue Owner
  110. await userEvent.click(screen.getByText('Auto-assign to suspect commits'));
  111. await userEvent.click(screen.getByText('Auto-assign to issue owner'));
  112. await waitFor(() => {
  113. expect(updateOwnership).toHaveBeenCalledWith(
  114. expect.anything(),
  115. expect.objectContaining({
  116. data: {
  117. autoAssignment: 'Auto Assign to Issue Owner',
  118. },
  119. })
  120. );
  121. });
  122. });
  123. it('should hide issue owners for issue-alert-fallback-targeting flag', () => {
  124. const organization = {...org, features: ['issue-alert-fallback-targeting']};
  125. render(
  126. <ProjectOwnership
  127. params={{orgId: organization.slug, projectId: project.slug}}
  128. organization={organization}
  129. project={project}
  130. />
  131. );
  132. expect(screen.getByText('Prioritize Auto Assignment')).toBeInTheDocument();
  133. expect(
  134. screen.queryByText('Send alert to project members if there’s no assigned owner')
  135. ).not.toBeInTheDocument();
  136. });
  137. });
  138. });