health.spec.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {ProjectFixture} from 'sentry-fixture/project';
  3. import {TeamFixture} from 'sentry-fixture/team';
  4. import {TeamAlertsTriggeredFixture} from 'sentry-fixture/teamAlertsTriggered';
  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 TeamStatsHealth from 'sentry/views/organizationStats/teamInsights/health';
  14. jest.mock('sentry/utils/localStorage');
  15. jest.mock('sentry/utils/isActiveSuperuser', () => ({
  16. isActiveSuperuser: jest.fn(),
  17. }));
  18. describe('TeamStatsHealth', () => {
  19. const project1 = ProjectFixture({id: '2', name: 'js', slug: 'js'});
  20. const project2 = ProjectFixture({id: '3', name: 'py', slug: 'py'});
  21. const team1 = TeamFixture({
  22. id: '2',
  23. slug: 'frontend',
  24. name: 'frontend',
  25. projects: [project1],
  26. isMember: true,
  27. });
  28. const team2 = TeamFixture({
  29. id: '3',
  30. slug: 'backend',
  31. name: 'backend',
  32. projects: [project2],
  33. isMember: true,
  34. });
  35. const team3 = TeamFixture({
  36. id: '4',
  37. slug: 'internal',
  38. name: 'internal',
  39. projects: [],
  40. isMember: false,
  41. });
  42. const {routerProps, router} = initializeOrg();
  43. beforeEach(() => {
  44. TeamStore.reset();
  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. afterEach(() => {
  154. jest.resetAllMocks();
  155. });
  156. function createWrapper({
  157. projects,
  158. teams,
  159. isOrgOwner,
  160. }: {isOrgOwner?: boolean; projects?: Project[]; teams?: TeamType[]} = {}) {
  161. teams = teams ?? [team1, team2, team3];
  162. projects = projects ?? [project1, project2];
  163. ProjectsStore.loadInitialData(projects);
  164. const organization = OrganizationFixture();
  165. if (isOrgOwner !== undefined && !isOrgOwner) {
  166. organization.access = organization.access.filter(scope => scope !== 'org:admin');
  167. }
  168. TeamStore.loadInitialData(teams, false, null);
  169. MockApiClient.addMockResponse({
  170. url: `/organizations/${organization.slug}/events/`,
  171. body: [],
  172. });
  173. return render(<TeamStatsHealth {...routerProps} />, {
  174. organization,
  175. });
  176. }
  177. it('defaults to first team', async () => {
  178. createWrapper();
  179. expect(await screen.findByText('#backend')).toBeInTheDocument();
  180. expect(screen.getByText('Key transaction')).toBeInTheDocument();
  181. });
  182. it('allows team switching as non-owner', async () => {
  183. createWrapper({isOrgOwner: false});
  184. expect(screen.getByText('#backend')).toBeInTheDocument();
  185. await userEvent.type(screen.getByText('#backend'), '{mouseDown}');
  186. expect(screen.getByText('#frontend')).toBeInTheDocument();
  187. // Teams user is not a member of are hidden
  188. expect(screen.queryByText('#internal')).not.toBeInTheDocument();
  189. await userEvent.click(screen.getByText('#frontend'));
  190. expect(router.push).toHaveBeenCalledWith(
  191. expect.objectContaining({query: {team: team1.id}})
  192. );
  193. expect(localStorage.setItem).toHaveBeenCalledWith(
  194. 'teamInsightsSelectedTeamId:org-slug',
  195. team1.id
  196. );
  197. });
  198. it('allows team switching as owner', async () => {
  199. createWrapper();
  200. expect(screen.getByText('#backend')).toBeInTheDocument();
  201. await userEvent.type(screen.getByText('#backend'), '{mouseDown}');
  202. expect(screen.getByText('#frontend')).toBeInTheDocument();
  203. // Org owners can see all teams including ones they are not members of
  204. expect(screen.queryByText('#internal')).toBeInTheDocument();
  205. await userEvent.click(screen.getByText('#internal'));
  206. expect(router.push).toHaveBeenCalledWith(
  207. expect.objectContaining({query: {team: team3.id}})
  208. );
  209. expect(localStorage.setItem).toHaveBeenCalledWith(
  210. 'teamInsightsSelectedTeamId:org-slug',
  211. team3.id
  212. );
  213. });
  214. it('superusers can switch to any team', async () => {
  215. jest.mocked(isActiveSuperuser).mockReturnValue(true);
  216. createWrapper();
  217. expect(screen.getByText('#backend')).toBeInTheDocument();
  218. await userEvent.type(screen.getByText('#backend'), '{mouseDown}');
  219. expect(screen.getByText('#frontend')).toBeInTheDocument();
  220. // User is not a member of internal team
  221. expect(screen.getByText('#internal')).toBeInTheDocument();
  222. });
  223. it('shows users with no teams the join team button', () => {
  224. createWrapper({
  225. projects: [{...project1, isMember: false}],
  226. teams: [],
  227. });
  228. expect(screen.getByText('Join a Team')).toBeInTheDocument();
  229. });
  230. });