issues.spec.tsx 7.5 KB

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