index.spec.tsx 8.8 KB

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