issues.spec.tsx 6.4 KB

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