groupReplays.spec.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. 'countErrors',
  98. 'duration',
  99. 'finishedAt',
  100. 'id',
  101. 'projectId',
  102. 'startedAt',
  103. 'urls',
  104. 'user',
  105. ],
  106. sort: '-startedAt',
  107. per_page: 50,
  108. query:
  109. 'id:[346789a703f6454384f1de473b8b9fcc,b05dae9b6be54d21a4d5ad9f8f02b780]',
  110. }),
  111. })
  112. );
  113. });
  114. });
  115. it('should snapshot empty state', async () => {
  116. MockApiClient.addMockResponse({
  117. url: mockUrl,
  118. body: {
  119. data: [],
  120. },
  121. statusCode: 200,
  122. });
  123. const {container} = renderComponent();
  124. await waitFor(() => {
  125. expect(container).toSnapshot();
  126. });
  127. });
  128. it('should show empty message when no replays are found', async () => {
  129. const mockApi = MockApiClient.addMockResponse({
  130. url: mockUrl,
  131. body: {
  132. data: [],
  133. },
  134. statusCode: 200,
  135. });
  136. renderComponent();
  137. await waitFor(() => {
  138. expect(mockApi).toHaveBeenCalledTimes(1);
  139. expect(screen.getByText('There are no items to display')).toBeInTheDocument();
  140. });
  141. });
  142. it('should display error message when api call fails', async () => {
  143. const mockApi = MockApiClient.addMockResponse({
  144. url: mockUrl,
  145. statusCode: 500,
  146. body: {
  147. detail: 'Invalid number: asdf. Expected number.',
  148. },
  149. });
  150. renderComponent();
  151. await waitFor(() => {
  152. expect(mockApi).toHaveBeenCalledTimes(1);
  153. expect(
  154. screen.getByText('Invalid number: asdf. Expected number.')
  155. ).toBeInTheDocument();
  156. });
  157. });
  158. it('should display default error message when api call fails without a body', async () => {
  159. const mockApi = MockApiClient.addMockResponse({
  160. url: mockUrl,
  161. statusCode: 500,
  162. body: {},
  163. });
  164. renderComponent();
  165. await waitFor(() => {
  166. expect(mockApi).toHaveBeenCalledTimes(1);
  167. expect(
  168. screen.getByText(
  169. 'Sorry, the list of replays could not be loaded. This could be due to invalid search parameters or an internal systems error.'
  170. )
  171. ).toBeInTheDocument();
  172. });
  173. });
  174. it('should show loading indicator when loading replays', async () => {
  175. const mockApi = MockApiClient.addMockResponse({
  176. url: mockUrl,
  177. statusCode: 200,
  178. body: {
  179. data: [],
  180. },
  181. });
  182. renderComponent();
  183. expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
  184. await waitFor(() => {
  185. expect(mockApi).toHaveBeenCalledTimes(1);
  186. });
  187. });
  188. it('should show a list of replays and have the correct values', async () => {
  189. const mockApi = MockApiClient.addMockResponse({
  190. url: mockUrl,
  191. statusCode: 200,
  192. body: {
  193. data: [
  194. {
  195. countErrors: 1,
  196. duration: 52346,
  197. finishedAt: '2022-09-15T06:54:00+00:00',
  198. id: '346789a703f6454384f1de473b8b9fcc',
  199. projectId: '2',
  200. startedAt: '2022-09-15T06:50:03+00:00',
  201. urls: [
  202. 'https://dev.getsentry.net:7999/organizations/sentry-emerging-tech/replays/',
  203. '/organizations/sentry-emerging-tech/replays/?project=2',
  204. ],
  205. user: {
  206. id: '147086',
  207. name: '',
  208. email: '',
  209. ip_address: '127.0.0.1',
  210. displayName: 'testDisplayName',
  211. },
  212. },
  213. {
  214. countErrors: 4,
  215. duration: 400,
  216. finishedAt: '2022-09-21T21:40:38+00:00',
  217. id: 'b05dae9b6be54d21a4d5ad9f8f02b780',
  218. projectId: '2',
  219. startedAt: '2022-09-21T21:30:44+00:00',
  220. urls: [
  221. 'https://dev.getsentry.net:7999/organizations/sentry-emerging-tech/replays/?project=2&statsPeriod=24h',
  222. '/organizations/sentry-emerging-tech/issues/',
  223. '/organizations/sentry-emerging-tech/issues/?project=2',
  224. ],
  225. user: {
  226. id: '147086',
  227. name: '',
  228. email: '',
  229. ip_address: '127.0.0.1',
  230. displayName: 'testDisplayName',
  231. },
  232. },
  233. ],
  234. },
  235. });
  236. // Mock the system date to be 2022-09-28
  237. jest.useFakeTimers().setSystemTime(new Date('Sep 28, 2022 11:29:13 PM UTC'));
  238. renderComponent();
  239. await waitFor(() => {
  240. expect(mockApi).toHaveBeenCalledTimes(1);
  241. });
  242. // Expect the table to have 2 rows
  243. expect(screen.getAllByText('testDisplayName')).toHaveLength(2);
  244. // Expect the first row to have the correct href
  245. expect(screen.getAllByRole('link', {name: 'testDisplayName'})[0]).toHaveAttribute(
  246. 'href',
  247. '/organizations/org-slug/replays/project-slug:346789a703f6454384f1de473b8b9fcc/?referrer=%2Forganizations%2F%3AorgId%2Fissues%2F%3AgroupId%2Freplays%2F'
  248. );
  249. // Expect the second row to have the correct href
  250. expect(screen.getAllByRole('link', {name: 'testDisplayName'})[1]).toHaveAttribute(
  251. 'href',
  252. '/organizations/org-slug/replays/project-slug:b05dae9b6be54d21a4d5ad9f8f02b780/?referrer=%2Forganizations%2F%3AorgId%2Fissues%2F%3AgroupId%2Freplays%2F'
  253. );
  254. // Expect the first row to have the correct duration
  255. expect(screen.getByText('14hr 32min 26s')).toBeInTheDocument();
  256. // Expect the second row to have the correct duration
  257. expect(screen.getByText('6min 40s')).toBeInTheDocument();
  258. // Expect the first row to have the correct errors
  259. expect(screen.getAllByTestId('replay-table-count-errors')[0]).toHaveTextContent('1');
  260. // Expect the second row to have the correct errors
  261. expect(screen.getAllByTestId('replay-table-count-errors')[1]).toHaveTextContent('4');
  262. // Expect the first row to have the correct date
  263. expect(screen.getByText('14 days ago')).toBeInTheDocument();
  264. // Expect the second row to have the correct date
  265. expect(screen.getByText('7 days ago')).toBeInTheDocument();
  266. });
  267. it('should be able to click the `Start Time` column and request data sorted by startedAt query', async () => {
  268. const mockApi = MockApiClient.addMockResponse({
  269. url: mockUrl,
  270. body: {
  271. data: [],
  272. },
  273. statusCode: 200,
  274. });
  275. const {rerender} = renderComponent();
  276. await waitFor(() => {
  277. expect(mockApi).toHaveBeenCalledWith(
  278. mockUrl,
  279. expect.objectContaining({
  280. query: expect.objectContaining({
  281. sort: '-startedAt',
  282. }),
  283. })
  284. );
  285. });
  286. // Click on the start time header and expect the sort to be startedAt
  287. userEvent.click(screen.getByRole('columnheader', {name: 'Start Time'}));
  288. expect(mockRouterContext.context.router.push).toHaveBeenCalledWith({
  289. pathname: '/organizations/org-slug/replays/',
  290. query: {
  291. sort: 'startedAt',
  292. },
  293. });
  294. // Need to simulate a rerender to get the new sort
  295. rerender(
  296. getComponent({
  297. location: {
  298. query: {
  299. sort: 'startedAt',
  300. },
  301. },
  302. })
  303. );
  304. await waitFor(() => {
  305. expect(mockApi).toHaveBeenCalledTimes(2);
  306. expect(mockApi).toHaveBeenCalledWith(
  307. mockUrl,
  308. expect.objectContaining({
  309. query: expect.objectContaining({
  310. sort: 'startedAt',
  311. }),
  312. })
  313. );
  314. });
  315. });
  316. it('should be able to click the `Duration` column and request data sorted by duration query', async () => {
  317. const mockApi = MockApiClient.addMockResponse({
  318. url: mockUrl,
  319. body: {
  320. data: [],
  321. },
  322. statusCode: 200,
  323. });
  324. const {rerender} = renderComponent();
  325. await waitFor(() => {
  326. expect(mockApi).toHaveBeenCalledWith(
  327. mockUrl,
  328. expect.objectContaining({
  329. query: expect.objectContaining({
  330. sort: '-startedAt',
  331. }),
  332. })
  333. );
  334. });
  335. // Click on the duration header and expect the sort to be duration
  336. userEvent.click(screen.getByRole('columnheader', {name: 'Duration'}));
  337. expect(mockRouterContext.context.router.push).toHaveBeenCalledWith({
  338. pathname: '/organizations/org-slug/replays/',
  339. query: {
  340. sort: '-duration',
  341. },
  342. });
  343. // Need to simulate a rerender to get the new sort
  344. rerender(
  345. getComponent({
  346. location: {
  347. query: {
  348. sort: '-duration',
  349. },
  350. },
  351. })
  352. );
  353. await waitFor(() => {
  354. expect(mockApi).toHaveBeenCalledTimes(2);
  355. expect(mockApi).toHaveBeenCalledWith(
  356. mockUrl,
  357. expect.objectContaining({
  358. query: expect.objectContaining({
  359. sort: '-duration',
  360. }),
  361. })
  362. );
  363. });
  364. });
  365. it('should be able to click the `Errors` column and request data sorted by countErrors query', async () => {
  366. const mockApi = MockApiClient.addMockResponse({
  367. url: mockUrl,
  368. body: {
  369. data: [],
  370. },
  371. statusCode: 200,
  372. });
  373. const {rerender} = renderComponent();
  374. await waitFor(() => {
  375. expect(mockApi).toHaveBeenCalledWith(
  376. mockUrl,
  377. expect.objectContaining({
  378. query: expect.objectContaining({
  379. sort: '-startedAt',
  380. }),
  381. })
  382. );
  383. });
  384. // Click on the errors header and expect the sort to be countErrors
  385. userEvent.click(screen.getByRole('columnheader', {name: 'Errors'}));
  386. expect(mockRouterContext.context.router.push).toHaveBeenCalledWith({
  387. pathname: '/organizations/org-slug/replays/',
  388. query: {
  389. sort: '-countErrors',
  390. },
  391. });
  392. // Need to simulate a rerender to get the new sort
  393. rerender(
  394. getComponent({
  395. location: {
  396. query: {
  397. sort: '-countErrors',
  398. },
  399. },
  400. })
  401. );
  402. await waitFor(() => {
  403. expect(mockApi).toHaveBeenCalledTimes(2);
  404. expect(mockApi).toHaveBeenCalledWith(
  405. mockUrl,
  406. expect.objectContaining({
  407. query: expect.objectContaining({
  408. sort: '-countErrors',
  409. }),
  410. })
  411. );
  412. });
  413. });
  414. it("should show a message when the organization doesn't have access to the replay feature", () => {
  415. renderComponent({
  416. organizationProps: {
  417. features: [],
  418. },
  419. });
  420. expect(screen.getByText("You don't have access to this feature")).toBeInTheDocument();
  421. });
  422. });