issues.spec.tsx 6.2 KB

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