health.spec.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 {TeamAlertsTriggeredFixture} from 'sentry-fixture/teamAlertsTriggered';
  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 TeamStatsHealth from 'sentry/views/organizationStats/teamInsights/health';
  15. jest.mock('sentry/utils/localStorage');
  16. jest.mock('sentry/utils/isActiveSuperuser', () => ({
  17. isActiveSuperuser: jest.fn(),
  18. }));
  19. describe('TeamStatsHealth', () => {
  20. const project1 = ProjectFixture({id: '2', name: 'js', slug: 'js'});
  21. const project2 = ProjectFixture({id: '3', name: 'py', slug: 'py'});
  22. const team1 = TeamFixture({
  23. id: '2',
  24. slug: 'frontend',
  25. name: 'frontend',
  26. projects: [project1],
  27. isMember: true,
  28. });
  29. const team2 = TeamFixture({
  30. id: '3',
  31. slug: 'backend',
  32. name: 'backend',
  33. projects: [project2],
  34. isMember: true,
  35. });
  36. const team3 = TeamFixture({
  37. id: '4',
  38. slug: 'internal',
  39. name: 'internal',
  40. projects: [],
  41. isMember: false,
  42. });
  43. const {routerProps, router} = initializeOrg();
  44. beforeEach(() => {
  45. MockApiClient.addMockResponse({
  46. method: 'GET',
  47. url: `/organizations/org-slug/projects/`,
  48. body: [],
  49. });
  50. MockApiClient.addMockResponse({
  51. method: 'GET',
  52. url: `/organizations/org-slug/key-transactions-list/`,
  53. body: [],
  54. });
  55. MockApiClient.addMockResponse({
  56. method: 'GET',
  57. url: `/organizations/org-slug/legacy-key-transactions-count/`,
  58. body: [],
  59. });
  60. MockApiClient.addMockResponse({
  61. method: 'GET',
  62. url: `/organizations/org-slug/sessions/`,
  63. body: {
  64. start: '2021-10-30T00:00:00Z',
  65. end: '2021-12-24T00:00:00Z',
  66. query: '',
  67. intervals: [],
  68. groups: [
  69. {
  70. by: {project: 1, 'session.status': 'healthy'},
  71. totals: {'sum(session)': 0},
  72. series: {'sum(session)': []},
  73. },
  74. {
  75. by: {project: 1, 'session.status': 'crashed'},
  76. totals: {'sum(session)': 0},
  77. series: {'sum(session)': []},
  78. },
  79. {
  80. by: {project: 1, 'session.status': 'errored'},
  81. totals: {'sum(session)': 0},
  82. series: {'sum(session)': []},
  83. },
  84. {
  85. by: {project: 1, 'session.status': 'abnormal'},
  86. totals: {'sum(session)': 0},
  87. series: {'sum(session)': []},
  88. },
  89. ],
  90. },
  91. });
  92. MockApiClient.addMockResponse({
  93. url: '/organizations/org-slug/eventsv2/',
  94. body: {
  95. meta: {
  96. user: 'string',
  97. transaction: 'string',
  98. project: 'string',
  99. tpm: 'number',
  100. count_unique_user: 'number',
  101. count_miserable_user: 'number',
  102. user_misery: 'number',
  103. },
  104. data: [
  105. {
  106. key_transaction: 1,
  107. transaction: '/apple/cart',
  108. project: project1.slug,
  109. tpm: 30,
  110. count_unique_user: 1000,
  111. count_miserable_user: 122,
  112. user_misery: 0.114,
  113. project_threshold_config: ['duration', 300],
  114. },
  115. ],
  116. },
  117. });
  118. MockApiClient.addMockResponse({
  119. url: `/teams/org-slug/${team1.slug}/alerts-triggered/`,
  120. body: TeamAlertsTriggeredFixture(),
  121. });
  122. MockApiClient.addMockResponse({
  123. url: `/teams/org-slug/${team1.slug}/alerts-triggered-index/`,
  124. body: [],
  125. });
  126. MockApiClient.addMockResponse({
  127. url: `/teams/org-slug/${team1.slug}/time-to-resolution/`,
  128. body: TeamResolutionTimeFixture(),
  129. });
  130. MockApiClient.addMockResponse({
  131. method: 'GET',
  132. url: `/teams/org-slug/${team1.slug}/release-count/`,
  133. body: [],
  134. });
  135. MockApiClient.addMockResponse({
  136. url: `/teams/org-slug/${team2.slug}/alerts-triggered/`,
  137. body: TeamAlertsTriggeredFixture(),
  138. });
  139. MockApiClient.addMockResponse({
  140. url: `/teams/org-slug/${team2.slug}/alerts-triggered-index/`,
  141. body: [],
  142. });
  143. MockApiClient.addMockResponse({
  144. url: `/teams/org-slug/${team2.slug}/time-to-resolution/`,
  145. body: TeamResolutionTimeFixture(),
  146. });
  147. MockApiClient.addMockResponse({
  148. method: 'GET',
  149. url: `/teams/org-slug/${team2.slug}/release-count/`,
  150. body: [],
  151. });
  152. });
  153. beforeEach(() => {
  154. TeamStore.reset();
  155. });
  156. afterEach(() => {
  157. jest.resetAllMocks();
  158. });
  159. function createWrapper({
  160. projects,
  161. teams,
  162. isOrgOwner,
  163. }: {isOrgOwner?: boolean; projects?: Project[]; teams?: TeamType[]} = {}) {
  164. teams = teams ?? [team1, team2, team3];
  165. projects = projects ?? [project1, project2];
  166. ProjectsStore.loadInitialData(projects);
  167. const organization = OrganizationFixture({
  168. teams,
  169. projects,
  170. });
  171. if (isOrgOwner !== undefined && !isOrgOwner) {
  172. organization.access = organization.access.filter(scope => scope !== 'org:admin');
  173. }
  174. const context = RouterContextFixture([{organization}]);
  175. TeamStore.loadInitialData(teams, false, null);
  176. MockApiClient.addMockResponse({
  177. url: `/organizations/${organization.slug}/events/`,
  178. body: [],
  179. });
  180. return render(<TeamStatsHealth {...routerProps} />, {
  181. context,
  182. organization,
  183. });
  184. }
  185. it('defaults to first team', async () => {
  186. createWrapper();
  187. expect(await screen.findByText('#backend')).toBeInTheDocument();
  188. expect(screen.getByText('Key transaction')).toBeInTheDocument();
  189. });
  190. it('allows team switching as non-owner', async () => {
  191. createWrapper({isOrgOwner: false});
  192. expect(screen.getByText('#backend')).toBeInTheDocument();
  193. await userEvent.type(screen.getByText('#backend'), '{mouseDown}');
  194. expect(screen.getByText('#frontend')).toBeInTheDocument();
  195. // Teams user is not a member of are hidden
  196. expect(screen.queryByText('#internal')).not.toBeInTheDocument();
  197. await userEvent.click(screen.getByText('#frontend'));
  198. expect(router.push).toHaveBeenCalledWith(
  199. expect.objectContaining({query: {team: team1.id}})
  200. );
  201. expect(localStorage.setItem).toHaveBeenCalledWith(
  202. 'teamInsightsSelectedTeamId:org-slug',
  203. team1.id
  204. );
  205. });
  206. it('allows team switching as owner', async () => {
  207. createWrapper();
  208. expect(screen.getByText('#backend')).toBeInTheDocument();
  209. await userEvent.type(screen.getByText('#backend'), '{mouseDown}');
  210. expect(screen.getByText('#frontend')).toBeInTheDocument();
  211. // Org owners can see all teams including ones they are not members of
  212. expect(screen.queryByText('#internal')).toBeInTheDocument();
  213. await userEvent.click(screen.getByText('#internal'));
  214. expect(router.push).toHaveBeenCalledWith(
  215. expect.objectContaining({query: {team: team3.id}})
  216. );
  217. expect(localStorage.setItem).toHaveBeenCalledWith(
  218. 'teamInsightsSelectedTeamId:org-slug',
  219. team3.id
  220. );
  221. });
  222. it('superusers can switch to any team', async () => {
  223. jest.mocked(isActiveSuperuser).mockReturnValue(true);
  224. createWrapper();
  225. expect(screen.getByText('#backend')).toBeInTheDocument();
  226. await userEvent.type(screen.getByText('#backend'), '{mouseDown}');
  227. expect(screen.getByText('#frontend')).toBeInTheDocument();
  228. // User is not a member of internal team
  229. expect(screen.getByText('#internal')).toBeInTheDocument();
  230. });
  231. it('shows users with no teams the join team button', () => {
  232. createWrapper({
  233. projects: [{...project1, isMember: false}],
  234. teams: [],
  235. });
  236. expect(screen.getByText('Join a Team')).toBeInTheDocument();
  237. });
  238. });