index.spec.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. import {ReplayList} from 'sentry-fixture/replayList';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {render, screen, waitFor} from 'sentry-test/reactTestingLibrary';
  4. import ProjectsStore from 'sentry/stores/projectsStore';
  5. import {
  6. SPAN_OP_BREAKDOWN_FIELDS,
  7. SPAN_OP_RELATIVE_BREAKDOWN_FIELD,
  8. } from 'sentry/utils/discover/fields';
  9. import {OrganizationContext} from 'sentry/views/organizationContext';
  10. import TransactionReplays from 'sentry/views/performance/transactionSummary/transactionReplays';
  11. import {RouteContext} from 'sentry/views/routeContext';
  12. type InitializeOrgProps = {
  13. location?: {
  14. pathname?: string;
  15. query?: {[key: string]: string};
  16. };
  17. organizationProps?: {
  18. features?: string[];
  19. };
  20. };
  21. jest.mock('sentry/utils/useMedia', () => ({
  22. __esModule: true,
  23. default: jest.fn(() => true),
  24. }));
  25. const mockEventsUrl = '/organizations/org-slug/events/';
  26. const mockReplaysUrl = '/organizations/org-slug/replays/';
  27. let mockRouterContext: {
  28. childContextTypes?: any;
  29. context?: any;
  30. } = {};
  31. const getComponent = ({
  32. location,
  33. organizationProps = {features: ['performance-view', 'session-replay']},
  34. }: InitializeOrgProps) => {
  35. const {router, organization, routerContext} = initializeOrg({
  36. organization: {
  37. ...organizationProps,
  38. },
  39. project: TestStubs.Project(),
  40. projects: [TestStubs.Project()],
  41. router: {
  42. routes: [
  43. {path: '/'},
  44. {path: '/organizations/:orgId/performance/summary/'},
  45. {path: 'replays/'},
  46. ],
  47. location: {
  48. pathname: '/organizations/org-slug/replays/',
  49. query: {
  50. project: '1',
  51. transaction: 'Settings Page',
  52. },
  53. ...location,
  54. },
  55. },
  56. });
  57. ProjectsStore.init();
  58. ProjectsStore.loadInitialData(organization.projects);
  59. mockRouterContext = routerContext;
  60. return (
  61. <OrganizationContext.Provider value={organization}>
  62. <RouteContext.Provider
  63. value={{
  64. router,
  65. location: router.location,
  66. params: router.params,
  67. routes: router.routes,
  68. }}
  69. >
  70. <TransactionReplays />
  71. </RouteContext.Provider>
  72. </OrganizationContext.Provider>
  73. );
  74. };
  75. const renderComponent = (componentProps: InitializeOrgProps = {}) => {
  76. return render(getComponent(componentProps), {context: mockRouterContext});
  77. };
  78. describe('TransactionReplays', () => {
  79. let eventsMockApi: jest.Mock<any, any>;
  80. let replaysMockApi: jest.Mock<any, any>;
  81. beforeEach(() => {
  82. MockApiClient.addMockResponse({
  83. method: 'GET',
  84. url: `/organizations/org-slug/sdk-updates/`,
  85. body: [],
  86. });
  87. MockApiClient.addMockResponse({
  88. url: '/organizations/org-slug/events-has-measurements/',
  89. body: {measurements: false},
  90. });
  91. MockApiClient.addMockResponse({
  92. url: '/organizations/org-slug/replay-count/',
  93. body: {
  94. data: [],
  95. },
  96. statusCode: 200,
  97. });
  98. eventsMockApi = MockApiClient.addMockResponse({
  99. url: '/organizations/org-slug/events/',
  100. body: {
  101. data: [],
  102. },
  103. statusCode: 200,
  104. });
  105. replaysMockApi = MockApiClient.addMockResponse({
  106. url: '/organizations/org-slug/replays/',
  107. body: {
  108. data: [],
  109. },
  110. statusCode: 200,
  111. });
  112. });
  113. afterEach(() => {
  114. MockApiClient.clearMockResponses();
  115. });
  116. it('should query the events endpoint for replayIds of a transaction', async () => {
  117. renderComponent();
  118. await waitFor(() => {
  119. expect(eventsMockApi).toHaveBeenCalledWith(
  120. '/organizations/org-slug/events/',
  121. expect.objectContaining({
  122. query: expect.objectContaining({
  123. cursor: undefined,
  124. statsPeriod: '14d',
  125. project: ['1'],
  126. environment: [],
  127. field: expect.arrayContaining([
  128. 'replayId',
  129. 'count()',
  130. 'transaction.duration',
  131. 'trace',
  132. 'timestamp',
  133. ...SPAN_OP_BREAKDOWN_FIELDS,
  134. SPAN_OP_RELATIVE_BREAKDOWN_FIELD,
  135. ]),
  136. per_page: 50,
  137. query: 'event.type:transaction transaction:"Settings Page" !replayId:""',
  138. }),
  139. })
  140. );
  141. });
  142. });
  143. it('should snapshot empty state', async () => {
  144. const mockApi = MockApiClient.addMockResponse({
  145. url: mockReplaysUrl,
  146. body: {
  147. data: [],
  148. },
  149. statusCode: 200,
  150. });
  151. renderComponent();
  152. await waitFor(() => {
  153. expect(mockApi).toHaveBeenCalledTimes(1);
  154. });
  155. });
  156. it('should show empty message when no replays are found', async () => {
  157. renderComponent();
  158. await waitFor(() => {
  159. expect(replaysMockApi).toHaveBeenCalledTimes(1);
  160. expect(screen.getByText('There are no items to display')).toBeInTheDocument();
  161. });
  162. });
  163. it('should show loading indicator when loading replays', async () => {
  164. const mockApi = MockApiClient.addMockResponse({
  165. url: mockEventsUrl,
  166. statusCode: 200,
  167. body: {
  168. data: [],
  169. },
  170. });
  171. renderComponent();
  172. expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
  173. await waitFor(() => {
  174. expect(mockApi).toHaveBeenCalledTimes(1);
  175. });
  176. });
  177. it('should show a list of replays and have the correct values', async () => {
  178. const mockApi = MockApiClient.addMockResponse({
  179. url: mockReplaysUrl,
  180. statusCode: 200,
  181. body: {
  182. data: [
  183. {
  184. ...ReplayList()[0],
  185. count_errors: 1,
  186. duration: 52346,
  187. finished_at: new Date('2022-09-15T06:54:00+00:00'),
  188. id: '346789a703f6454384f1de473b8b9fcc',
  189. started_at: new Date('2022-09-15T06:50:00+00:00'),
  190. urls: [
  191. 'https://dev.getsentry.net:7999/organizations/sentry-emerging-tech/replays/',
  192. '/organizations/sentry-emerging-tech/replays/?project=2',
  193. ],
  194. },
  195. {
  196. ...ReplayList()[0],
  197. count_errors: 4,
  198. duration: 400,
  199. finished_at: new Date('2022-09-21T21:40:38+00:00'),
  200. id: 'b05dae9b6be54d21a4d5ad9f8f02b780',
  201. started_at: new Date('2022-09-21T21:30:44+00:00'),
  202. urls: [
  203. 'https://dev.getsentry.net:7999/organizations/sentry-emerging-tech/replays/?project=2&statsPeriod=24h',
  204. '/organizations/sentry-emerging-tech/issues/',
  205. '/organizations/sentry-emerging-tech/issues/?project=2',
  206. ],
  207. },
  208. ].map(hydrated => ({
  209. ...hydrated,
  210. started_at: hydrated.started_at.toString(),
  211. finished_at: hydrated.finished_at.toString(),
  212. })),
  213. },
  214. });
  215. // Mock the system date to be 2022-09-28
  216. jest.useFakeTimers().setSystemTime(new Date('Sep 28, 2022 11:29:13 PM UTC'));
  217. renderComponent();
  218. await waitFor(() => {
  219. expect(mockApi).toHaveBeenCalledTimes(1);
  220. });
  221. // Expect the table to have 2 rows
  222. expect(screen.getAllByText('testDisplayName')).toHaveLength(2);
  223. const expectedQuery =
  224. 'project=1&query=&referrer=%2Forganizations%2F%3AorgId%2Fperformance%2Fsummary%2Freplays%2F&statsPeriod=14d&yAxis=count%28%29';
  225. // Expect the first row to have the correct href
  226. expect(screen.getAllByRole('link', {name: 'testDisplayName'})[0]).toHaveAttribute(
  227. 'href',
  228. `/organizations/org-slug/replays/346789a703f6454384f1de473b8b9fcc/?${expectedQuery}`
  229. );
  230. // Expect the second row to have the correct href
  231. expect(screen.getAllByRole('link', {name: 'testDisplayName'})[1]).toHaveAttribute(
  232. 'href',
  233. `/organizations/org-slug/replays/b05dae9b6be54d21a4d5ad9f8f02b780/?${expectedQuery}`
  234. );
  235. // Expect the first row to have the correct duration
  236. expect(screen.getByText('14:32:26')).toBeInTheDocument();
  237. // Expect the second row to have the correct duration
  238. expect(screen.getByText('06:40')).toBeInTheDocument();
  239. // Expect the first row to have the correct errors
  240. expect(screen.getAllByTestId('replay-table-count-errors')[0]).toHaveTextContent('1');
  241. // Expect the second row to have the correct errors
  242. expect(screen.getAllByTestId('replay-table-count-errors')[1]).toHaveTextContent('4');
  243. // Expect the first row to have the correct date
  244. expect(screen.getByText('14 days ago')).toBeInTheDocument();
  245. // Expect the second row to have the correct date
  246. expect(screen.getByText('7 days ago')).toBeInTheDocument();
  247. });
  248. it("should show a message when the organization doesn't have access to the replay feature", async () => {
  249. renderComponent({
  250. organizationProps: {
  251. features: ['performance-view'],
  252. },
  253. });
  254. await waitFor(() => {
  255. expect(
  256. screen.getByText("You don't have access to this feature")
  257. ).toBeInTheDocument();
  258. });
  259. });
  260. });