health.spec.tsx 8.0 KB

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