groupReplays.spec.tsx 11 KB

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