health.spec.tsx 6.9 KB

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