issues.spec.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. teams,
  134. projects,
  135. });
  136. if (isOrgOwner !== undefined && !isOrgOwner) {
  137. organization.access = organization.access.filter(scope => scope !== 'org:admin');
  138. }
  139. TeamStore.loadInitialData(teams, false, null);
  140. return render(<TeamStatsIssues {...routerProps} />, {organization});
  141. }
  142. it('defaults to first team', async () => {
  143. createWrapper();
  144. expect(await screen.findByText('#backend')).toBeInTheDocument();
  145. expect(screen.getByText('All Unresolved Issues')).toBeInTheDocument();
  146. });
  147. it('allows team switching as non-owner', async () => {
  148. createWrapper({isOrgOwner: false});
  149. expect(screen.getByText('#backend')).toBeInTheDocument();
  150. await userEvent.type(screen.getByText('#backend'), '{mouseDown}');
  151. expect(screen.getByText('#frontend')).toBeInTheDocument();
  152. // Teams user is not a member of are hidden
  153. expect(screen.queryByText('#internal')).not.toBeInTheDocument();
  154. await userEvent.click(screen.getByText('#frontend'));
  155. expect(router.push).toHaveBeenCalledWith(
  156. expect.objectContaining({query: {team: team1.id}})
  157. );
  158. expect(localStorage.setItem).toHaveBeenCalledWith(
  159. 'teamInsightsSelectedTeamId:org-slug',
  160. team1.id
  161. );
  162. });
  163. it('allows team switching as owner', async () => {
  164. createWrapper();
  165. expect(screen.getByText('#backend')).toBeInTheDocument();
  166. await userEvent.type(screen.getByText('#backend'), '{mouseDown}');
  167. expect(screen.getByText('#frontend')).toBeInTheDocument();
  168. // Org owners can see all teams including ones they are not members of
  169. expect(screen.queryByText('#internal')).toBeInTheDocument();
  170. await userEvent.click(screen.getByText('#internal'));
  171. expect(router.push).toHaveBeenCalledWith(
  172. expect.objectContaining({query: {team: team3.id}})
  173. );
  174. expect(localStorage.setItem).toHaveBeenCalledWith(
  175. 'teamInsightsSelectedTeamId:org-slug',
  176. team3.id
  177. );
  178. });
  179. it('can filter by environment', async () => {
  180. createWrapper();
  181. // For some reason the "Environment:" is rendered via css :before
  182. expect(screen.getByText('All')).toBeInTheDocument();
  183. await userEvent.type(screen.getByText('All'), '{mouseDown}');
  184. expect(screen.getByText(env1)).toBeInTheDocument();
  185. await userEvent.click(screen.getByText(env1));
  186. expect(router.push).toHaveBeenCalledWith(
  187. expect.objectContaining({query: {environment: 'prod'}})
  188. );
  189. });
  190. it('superusers can switch to any team', async () => {
  191. jest.mocked(isActiveSuperuser).mockReturnValue(true);
  192. createWrapper();
  193. expect(screen.getByText('#backend')).toBeInTheDocument();
  194. await userEvent.type(screen.getByText('#backend'), '{mouseDown}');
  195. expect(screen.getByText('#frontend')).toBeInTheDocument();
  196. // User is not a member of internal team
  197. expect(screen.getByText('#internal')).toBeInTheDocument();
  198. });
  199. it('shows users with no teams the join team button', () => {
  200. createWrapper({
  201. projects: [{...project1, isMember: false}],
  202. teams: [],
  203. });
  204. expect(screen.getByText('Join a Team')).toBeInTheDocument();
  205. });
  206. });