issues.spec.tsx 7.5 KB

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