index.spec.tsx 8.7 KB

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