health.spec.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 {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. 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. beforeEach(() => {
  154. TeamStore.reset();
  155. });
  156. afterEach(() => {
  157. jest.resetAllMocks();
  158. });
  159. function createWrapper({
  160. projects,
  161. teams,
  162. }: {projects?: Project[]; teams?: TeamType[]} = {}) {
  163. teams = teams ?? [team1, team2, team3];
  164. projects = projects ?? [project1, project2];
  165. ProjectsStore.loadInitialData(projects);
  166. const organization = OrganizationFixture({
  167. teams,
  168. projects,
  169. });
  170. const context = RouterContextFixture([{organization}]);
  171. TeamStore.loadInitialData(teams, false, null);
  172. MockApiClient.addMockResponse({
  173. url: `/organizations/${organization.slug}/events/`,
  174. body: [],
  175. });
  176. return render(<TeamStatsHealth {...routerProps} />, {
  177. context,
  178. organization,
  179. });
  180. }
  181. it('defaults to first team', () => {
  182. createWrapper();
  183. expect(screen.getByText('#backend')).toBeInTheDocument();
  184. expect(screen.getByText('Key transaction')).toBeInTheDocument();
  185. });
  186. it('allows team switching', async () => {
  187. createWrapper();
  188. expect(screen.getByText('#backend')).toBeInTheDocument();
  189. await userEvent.type(screen.getByText('#backend'), '{mouseDown}');
  190. expect(screen.getByText('#frontend')).toBeInTheDocument();
  191. // Teams user is not a member of are hidden
  192. expect(screen.queryByText('#internal')).not.toBeInTheDocument();
  193. await userEvent.click(screen.getByText('#frontend'));
  194. expect(router.push).toHaveBeenCalledWith(
  195. expect.objectContaining({query: {team: team1.id}})
  196. );
  197. expect(localStorage.setItem).toHaveBeenCalledWith(
  198. 'teamInsightsSelectedTeamId:org-slug',
  199. team1.id
  200. );
  201. });
  202. it('superusers can switch to any team', async () => {
  203. jest.mocked(isActiveSuperuser).mockReturnValue(true);
  204. createWrapper();
  205. expect(screen.getByText('#backend')).toBeInTheDocument();
  206. await userEvent.type(screen.getByText('#backend'), '{mouseDown}');
  207. expect(screen.getByText('#frontend')).toBeInTheDocument();
  208. // User is not a member of internal team
  209. expect(screen.getByText('#internal')).toBeInTheDocument();
  210. });
  211. it('shows users with no teams the join team button', () => {
  212. createWrapper({
  213. projects: [{...project1, isMember: false}],
  214. teams: [],
  215. });
  216. expect(screen.getByText('Join a Team')).toBeInTheDocument();
  217. });
  218. });