groupReplays.spec.tsx 11 KB

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