index.spec.tsx 8.7 KB

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