health.spec.tsx 6.8 KB

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