health.spec.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 {Team} from 'sentry/types/organization';
  11. import type {Project} from 'sentry/types/project';
  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?: Team[]} = {}) {
  162. teams = teams ?? [team1, team2, team3];
  163. projects = projects ?? [project1, project2];
  164. ProjectsStore.loadInitialData(projects);
  165. const organization = OrganizationFixture();
  166. if (isOrgOwner !== undefined && !isOrgOwner) {
  167. organization.access = organization.access.filter(scope => scope !== 'org:admin');
  168. }
  169. TeamStore.loadInitialData(teams, false, null);
  170. MockApiClient.addMockResponse({
  171. url: `/organizations/${organization.slug}/events/`,
  172. body: [],
  173. });
  174. return render(<TeamStatsHealth {...routerProps} />, {
  175. organization,
  176. });
  177. }
  178. it('defaults to first team', async () => {
  179. createWrapper();
  180. expect(await screen.findByText('#backend')).toBeInTheDocument();
  181. expect(screen.getByText('Key transaction')).toBeInTheDocument();
  182. });
  183. it('allows team switching as non-owner', async () => {
  184. createWrapper({isOrgOwner: false});
  185. expect(screen.getByText('#backend')).toBeInTheDocument();
  186. await userEvent.type(screen.getByText('#backend'), '{mouseDown}');
  187. expect(screen.getByText('#frontend')).toBeInTheDocument();
  188. // Teams user is not a member of are hidden
  189. expect(screen.queryByText('#internal')).not.toBeInTheDocument();
  190. await userEvent.click(screen.getByText('#frontend'));
  191. expect(router.push).toHaveBeenCalledWith(
  192. expect.objectContaining({query: {team: team1.id}})
  193. );
  194. expect(localStorage.setItem).toHaveBeenCalledWith(
  195. 'teamInsightsSelectedTeamId:org-slug',
  196. team1.id
  197. );
  198. });
  199. it('allows team switching as owner', async () => {
  200. createWrapper();
  201. expect(screen.getByText('#backend')).toBeInTheDocument();
  202. await userEvent.type(screen.getByText('#backend'), '{mouseDown}');
  203. expect(screen.getByText('#frontend')).toBeInTheDocument();
  204. // Org owners can see all teams including ones they are not members of
  205. expect(screen.queryByText('#internal')).toBeInTheDocument();
  206. await userEvent.click(screen.getByText('#internal'));
  207. expect(router.push).toHaveBeenCalledWith(
  208. expect.objectContaining({query: {team: team3.id}})
  209. );
  210. expect(localStorage.setItem).toHaveBeenCalledWith(
  211. 'teamInsightsSelectedTeamId:org-slug',
  212. team3.id
  213. );
  214. });
  215. it('superusers can switch to any team', async () => {
  216. jest.mocked(isActiveSuperuser).mockReturnValue(true);
  217. createWrapper();
  218. expect(screen.getByText('#backend')).toBeInTheDocument();
  219. await userEvent.type(screen.getByText('#backend'), '{mouseDown}');
  220. expect(screen.getByText('#frontend')).toBeInTheDocument();
  221. // User is not a member of internal team
  222. expect(screen.getByText('#internal')).toBeInTheDocument();
  223. });
  224. it('shows users with no teams the join team button', () => {
  225. createWrapper({
  226. projects: [{...project1, isMember: false}],
  227. teams: [],
  228. });
  229. expect(screen.getByText('Join a Team')).toBeInTheDocument();
  230. });
  231. });