groupReplays.spec.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {render, screen, waitFor} from 'sentry-test/reactTestingLibrary';
  3. import ProjectsStore from 'sentry/stores/projectsStore';
  4. import GroupReplays from 'sentry/views/issueDetails/groupReplays';
  5. jest.mock('sentry/utils/useMedia', () => ({
  6. __esModule: true,
  7. default: jest.fn(() => true),
  8. }));
  9. const mockReplayCountUrl = '/organizations/org-slug/replay-count/';
  10. const mockReplayUrl = '/organizations/org-slug/replays/';
  11. type InitializeOrgProps = {
  12. organizationProps?: {
  13. features?: string[];
  14. };
  15. };
  16. const REPLAY_ID_1 = '346789a703f6454384f1de473b8b9fcc';
  17. const REPLAY_ID_2 = 'b05dae9b6be54d21a4d5ad9f8f02b780';
  18. function init({organizationProps = {features: ['session-replay']}}: InitializeOrgProps) {
  19. const mockProject = TestStubs.Project();
  20. const {router, organization, routerContext} = initializeOrg({
  21. organization: {
  22. ...organizationProps,
  23. },
  24. project: mockProject,
  25. projects: [mockProject],
  26. router: {
  27. routes: [
  28. {path: '/'},
  29. {path: '/organizations/:orgId/issues/:groupId/'},
  30. {path: 'replays/'},
  31. ],
  32. location: {
  33. pathname: '/organizations/org-slug/replays/',
  34. query: {},
  35. },
  36. },
  37. });
  38. ProjectsStore.init();
  39. ProjectsStore.loadInitialData(organization.projects);
  40. return {router, organization, routerContext};
  41. }
  42. describe('GroupReplays', () => {
  43. beforeEach(() => {
  44. MockApiClient.clearMockResponses();
  45. MockApiClient.addMockResponse({
  46. method: 'GET',
  47. url: `/organizations/org-slug/sdk-updates/`,
  48. body: [],
  49. });
  50. });
  51. describe('Replay Feature Disabled', () => {
  52. const mockGroup = TestStubs.Group();
  53. const {router, organization, routerContext} = init({
  54. organizationProps: {features: []},
  55. });
  56. it("should show a message when the organization doesn't have access to the replay feature", () => {
  57. render(<GroupReplays group={mockGroup} />, {
  58. context: routerContext,
  59. organization,
  60. router,
  61. });
  62. expect(
  63. screen.getByText("You don't have access to this feature")
  64. ).toBeInTheDocument();
  65. });
  66. });
  67. describe('Replay Feature Enabled', () => {
  68. const {router, organization, routerContext} = init({});
  69. it('should query the replay-count endpoint with the fetched replayIds', async () => {
  70. const mockGroup = TestStubs.Group();
  71. const mockReplayCountApi = MockApiClient.addMockResponse({
  72. url: mockReplayCountUrl,
  73. body: {
  74. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  75. },
  76. });
  77. const mockReplayApi = MockApiClient.addMockResponse({
  78. url: mockReplayUrl,
  79. body: {
  80. data: [],
  81. },
  82. });
  83. render(<GroupReplays group={mockGroup} />, {
  84. context: routerContext,
  85. organization,
  86. router,
  87. });
  88. await waitFor(() => {
  89. expect(mockReplayCountApi).toHaveBeenCalledWith(
  90. mockReplayCountUrl,
  91. expect.objectContaining({
  92. query: {
  93. returnIds: true,
  94. query: `issue.id:[${mockGroup.id}]`,
  95. statsPeriod: '14d',
  96. project: -1,
  97. },
  98. })
  99. );
  100. // Expect api path to have the correct query params
  101. expect(mockReplayApi).toHaveBeenCalledWith(
  102. mockReplayUrl,
  103. expect.objectContaining({
  104. query: expect.objectContaining({
  105. environment: [],
  106. field: [
  107. 'activity',
  108. 'browser',
  109. 'count_dead_clicks',
  110. 'count_errors',
  111. 'count_rage_clicks',
  112. 'duration',
  113. 'finished_at',
  114. 'id',
  115. 'is_archived',
  116. 'os',
  117. 'project_id',
  118. 'started_at',
  119. 'urls',
  120. 'user',
  121. ],
  122. per_page: 50,
  123. project: -1,
  124. queryReferrer: 'issueReplays',
  125. query: `id:[${REPLAY_ID_1},${REPLAY_ID_2}]`,
  126. sort: '-started_at',
  127. statsPeriod: '14d',
  128. }),
  129. })
  130. );
  131. });
  132. });
  133. it('should show empty message when no replays are found', async () => {
  134. const mockGroup = TestStubs.Group();
  135. const mockReplayCountApi = MockApiClient.addMockResponse({
  136. url: mockReplayCountUrl,
  137. body: {
  138. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  139. },
  140. });
  141. const mockReplayApi = MockApiClient.addMockResponse({
  142. url: mockReplayUrl,
  143. body: {
  144. data: [],
  145. },
  146. });
  147. const {container} = render(<GroupReplays group={mockGroup} />, {
  148. context: routerContext,
  149. organization,
  150. router,
  151. });
  152. expect(
  153. await screen.findByText('There are no items to display')
  154. ).toBeInTheDocument();
  155. expect(mockReplayCountApi).toHaveBeenCalledTimes(1);
  156. expect(mockReplayApi).toHaveBeenCalledTimes(1);
  157. expect(container).toSnapshot();
  158. });
  159. it('should display error message when api call fails', async () => {
  160. const mockGroup = TestStubs.Group();
  161. const mockReplayCountApi = MockApiClient.addMockResponse({
  162. url: mockReplayCountUrl,
  163. body: {
  164. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  165. },
  166. });
  167. const mockReplayApi = MockApiClient.addMockResponse({
  168. url: mockReplayUrl,
  169. statusCode: 500,
  170. body: {
  171. detail: 'Invalid number: asdf. Expected number.',
  172. },
  173. });
  174. render(<GroupReplays group={mockGroup} />, {
  175. context: routerContext,
  176. organization,
  177. router,
  178. });
  179. await waitFor(() => {
  180. expect(mockReplayCountApi).toHaveBeenCalledTimes(1);
  181. expect(mockReplayApi).toHaveBeenCalledTimes(1);
  182. expect(
  183. screen.getByText('Invalid number: asdf. Expected number.')
  184. ).toBeInTheDocument();
  185. });
  186. });
  187. it('should display default error message when api call fails without a body', async () => {
  188. const mockGroup = TestStubs.Group();
  189. const mockReplayCountApi = MockApiClient.addMockResponse({
  190. url: mockReplayCountUrl,
  191. body: {
  192. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  193. },
  194. });
  195. const mockReplayApi = MockApiClient.addMockResponse({
  196. url: mockReplayUrl,
  197. statusCode: 500,
  198. body: {},
  199. });
  200. render(<GroupReplays group={mockGroup} />, {
  201. context: routerContext,
  202. organization,
  203. router,
  204. });
  205. await waitFor(() => {
  206. expect(mockReplayCountApi).toHaveBeenCalledTimes(1);
  207. expect(mockReplayApi).toHaveBeenCalledTimes(1);
  208. expect(
  209. screen.getByText(
  210. 'Sorry, the list of replays could not be loaded. This could be due to invalid search parameters or an internal systems error.'
  211. )
  212. ).toBeInTheDocument();
  213. });
  214. });
  215. it('should show loading indicator when loading replays', async () => {
  216. const mockGroup = TestStubs.Group();
  217. const mockReplayCountApi = MockApiClient.addMockResponse({
  218. url: mockReplayCountUrl,
  219. body: {
  220. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  221. },
  222. });
  223. const mockReplayApi = MockApiClient.addMockResponse({
  224. url: mockReplayUrl,
  225. statusCode: 200,
  226. body: {
  227. data: [],
  228. },
  229. });
  230. render(<GroupReplays group={mockGroup} />, {
  231. context: routerContext,
  232. organization,
  233. router,
  234. });
  235. expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
  236. await waitFor(() => {
  237. expect(mockReplayCountApi).toHaveBeenCalledTimes(1);
  238. expect(mockReplayApi).toHaveBeenCalledTimes(1);
  239. });
  240. });
  241. it('should show a list of replays and have the correct values', async () => {
  242. const mockGroup = TestStubs.Group();
  243. const mockReplayCountApi = MockApiClient.addMockResponse({
  244. url: mockReplayCountUrl,
  245. body: {
  246. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  247. },
  248. });
  249. const mockReplayApi = MockApiClient.addMockResponse({
  250. url: mockReplayUrl,
  251. statusCode: 200,
  252. body: {
  253. data: [
  254. {
  255. ...TestStubs.ReplayList()[0],
  256. count_errors: 1,
  257. duration: 52346,
  258. finished_at: new Date('2022-09-15T06:54:00+00:00'),
  259. id: '346789a703f6454384f1de473b8b9fcc',
  260. started_at: new Date('2022-09-15T06:50:00+00:00'),
  261. urls: [
  262. 'https://dev.getsentry.net:7999/replays/',
  263. '/organizations/sentry-emerging-tech/replays/?project=2',
  264. ],
  265. },
  266. {
  267. ...TestStubs.ReplayList()[0],
  268. count_errors: 4,
  269. duration: 400,
  270. finished_at: new Date('2022-09-21T21:40:38+00:00'),
  271. id: 'b05dae9b6be54d21a4d5ad9f8f02b780',
  272. started_at: new Date('2022-09-21T21:30:44+00:00'),
  273. urls: [
  274. 'https://dev.getsentry.net:7999/organizations/sentry-emerging-tech/replays/?project=2&statsPeriod=24h',
  275. '/organizations/sentry-emerging-tech/issues/',
  276. '/organizations/sentry-emerging-tech/issues/?project=2',
  277. ],
  278. },
  279. ].map(hydrated => ({
  280. ...hydrated,
  281. started_at: hydrated.started_at.toString(),
  282. finished_at: hydrated.finished_at.toString(),
  283. })),
  284. },
  285. });
  286. // Mock the system date to be 2022-09-28
  287. jest.useFakeTimers().setSystemTime(new Date('Sep 28, 2022 11:29:13 PM UTC'));
  288. render(<GroupReplays group={mockGroup} />, {
  289. context: routerContext,
  290. organization,
  291. router,
  292. });
  293. await waitFor(() => {
  294. expect(mockReplayCountApi).toHaveBeenCalledTimes(1);
  295. expect(mockReplayApi).toHaveBeenCalledTimes(1);
  296. });
  297. // Expect the table to have 2 rows
  298. expect(screen.getAllByText('testDisplayName')).toHaveLength(2);
  299. const expectedQuery =
  300. 'query=&referrer=%2Forganizations%2F%3AorgId%2Fissues%2F%3AgroupId%2Freplays%2F&statsPeriod=14d&yAxis=count%28%29';
  301. // Expect the first row to have the correct href
  302. expect(screen.getAllByRole('link', {name: 'testDisplayName'})[0]).toHaveAttribute(
  303. 'href',
  304. `/organizations/org-slug/replays/${REPLAY_ID_1}/?${expectedQuery}`
  305. );
  306. // Expect the second row to have the correct href
  307. expect(screen.getAllByRole('link', {name: 'testDisplayName'})[1]).toHaveAttribute(
  308. 'href',
  309. `/organizations/org-slug/replays/${REPLAY_ID_2}/?${expectedQuery}`
  310. );
  311. // Expect the first row to have the correct duration
  312. expect(screen.getByText('14:32:26')).toBeInTheDocument();
  313. // Expect the second row to have the correct duration
  314. expect(screen.getByText('06:40')).toBeInTheDocument();
  315. // Expect the first row to have the correct errors
  316. expect(screen.getAllByTestId('replay-table-count-errors')[0]).toHaveTextContent(
  317. '1'
  318. );
  319. // Expect the second row to have the correct errors
  320. expect(screen.getAllByTestId('replay-table-count-errors')[1]).toHaveTextContent(
  321. '4'
  322. );
  323. // Expect the first row to have the correct date
  324. expect(screen.getByText('14 days ago')).toBeInTheDocument();
  325. // Expect the second row to have the correct date
  326. expect(screen.getByText('7 days ago')).toBeInTheDocument();
  327. });
  328. });
  329. });