groupReplays.spec.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  3. import ProjectsStore from 'sentry/stores/projectsStore';
  4. import {OrganizationContext} from 'sentry/views/organizationContext';
  5. import GroupReplays from 'sentry/views/organizationGroupDetails/groupReplays';
  6. import {RouteContext} from 'sentry/views/routeContext';
  7. type InitializeOrgProps = {
  8. location?: {
  9. pathname?: string;
  10. query?: {[key: string]: string};
  11. };
  12. organizationProps?: {
  13. features?: string[];
  14. };
  15. };
  16. jest.mock('sentry/utils/useMedia', () => ({
  17. __esModule: true,
  18. default: jest.fn(() => true),
  19. }));
  20. const mockUrl = '/organizations/org-slug/replays/';
  21. const mockProps = {
  22. group: TestStubs.Group(),
  23. replayIds: ['346789a703f6454384f1de473b8b9fcc', 'b05dae9b6be54d21a4d5ad9f8f02b780'],
  24. };
  25. let mockRouterContext: {
  26. childContextTypes?: any;
  27. context?: any;
  28. } = {};
  29. const getComponent = ({
  30. location,
  31. organizationProps = {features: ['session-replay-ui']},
  32. }: InitializeOrgProps) => {
  33. const {router, organization, routerContext} = initializeOrg({
  34. organization: {
  35. ...organizationProps,
  36. },
  37. project: TestStubs.Project(),
  38. projects: [TestStubs.Project()],
  39. router: {
  40. routes: [
  41. {path: '/'},
  42. {path: '/organizations/:orgId/issues/:groupId/'},
  43. {path: 'replays/'},
  44. ],
  45. location: {
  46. pathname: '/organizations/org-slug/replays/',
  47. query: {},
  48. ...location,
  49. },
  50. },
  51. });
  52. ProjectsStore.init();
  53. ProjectsStore.loadInitialData(organization.projects);
  54. mockRouterContext = routerContext;
  55. return (
  56. <OrganizationContext.Provider value={organization}>
  57. <RouteContext.Provider
  58. value={{
  59. router,
  60. location: router.location,
  61. params: router.params,
  62. routes: router.routes,
  63. }}
  64. >
  65. <GroupReplays {...mockProps} />
  66. </RouteContext.Provider>
  67. </OrganizationContext.Provider>
  68. );
  69. };
  70. const renderComponent = (componentProps: InitializeOrgProps = {}) => {
  71. return render(getComponent(componentProps), {context: mockRouterContext});
  72. };
  73. describe('GroupReplays', () => {
  74. beforeEach(() => {
  75. MockApiClient.clearMockResponses();
  76. });
  77. it('should query the events endpoint with the passed in replayIds', async () => {
  78. const mockApi = MockApiClient.addMockResponse({
  79. url: mockUrl,
  80. body: {
  81. data: [],
  82. },
  83. statusCode: 200,
  84. });
  85. renderComponent();
  86. await waitFor(() => {
  87. expect(mockApi).toHaveBeenCalledTimes(1);
  88. // Expect api path to have the correct query params
  89. expect(mockApi).toHaveBeenCalledWith(
  90. mockUrl,
  91. expect.objectContaining({
  92. query: expect.objectContaining({
  93. statsPeriod: '14d',
  94. project: ['2'],
  95. environment: [],
  96. field: [
  97. 'activity',
  98. 'countErrors',
  99. 'duration',
  100. 'finishedAt',
  101. 'id',
  102. 'projectId',
  103. 'startedAt',
  104. 'urls',
  105. 'user',
  106. ],
  107. sort: '-startedAt',
  108. per_page: 50,
  109. query:
  110. 'id:[346789a703f6454384f1de473b8b9fcc,b05dae9b6be54d21a4d5ad9f8f02b780]',
  111. }),
  112. })
  113. );
  114. });
  115. });
  116. it('should show empty message when no replays are found', async () => {
  117. const mockApi = MockApiClient.addMockResponse({
  118. url: mockUrl,
  119. body: {
  120. data: [],
  121. },
  122. statusCode: 200,
  123. });
  124. const {container} = renderComponent();
  125. expect(await screen.findByText('There are no items to display')).toBeInTheDocument();
  126. expect(mockApi).toHaveBeenCalledTimes(1);
  127. expect(container).toSnapshot();
  128. });
  129. it('should display error message when api call fails', async () => {
  130. const mockApi = MockApiClient.addMockResponse({
  131. url: mockUrl,
  132. statusCode: 500,
  133. body: {
  134. detail: 'Invalid number: asdf. Expected number.',
  135. },
  136. });
  137. renderComponent();
  138. await waitFor(() => {
  139. expect(mockApi).toHaveBeenCalledTimes(1);
  140. expect(
  141. screen.getByText('Invalid number: asdf. Expected number.')
  142. ).toBeInTheDocument();
  143. });
  144. });
  145. it('should display default error message when api call fails without a body', async () => {
  146. const mockApi = MockApiClient.addMockResponse({
  147. url: mockUrl,
  148. statusCode: 500,
  149. body: {},
  150. });
  151. renderComponent();
  152. await waitFor(() => {
  153. expect(mockApi).toHaveBeenCalledTimes(1);
  154. expect(
  155. screen.getByText(
  156. 'Sorry, the list of replays could not be loaded. This could be due to invalid search parameters or an internal systems error.'
  157. )
  158. ).toBeInTheDocument();
  159. });
  160. });
  161. it('should show loading indicator when loading replays', async () => {
  162. const mockApi = MockApiClient.addMockResponse({
  163. url: mockUrl,
  164. statusCode: 200,
  165. body: {
  166. data: [],
  167. },
  168. });
  169. renderComponent();
  170. expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
  171. await waitFor(() => {
  172. expect(mockApi).toHaveBeenCalledTimes(1);
  173. });
  174. });
  175. it('should show a list of replays and have the correct values', async () => {
  176. const mockApi = MockApiClient.addMockResponse({
  177. url: mockUrl,
  178. statusCode: 200,
  179. body: {
  180. data: [
  181. {
  182. countErrors: 1,
  183. duration: 52346,
  184. finishedAt: '2022-09-15T06:54:00+00:00',
  185. id: '346789a703f6454384f1de473b8b9fcc',
  186. projectId: '2',
  187. startedAt: '2022-09-15T06:50:03+00:00',
  188. urls: [
  189. 'https://dev.getsentry.net:7999/organizations/sentry-emerging-tech/replays/',
  190. '/organizations/sentry-emerging-tech/replays/?project=2',
  191. ],
  192. user: {
  193. id: '147086',
  194. name: '',
  195. email: '',
  196. ip_address: '127.0.0.1',
  197. displayName: 'testDisplayName',
  198. },
  199. },
  200. {
  201. countErrors: 4,
  202. duration: 400,
  203. finishedAt: '2022-09-21T21:40:38+00:00',
  204. id: 'b05dae9b6be54d21a4d5ad9f8f02b780',
  205. projectId: '2',
  206. startedAt: '2022-09-21T21:30:44+00:00',
  207. urls: [
  208. 'https://dev.getsentry.net:7999/organizations/sentry-emerging-tech/replays/?project=2&statsPeriod=24h',
  209. '/organizations/sentry-emerging-tech/issues/',
  210. '/organizations/sentry-emerging-tech/issues/?project=2',
  211. ],
  212. user: {
  213. id: '147086',
  214. name: '',
  215. email: '',
  216. ip_address: '127.0.0.1',
  217. displayName: 'testDisplayName',
  218. },
  219. },
  220. ],
  221. },
  222. });
  223. // Mock the system date to be 2022-09-28
  224. jest.useFakeTimers().setSystemTime(new Date('Sep 28, 2022 11:29:13 PM UTC'));
  225. renderComponent();
  226. await waitFor(() => {
  227. expect(mockApi).toHaveBeenCalledTimes(1);
  228. });
  229. // Expect the table to have 2 rows
  230. expect(screen.getAllByText('testDisplayName')).toHaveLength(2);
  231. // Expect the first row to have the correct href
  232. expect(screen.getAllByRole('link', {name: 'testDisplayName'})[0]).toHaveAttribute(
  233. 'href',
  234. '/organizations/org-slug/replays/project-slug:346789a703f6454384f1de473b8b9fcc/?referrer=%2Forganizations%2F%3AorgId%2Fissues%2F%3AgroupId%2Freplays%2F'
  235. );
  236. // Expect the second row to have the correct href
  237. expect(screen.getAllByRole('link', {name: 'testDisplayName'})[1]).toHaveAttribute(
  238. 'href',
  239. '/organizations/org-slug/replays/project-slug:b05dae9b6be54d21a4d5ad9f8f02b780/?referrer=%2Forganizations%2F%3AorgId%2Fissues%2F%3AgroupId%2Freplays%2F'
  240. );
  241. // Expect the first row to have the correct duration
  242. expect(screen.getByText('14hr 32min 26s')).toBeInTheDocument();
  243. // Expect the second row to have the correct duration
  244. expect(screen.getByText('6min 40s')).toBeInTheDocument();
  245. // Expect the first row to have the correct errors
  246. expect(screen.getAllByTestId('replay-table-count-errors')[0]).toHaveTextContent('1');
  247. // Expect the second row to have the correct errors
  248. expect(screen.getAllByTestId('replay-table-count-errors')[1]).toHaveTextContent('4');
  249. // Expect the first row to have the correct date
  250. expect(screen.getByText('14 days ago')).toBeInTheDocument();
  251. // Expect the second row to have the correct date
  252. expect(screen.getByText('7 days ago')).toBeInTheDocument();
  253. });
  254. it('should be able to click the `Start Time` column and request data sorted by startedAt query', async () => {
  255. const mockApi = MockApiClient.addMockResponse({
  256. url: mockUrl,
  257. body: {
  258. data: [],
  259. },
  260. statusCode: 200,
  261. });
  262. const {rerender} = renderComponent();
  263. await waitFor(() => {
  264. expect(mockApi).toHaveBeenCalledWith(
  265. mockUrl,
  266. expect.objectContaining({
  267. query: expect.objectContaining({
  268. sort: '-startedAt',
  269. }),
  270. })
  271. );
  272. });
  273. // Click on the start time header and expect the sort to be startedAt
  274. userEvent.click(screen.getByRole('columnheader', {name: 'Start Time'}));
  275. expect(mockRouterContext.context.router.push).toHaveBeenCalledWith({
  276. pathname: '/organizations/org-slug/replays/',
  277. query: {
  278. sort: 'startedAt',
  279. },
  280. });
  281. // Need to simulate a rerender to get the new sort
  282. rerender(
  283. getComponent({
  284. location: {
  285. query: {
  286. sort: 'startedAt',
  287. },
  288. },
  289. })
  290. );
  291. await waitFor(() => {
  292. expect(mockApi).toHaveBeenCalledTimes(2);
  293. expect(mockApi).toHaveBeenCalledWith(
  294. mockUrl,
  295. expect.objectContaining({
  296. query: expect.objectContaining({
  297. sort: 'startedAt',
  298. }),
  299. })
  300. );
  301. });
  302. });
  303. it('should be able to click the `Duration` column and request data sorted by duration query', async () => {
  304. const mockApi = MockApiClient.addMockResponse({
  305. url: mockUrl,
  306. body: {
  307. data: [],
  308. },
  309. statusCode: 200,
  310. });
  311. const {rerender} = renderComponent();
  312. await waitFor(() => {
  313. expect(mockApi).toHaveBeenCalledWith(
  314. mockUrl,
  315. expect.objectContaining({
  316. query: expect.objectContaining({
  317. sort: '-startedAt',
  318. }),
  319. })
  320. );
  321. });
  322. // Click on the duration header and expect the sort to be duration
  323. userEvent.click(screen.getByRole('columnheader', {name: 'Duration'}));
  324. expect(mockRouterContext.context.router.push).toHaveBeenCalledWith({
  325. pathname: '/organizations/org-slug/replays/',
  326. query: {
  327. sort: '-duration',
  328. },
  329. });
  330. // Need to simulate a rerender to get the new sort
  331. rerender(
  332. getComponent({
  333. location: {
  334. query: {
  335. sort: '-duration',
  336. },
  337. },
  338. })
  339. );
  340. await waitFor(() => {
  341. expect(mockApi).toHaveBeenCalledTimes(2);
  342. expect(mockApi).toHaveBeenCalledWith(
  343. mockUrl,
  344. expect.objectContaining({
  345. query: expect.objectContaining({
  346. sort: '-duration',
  347. }),
  348. })
  349. );
  350. });
  351. });
  352. it('should be able to click the `Errors` column and request data sorted by countErrors query', async () => {
  353. const mockApi = MockApiClient.addMockResponse({
  354. url: mockUrl,
  355. body: {
  356. data: [],
  357. },
  358. statusCode: 200,
  359. });
  360. const {rerender} = renderComponent();
  361. await waitFor(() => {
  362. expect(mockApi).toHaveBeenCalledWith(
  363. mockUrl,
  364. expect.objectContaining({
  365. query: expect.objectContaining({
  366. sort: '-startedAt',
  367. }),
  368. })
  369. );
  370. });
  371. // Click on the errors header and expect the sort to be countErrors
  372. userEvent.click(screen.getByRole('columnheader', {name: 'Errors'}));
  373. expect(mockRouterContext.context.router.push).toHaveBeenCalledWith({
  374. pathname: '/organizations/org-slug/replays/',
  375. query: {
  376. sort: '-countErrors',
  377. },
  378. });
  379. // Need to simulate a rerender to get the new sort
  380. rerender(
  381. getComponent({
  382. location: {
  383. query: {
  384. sort: '-countErrors',
  385. },
  386. },
  387. })
  388. );
  389. await waitFor(() => {
  390. expect(mockApi).toHaveBeenCalledTimes(2);
  391. expect(mockApi).toHaveBeenCalledWith(
  392. mockUrl,
  393. expect.objectContaining({
  394. query: expect.objectContaining({
  395. sort: '-countErrors',
  396. }),
  397. })
  398. );
  399. });
  400. });
  401. it('should be able to click the `Activity` column and request data sorted by startedAt query', async () => {
  402. const mockApi = MockApiClient.addMockResponse({
  403. url: mockUrl,
  404. body: {
  405. data: [],
  406. },
  407. statusCode: 200,
  408. });
  409. const {rerender} = renderComponent();
  410. await waitFor(() => {
  411. expect(mockApi).toHaveBeenCalledWith(
  412. mockUrl,
  413. expect.objectContaining({
  414. query: expect.objectContaining({
  415. sort: '-startedAt',
  416. }),
  417. })
  418. );
  419. });
  420. // Click on the activity header and expect the sort to be activity
  421. userEvent.click(screen.getByRole('columnheader', {name: 'Activity'}));
  422. expect(mockRouterContext.context.router.push).toHaveBeenCalledWith({
  423. pathname: '/organizations/org-slug/replays/',
  424. query: {
  425. sort: '-activity',
  426. },
  427. });
  428. // Need to simulate a rerender to get the new sort
  429. rerender(
  430. getComponent({
  431. location: {
  432. query: {
  433. sort: '-activity',
  434. },
  435. },
  436. })
  437. );
  438. await waitFor(() => {
  439. expect(mockApi).toHaveBeenCalledTimes(2);
  440. expect(mockApi).toHaveBeenCalledWith(
  441. mockUrl,
  442. expect.objectContaining({
  443. query: expect.objectContaining({
  444. sort: '-activity',
  445. }),
  446. })
  447. );
  448. });
  449. });
  450. it("should show a message when the organization doesn't have access to the replay feature", () => {
  451. renderComponent({
  452. organizationProps: {
  453. features: [],
  454. },
  455. });
  456. expect(screen.getByText("You don't have access to this feature")).toBeInTheDocument();
  457. });
  458. });