index.spec.tsx 12 KB

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