issues.spec.tsx 7.3 KB

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