issues.spec.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {ProjectFixture} from 'sentry-fixture/project';
  3. import {TeamFixture} from 'sentry-fixture/team';
  4. import {TeamIssuesBreakdownFixture} from 'sentry-fixture/teamIssuesBreakdown';
  5. import {TeamResolutionTimeFixture} from 'sentry-fixture/teamResolutionTime';
  6. import {initializeOrg} from 'sentry-test/initializeOrg';
  7. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  8. import ProjectsStore from 'sentry/stores/projectsStore';
  9. import TeamStore from 'sentry/stores/teamStore';
  10. import type {Team} from 'sentry/types/organization';
  11. import type {Project} from 'sentry/types/project';
  12. import {isActiveSuperuser} from 'sentry/utils/isActiveSuperuser';
  13. import localStorage from 'sentry/utils/localStorage';
  14. import TeamStatsIssues from 'sentry/views/organizationStats/teamInsights/issues';
  15. jest.mock('sentry/utils/localStorage');
  16. jest.mock('sentry/utils/isActiveSuperuser', () => ({
  17. isActiveSuperuser: jest.fn(),
  18. }));
  19. describe('TeamStatsIssues', () => {
  20. const env1 = 'prod';
  21. const env2 = 'dev';
  22. const project1 = ProjectFixture({
  23. id: '2',
  24. name: 'js',
  25. slug: 'js',
  26. environments: [env1, env2],
  27. });
  28. const project2 = ProjectFixture({
  29. id: '3',
  30. name: 'py',
  31. slug: 'py',
  32. environments: [env1, env2],
  33. });
  34. const team1 = TeamFixture({
  35. id: '2',
  36. slug: 'frontend',
  37. name: 'frontend',
  38. projects: [project1],
  39. isMember: true,
  40. });
  41. const team2 = TeamFixture({
  42. id: '3',
  43. slug: 'backend',
  44. name: 'backend',
  45. projects: [project2],
  46. isMember: true,
  47. });
  48. const team3 = TeamFixture({
  49. id: '4',
  50. slug: 'internal',
  51. name: 'internal',
  52. projects: [],
  53. isMember: false,
  54. });
  55. const {routerProps, router} = initializeOrg();
  56. beforeEach(() => {
  57. TeamStore.reset();
  58. MockApiClient.addMockResponse({
  59. method: 'GET',
  60. url: `/organizations/org-slug/projects/`,
  61. body: [],
  62. });
  63. MockApiClient.addMockResponse({
  64. url: `/teams/org-slug/${team1.slug}/time-to-resolution/`,
  65. body: TeamResolutionTimeFixture(),
  66. });
  67. MockApiClient.addMockResponse({
  68. url: `/teams/org-slug/${team1.slug}/issue-breakdown/`,
  69. body: TeamIssuesBreakdownFixture(),
  70. });
  71. MockApiClient.addMockResponse({
  72. url: `/teams/org-slug/${team2.slug}/alerts-triggered-index/`,
  73. body: [],
  74. });
  75. MockApiClient.addMockResponse({
  76. url: `/teams/org-slug/${team2.slug}/time-to-resolution/`,
  77. body: TeamResolutionTimeFixture(),
  78. });
  79. MockApiClient.addMockResponse({
  80. url: `/teams/org-slug/${team2.slug}/issue-breakdown/`,
  81. body: TeamIssuesBreakdownFixture(),
  82. });
  83. MockApiClient.addMockResponse({
  84. method: 'GET',
  85. url: `/teams/org-slug/${team2.slug}/issues/old/`,
  86. body: [],
  87. });
  88. MockApiClient.addMockResponse({
  89. method: 'GET',
  90. url: `/teams/org-slug/${team2.slug}/unresolved-issue-age/`,
  91. body: [],
  92. });
  93. const unresolvedStats = {
  94. '2021-12-10T00:00:00+00:00': {unresolved: 45},
  95. '2021-12-11T00:00:00+00:00': {unresolved: 45},
  96. '2021-12-12T00:00:00+00:00': {unresolved: 45},
  97. '2021-12-13T00:00:00+00:00': {unresolved: 49},
  98. '2021-12-14T00:00:00+00:00': {unresolved: 50},
  99. '2021-12-15T00:00:00+00:00': {unresolved: 45},
  100. '2021-12-16T00:00:00+00:00': {unresolved: 44},
  101. '2021-12-17T00:00:00+00:00': {unresolved: 44},
  102. '2021-12-18T00:00:00+00:00': {unresolved: 44},
  103. '2021-12-19T00:00:00+00:00': {unresolved: 43},
  104. '2021-12-20T00:00:00+00:00': {unresolved: 40},
  105. '2021-12-21T00:00:00+00:00': {unresolved: 37},
  106. '2021-12-22T00:00:00+00:00': {unresolved: 36},
  107. '2021-12-23T00:00:00+00:00': {unresolved: 37},
  108. };
  109. MockApiClient.addMockResponse({
  110. url: `/teams/org-slug/${team1.slug}/all-unresolved-issues/`,
  111. body: {
  112. 2: unresolvedStats,
  113. },
  114. });
  115. MockApiClient.addMockResponse({
  116. url: `/teams/org-slug/${team2.slug}/all-unresolved-issues/`,
  117. body: {
  118. 3: unresolvedStats,
  119. },
  120. });
  121. });
  122. afterEach(() => {
  123. jest.resetAllMocks();
  124. });
  125. function createWrapper({
  126. projects,
  127. teams,
  128. isOrgOwner,
  129. }: {isOrgOwner?: boolean; projects?: Project[]; teams?: Team[]} = {}) {
  130. teams = teams ?? [team1, team2, team3];
  131. projects = projects ?? [project1, project2];
  132. ProjectsStore.loadInitialData(projects);
  133. const organization = OrganizationFixture();
  134. if (isOrgOwner !== undefined && !isOrgOwner) {
  135. organization.access = organization.access.filter(scope => scope !== 'org:admin');
  136. }
  137. TeamStore.loadInitialData(teams, false, null);
  138. return render(<TeamStatsIssues {...routerProps} />, {organization});
  139. }
  140. it('defaults to first team', async () => {
  141. createWrapper();
  142. expect(await screen.findByText('#backend')).toBeInTheDocument();
  143. expect(screen.getByText('All Unresolved Issues')).toBeInTheDocument();
  144. });
  145. it('allows team switching as non-owner', async () => {
  146. createWrapper({isOrgOwner: false});
  147. expect(screen.getByText('#backend')).toBeInTheDocument();
  148. await userEvent.type(screen.getByText('#backend'), '{mouseDown}');
  149. expect(screen.getByText('#frontend')).toBeInTheDocument();
  150. // Teams user is not a member of are hidden
  151. expect(screen.queryByText('#internal')).not.toBeInTheDocument();
  152. await userEvent.click(screen.getByText('#frontend'));
  153. expect(router.push).toHaveBeenCalledWith(
  154. expect.objectContaining({query: {team: team1.id}})
  155. );
  156. expect(localStorage.setItem).toHaveBeenCalledWith(
  157. 'teamInsightsSelectedTeamId:org-slug',
  158. team1.id
  159. );
  160. });
  161. it('allows team switching as owner', async () => {
  162. createWrapper();
  163. expect(screen.getByText('#backend')).toBeInTheDocument();
  164. await userEvent.type(screen.getByText('#backend'), '{mouseDown}');
  165. expect(screen.getByText('#frontend')).toBeInTheDocument();
  166. // Org owners can see all teams including ones they are not members of
  167. expect(screen.queryByText('#internal')).toBeInTheDocument();
  168. await userEvent.click(screen.getByText('#internal'));
  169. expect(router.push).toHaveBeenCalledWith(
  170. expect.objectContaining({query: {team: team3.id}})
  171. );
  172. expect(localStorage.setItem).toHaveBeenCalledWith(
  173. 'teamInsightsSelectedTeamId:org-slug',
  174. team3.id
  175. );
  176. });
  177. it('can filter by environment', async () => {
  178. createWrapper();
  179. // For some reason the "Environment:" is rendered via css :before
  180. expect(screen.getByText('All')).toBeInTheDocument();
  181. await userEvent.type(screen.getByText('All'), '{mouseDown}');
  182. expect(screen.getByText(env1)).toBeInTheDocument();
  183. await userEvent.click(screen.getByText(env1));
  184. expect(router.push).toHaveBeenCalledWith(
  185. expect.objectContaining({query: {environment: 'prod'}})
  186. );
  187. });
  188. it('superusers can switch to any team', async () => {
  189. jest.mocked(isActiveSuperuser).mockReturnValue(true);
  190. createWrapper();
  191. expect(screen.getByText('#backend')).toBeInTheDocument();
  192. await userEvent.type(screen.getByText('#backend'), '{mouseDown}');
  193. expect(screen.getByText('#frontend')).toBeInTheDocument();
  194. // User is not a member of internal team
  195. expect(screen.getByText('#internal')).toBeInTheDocument();
  196. });
  197. it('shows users with no teams the join team button', () => {
  198. createWrapper({
  199. projects: [{...project1, isMember: false}],
  200. teams: [],
  201. });
  202. expect(screen.getByText('Join a Team')).toBeInTheDocument();
  203. });
  204. });