issues.spec.jsx 6.0 KB

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