index.spec.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. method: 'GET',
  83. url: `/organizations/org-slug/sdk-updates/`,
  84. body: [],
  85. });
  86. MockApiClient.addMockResponse({
  87. url: '/organizations/org-slug/events-has-measurements/',
  88. body: {measurements: false},
  89. });
  90. MockApiClient.addMockResponse({
  91. url: '/organizations/org-slug/replay-count/',
  92. body: {
  93. data: [],
  94. },
  95. statusCode: 200,
  96. });
  97. eventsMockApi = MockApiClient.addMockResponse({
  98. url: '/organizations/org-slug/events/',
  99. body: {
  100. data: [],
  101. },
  102. statusCode: 200,
  103. });
  104. replaysMockApi = MockApiClient.addMockResponse({
  105. url: '/organizations/org-slug/replays/',
  106. body: {
  107. data: [],
  108. },
  109. statusCode: 200,
  110. });
  111. });
  112. afterEach(() => {
  113. MockApiClient.clearMockResponses();
  114. });
  115. it('should query the events endpoint for replayIds of a transaction', async () => {
  116. renderComponent();
  117. await waitFor(() => {
  118. expect(eventsMockApi).toHaveBeenCalledWith(
  119. '/organizations/org-slug/events/',
  120. expect.objectContaining({
  121. query: expect.objectContaining({
  122. cursor: undefined,
  123. statsPeriod: '14d',
  124. project: ['1'],
  125. environment: [],
  126. field: expect.arrayContaining([
  127. 'replayId',
  128. 'count()',
  129. 'transaction.duration',
  130. 'trace',
  131. 'timestamp',
  132. ...SPAN_OP_BREAKDOWN_FIELDS,
  133. SPAN_OP_RELATIVE_BREAKDOWN_FIELD,
  134. ]),
  135. per_page: 50,
  136. query: 'event.type:transaction transaction:"Settings Page" !replayId:""',
  137. }),
  138. })
  139. );
  140. });
  141. });
  142. it('should snapshot empty state', async () => {
  143. MockApiClient.addMockResponse({
  144. url: mockReplaysUrl,
  145. body: {
  146. data: [],
  147. },
  148. statusCode: 200,
  149. });
  150. const {container} = renderComponent();
  151. await waitFor(() => {
  152. expect(container).toSnapshot();
  153. });
  154. });
  155. it('should show empty message when no replays are found', async () => {
  156. renderComponent();
  157. await waitFor(() => {
  158. expect(replaysMockApi).toHaveBeenCalledTimes(1);
  159. expect(screen.getByText('There are no items to display')).toBeInTheDocument();
  160. });
  161. });
  162. it('should show loading indicator when loading replays', async () => {
  163. const mockApi = MockApiClient.addMockResponse({
  164. url: mockEventsUrl,
  165. statusCode: 200,
  166. body: {
  167. data: [],
  168. },
  169. });
  170. renderComponent();
  171. expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
  172. await waitFor(() => {
  173. expect(mockApi).toHaveBeenCalledTimes(1);
  174. });
  175. });
  176. it('should show a list of replays and have the correct values', async () => {
  177. const mockApi = MockApiClient.addMockResponse({
  178. url: mockReplaysUrl,
  179. statusCode: 200,
  180. body: {
  181. data: [
  182. {
  183. ...TestStubs.ReplayList()[0],
  184. count_errors: 1,
  185. duration: 52346,
  186. finished_at: new Date('2022-09-15T06:54:00+00:00'),
  187. id: '346789a703f6454384f1de473b8b9fcc',
  188. started_at: new Date('2022-09-15T06:50:00+00:00'),
  189. urls: [
  190. 'https://dev.getsentry.net:7999/organizations/sentry-emerging-tech/replays/',
  191. '/organizations/sentry-emerging-tech/replays/?project=2',
  192. ],
  193. },
  194. {
  195. ...TestStubs.ReplayList()[0],
  196. count_errors: 4,
  197. duration: 400,
  198. finished_at: new Date('2022-09-21T21:40:38+00:00'),
  199. id: 'b05dae9b6be54d21a4d5ad9f8f02b780',
  200. started_at: new Date('2022-09-21T21:30:44+00:00'),
  201. urls: [
  202. 'https://dev.getsentry.net:7999/organizations/sentry-emerging-tech/replays/?project=2&statsPeriod=24h',
  203. '/organizations/sentry-emerging-tech/issues/',
  204. '/organizations/sentry-emerging-tech/issues/?project=2',
  205. ],
  206. },
  207. ].map(hydrated => ({
  208. ...hydrated,
  209. started_at: hydrated.started_at.toString(),
  210. finished_at: hydrated.finished_at.toString(),
  211. })),
  212. },
  213. });
  214. // Mock the system date to be 2022-09-28
  215. jest.useFakeTimers().setSystemTime(new Date('Sep 28, 2022 11:29:13 PM UTC'));
  216. renderComponent();
  217. await waitFor(() => {
  218. expect(mockApi).toHaveBeenCalledTimes(1);
  219. });
  220. // Expect the table to have 2 rows
  221. expect(screen.getAllByText('testDisplayName')).toHaveLength(2);
  222. const expectedQuery =
  223. 'project=1&query=&referrer=%2Forganizations%2F%3AorgId%2Fperformance%2Fsummary%2Freplays%2F&statsPeriod=14d&yAxis=count%28%29';
  224. // Expect the first row to have the correct href
  225. expect(screen.getAllByRole('link', {name: 'testDisplayName'})[0]).toHaveAttribute(
  226. 'href',
  227. `/organizations/org-slug/replays/346789a703f6454384f1de473b8b9fcc/?${expectedQuery}`
  228. );
  229. // Expect the second row to have the correct href
  230. expect(screen.getAllByRole('link', {name: 'testDisplayName'})[1]).toHaveAttribute(
  231. 'href',
  232. `/organizations/org-slug/replays/b05dae9b6be54d21a4d5ad9f8f02b780/?${expectedQuery}`
  233. );
  234. // Expect the first row to have the correct duration
  235. expect(screen.getByText('14:32:26')).toBeInTheDocument();
  236. // Expect the second row to have the correct duration
  237. expect(screen.getByText('06:40')).toBeInTheDocument();
  238. // Expect the first row to have the correct errors
  239. expect(screen.getAllByTestId('replay-table-count-errors')[0]).toHaveTextContent('1');
  240. // Expect the second row to have the correct errors
  241. expect(screen.getAllByTestId('replay-table-count-errors')[1]).toHaveTextContent('4');
  242. // Expect the first row to have the correct date
  243. expect(screen.getByText('14 days ago')).toBeInTheDocument();
  244. // Expect the second row to have the correct date
  245. expect(screen.getByText('7 days ago')).toBeInTheDocument();
  246. });
  247. it("should show a message when the organization doesn't have access to the replay feature", async () => {
  248. renderComponent({
  249. organizationProps: {
  250. features: ['performance-view'],
  251. },
  252. });
  253. await waitFor(() => {
  254. expect(
  255. screen.getByText("You don't have access to this feature")
  256. ).toBeInTheDocument();
  257. });
  258. });
  259. });