health.spec.tsx 6.7 KB

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