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. 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. 'user',
  122. ],
  123. per_page: 50,
  124. project: -1,
  125. queryReferrer: 'issueReplays',
  126. query: `id:[${REPLAY_ID_1},${REPLAY_ID_2}]`,
  127. sort: '-started_at',
  128. statsPeriod: '14d',
  129. }),
  130. })
  131. );
  132. });
  133. });
  134. it('should show empty message when no replays are found', async () => {
  135. const mockGroup = TestStubs.Group();
  136. const mockReplayCountApi = MockApiClient.addMockResponse({
  137. url: mockReplayCountUrl,
  138. body: {
  139. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  140. },
  141. });
  142. const mockReplayApi = MockApiClient.addMockResponse({
  143. url: mockReplayUrl,
  144. body: {
  145. data: [],
  146. },
  147. });
  148. render(<GroupReplays group={mockGroup} />, {
  149. context: routerContext,
  150. organization,
  151. router,
  152. });
  153. expect(
  154. await screen.findByText('There are no items to display')
  155. ).toBeInTheDocument();
  156. expect(mockReplayCountApi).toHaveBeenCalledTimes(1);
  157. expect(mockReplayApi).toHaveBeenCalledTimes(1);
  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. ...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. ...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. });