groupReplays.spec.tsx 11 KB

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