health.spec.tsx 6.8 KB

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