index.spec.tsx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. import {browserHistory} from 'react-router';
  2. import {Location} from 'history';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {
  5. initializeData,
  6. InitializeDataSettings,
  7. } from 'sentry-test/performance/initializePerformanceData';
  8. import {
  9. act,
  10. fireEvent,
  11. render,
  12. screen,
  13. userEvent,
  14. waitFor,
  15. within,
  16. } from 'sentry-test/reactTestingLibrary';
  17. import PageFiltersStore from 'sentry/stores/pageFiltersStore';
  18. import ProjectsStore from 'sentry/stores/projectsStore';
  19. import {WebVital} from 'sentry/utils/fields';
  20. import TrendsIndex from 'sentry/views/performance/trends/';
  21. import {defaultTrendsSelectionDate} from 'sentry/views/performance/trends/content';
  22. import {
  23. DEFAULT_MAX_DURATION,
  24. TRENDS_FUNCTIONS,
  25. TRENDS_PARAMETERS,
  26. } from 'sentry/views/performance/trends/utils';
  27. const trendsViewQuery = {
  28. query: `tpm():>0.01 transaction.duration:>0 transaction.duration:<${DEFAULT_MAX_DURATION}`,
  29. };
  30. jest.mock(
  31. 'sentry/utils/getDynamicComponent',
  32. () =>
  33. ({fixed}) =>
  34. fixed
  35. );
  36. jest.mock('moment', () => {
  37. const moment = jest.requireActual('moment');
  38. moment.now = jest.fn().mockReturnValue(1601251200000);
  39. return moment;
  40. });
  41. async function getTrendDropdown() {
  42. const dropdown = await screen.findByRole('button', {name: /Percentile.+/});
  43. expect(dropdown).toBeInTheDocument();
  44. return dropdown;
  45. }
  46. async function getParameterDropdown() {
  47. const dropdown = await screen.findByRole('button', {name: /Parameter.+/});
  48. expect(dropdown).toBeInTheDocument();
  49. return dropdown;
  50. }
  51. async function waitForMockCall(mock: any) {
  52. await waitFor(() => {
  53. expect(mock).toHaveBeenCalled();
  54. });
  55. }
  56. function enterSearch(el, text) {
  57. fireEvent.change(el, {target: {value: text}});
  58. fireEvent.submit(el);
  59. }
  60. // Might swap on/off the skiphover to check perf later.
  61. async function clickEl(el) {
  62. await userEvent.click(el, {skipHover: true});
  63. }
  64. function _initializeData(
  65. settings: InitializeDataSettings,
  66. options?: {selectedProjectId?: string}
  67. ) {
  68. const newSettings = {...settings};
  69. newSettings.projects = settings.projects ?? [
  70. TestStubs.Project({id: '1', firstTransactionEvent: false}),
  71. TestStubs.Project({id: '2', firstTransactionEvent: true}),
  72. ];
  73. if (options?.selectedProjectId) {
  74. const selectedProject = newSettings.projects.find(
  75. p => p.id === options.selectedProjectId
  76. );
  77. if (!selectedProject) {
  78. throw new Error("Test is selecting project that isn't loaded");
  79. } else {
  80. PageFiltersStore.updateProjects(
  81. settings.selectedProject ? [Number(selectedProject)] : [],
  82. []
  83. );
  84. }
  85. newSettings.selectedProject = selectedProject.id;
  86. }
  87. newSettings.selectedProject = settings.selectedProject ?? newSettings.projects[0].id;
  88. const data = initializeData(newSettings);
  89. // Modify page filters store to stop rerendering due to the test harness.
  90. PageFiltersStore.onInitializeUrlState(
  91. {
  92. projects: [],
  93. environments: [],
  94. datetime: {start: null, end: null, period: '24h', utc: null},
  95. },
  96. new Set()
  97. );
  98. PageFiltersStore.updateDateTime(defaultTrendsSelectionDate);
  99. if (!options?.selectedProjectId) {
  100. PageFiltersStore.updateProjects(
  101. settings.selectedProject ? [Number(newSettings.projects[0].id)] : [],
  102. []
  103. );
  104. }
  105. act(() => ProjectsStore.loadInitialData(data.projects));
  106. return data;
  107. }
  108. function initializeTrendsData(
  109. projects: null | any[] = null,
  110. query = {},
  111. includeDefaultQuery = true,
  112. extraFeatures?: string[]
  113. ) {
  114. const _projects = Array.isArray(projects)
  115. ? projects
  116. : [
  117. TestStubs.Project({id: '1', firstTransactionEvent: false}),
  118. TestStubs.Project({id: '2', firstTransactionEvent: true}),
  119. ];
  120. const features = extraFeatures
  121. ? ['transaction-event', 'performance-view', ...extraFeatures]
  122. : ['transaction-event', 'performance-view'];
  123. const organization = TestStubs.Organization({
  124. features,
  125. projects: _projects,
  126. });
  127. const newQuery = {...(includeDefaultQuery ? trendsViewQuery : {}), ...query};
  128. const initialData = initializeOrg({
  129. organization,
  130. router: {
  131. location: {
  132. query: newQuery,
  133. },
  134. },
  135. projects: _projects,
  136. project: projects ? projects[0] : undefined,
  137. });
  138. act(() => ProjectsStore.loadInitialData(initialData.organization.projects));
  139. return initialData;
  140. }
  141. describe('Performance > Trends', function () {
  142. let trendsStatsMock;
  143. beforeEach(function () {
  144. browserHistory.push = jest.fn();
  145. MockApiClient.addMockResponse({
  146. url: '/organizations/org-slug/projects/',
  147. body: [],
  148. });
  149. MockApiClient.addMockResponse({
  150. url: '/organizations/org-slug/tags/',
  151. body: [],
  152. });
  153. MockApiClient.addMockResponse({
  154. url: '/organizations/org-slug/users/',
  155. body: [],
  156. });
  157. MockApiClient.addMockResponse({
  158. url: '/organizations/org-slug/recent-searches/',
  159. body: [],
  160. });
  161. MockApiClient.addMockResponse({
  162. url: '/organizations/org-slug/recent-searches/',
  163. method: 'POST',
  164. body: [],
  165. });
  166. MockApiClient.addMockResponse({
  167. url: '/organizations/org-slug/sdk-updates/',
  168. body: [],
  169. });
  170. MockApiClient.addMockResponse({
  171. url: '/prompts-activity/',
  172. body: {},
  173. });
  174. MockApiClient.addMockResponse({
  175. url: '/organizations/org-slug/releases/stats/',
  176. body: [],
  177. });
  178. MockApiClient.addMockResponse({
  179. url: '/organizations/org-slug/tags/transaction.duration/values/',
  180. body: [],
  181. });
  182. trendsStatsMock = MockApiClient.addMockResponse({
  183. url: '/organizations/org-slug/events-trends-stats/',
  184. body: {
  185. stats: {
  186. 'internal,/organizations/:orgId/performance/': {
  187. data: [[123, []]],
  188. },
  189. order: 0,
  190. },
  191. events: {
  192. meta: {
  193. count_range_1: 'integer',
  194. count_range_2: 'integer',
  195. count_percentage: 'percentage',
  196. breakpoint: 'number',
  197. trend_percentage: 'percentage',
  198. trend_difference: 'number',
  199. aggregate_range_1: 'duration',
  200. aggregate_range_2: 'duration',
  201. transaction: 'string',
  202. },
  203. data: [
  204. {
  205. count: 8,
  206. project: 'internal',
  207. count_range_1: 2,
  208. count_range_2: 6,
  209. count_percentage: 3,
  210. breakpoint: 1686967200,
  211. trend_percentage: 1.9235225955967554,
  212. trend_difference: 797,
  213. aggregate_range_1: 863,
  214. aggregate_range_2: 1660,
  215. transaction: '/organizations/:orgId/performance/',
  216. },
  217. {
  218. count: 60,
  219. project: 'internal',
  220. count_range_1: 20,
  221. count_range_2: 40,
  222. count_percentage: 2,
  223. breakpoint: 1686967200,
  224. trend_percentage: 1.204968944099379,
  225. trend_difference: 66,
  226. aggregate_range_1: 322,
  227. aggregate_range_2: 388,
  228. transaction: '/api/0/internal/health/',
  229. },
  230. ],
  231. },
  232. },
  233. });
  234. MockApiClient.addMockResponse({
  235. url: '/organizations/org-slug/events/',
  236. body: {
  237. data: [
  238. {
  239. 'p95()': 1010.9232499999998,
  240. 'p50()': 47.34580982348902,
  241. 'tps()': 3.7226926286168966,
  242. 'count()': 34872349,
  243. 'failure_rate()': 0.43428379,
  244. 'examples()': ['djk3w308er', '3298a9ui3h'],
  245. },
  246. ],
  247. meta: {
  248. fields: {
  249. 'p95()': 'duration',
  250. '950()': 'duration',
  251. 'tps()': 'number',
  252. 'count()': 'number',
  253. 'failure_rate()': 'number',
  254. 'examples()': 'Array',
  255. },
  256. units: {
  257. 'p95()': 'millisecond',
  258. 'p50()': 'millisecond',
  259. 'tps()': null,
  260. 'count()': null,
  261. 'failure_rate()': null,
  262. 'examples()': null,
  263. },
  264. isMetricsData: true,
  265. tips: {},
  266. dataset: 'metrics',
  267. },
  268. },
  269. });
  270. MockApiClient.addMockResponse({
  271. url: '/organizations/org-slug/events-spans-performance/',
  272. body: [],
  273. });
  274. });
  275. afterEach(function () {
  276. MockApiClient.clearMockResponses();
  277. act(() => ProjectsStore.reset());
  278. });
  279. it('renders basic UI elements', async function () {
  280. const data = _initializeData({});
  281. render(
  282. <TrendsIndex location={data.router.location} organization={data.organization} />,
  283. {
  284. context: data.routerContext,
  285. organization: data.organization,
  286. }
  287. );
  288. expect(await getTrendDropdown()).toBeInTheDocument();
  289. expect(await getParameterDropdown()).toBeInTheDocument();
  290. expect(screen.getAllByTestId('changed-transactions')).toHaveLength(2);
  291. });
  292. it('transaction list items are rendered', async function () {
  293. const data = _initializeData({});
  294. render(
  295. <TrendsIndex location={data.router.location} organization={data.organization} />,
  296. {
  297. context: data.routerContext,
  298. organization: data.organization,
  299. }
  300. );
  301. expect(await screen.findAllByTestId('trends-list-item-regression')).toHaveLength(2);
  302. expect(await screen.findAllByTestId('trends-list-item-improved')).toHaveLength(2);
  303. });
  304. it('view summary menu action links to the correct view', async function () {
  305. const projects = [TestStubs.Project({id: 1, slug: 'internal'}), TestStubs.Project()];
  306. const data = initializeTrendsData(projects, {project: ['1']});
  307. render(
  308. <TrendsIndex location={data.router.location} organization={data.organization} />,
  309. {
  310. context: data.routerContext,
  311. organization: data.organization,
  312. }
  313. );
  314. const transactions = await screen.findAllByTestId('trends-list-item-improved');
  315. expect(transactions).toHaveLength(2);
  316. const firstTransaction = transactions[0];
  317. const summaryLink = within(firstTransaction).getByTestId('item-transaction-name');
  318. expect(summaryLink.closest('a')).toHaveAttribute(
  319. 'href',
  320. '/organizations/org-slug/performance/summary/?display=trend&project=1&query=tpm%28%29%3A%3E0.01%20transaction.duration%3A%3E0%20transaction.duration%3A%3C15min%20count_percentage%28%29%3A%3E0.25%20count_percentage%28%29%3A%3C4%20trend_percentage%28%29%3A%3E0%25%20confidence%28%29%3A%3E6&referrer=performance-transaction-summary&statsPeriod=14d&transaction=%2Forganizations%2F%3AorgId%2Fperformance%2F&trendFunction=p50&unselectedSeries=p100%28%29&unselectedSeries=avg%28%29'
  321. );
  322. });
  323. it('view summary menu action opens performance change explorer with feature flag', async function () {
  324. const projects = [TestStubs.Project({id: 1, slug: 'internal'}), TestStubs.Project()];
  325. const data = initializeTrendsData(projects, {project: ['1']}, true, [
  326. 'performance-change-explorer',
  327. ]);
  328. render(
  329. <TrendsIndex location={data.router.location} organization={data.organization} />,
  330. {
  331. context: data.routerContext,
  332. organization: data.organization,
  333. }
  334. );
  335. const transactions = await screen.findAllByTestId('trends-list-item-improved');
  336. expect(transactions).toHaveLength(2);
  337. const firstTransaction = transactions[0];
  338. const summaryLink = within(firstTransaction).getByTestId('item-transaction-name');
  339. expect(summaryLink.closest('a')).not.toHaveAttribute('href');
  340. await clickEl(summaryLink);
  341. await waitFor(() => {
  342. expect(screen.getByText('Ongoing Improvement')).toBeInTheDocument();
  343. expect(screen.getByText('Throughput')).toBeInTheDocument();
  344. expect(screen.getByText('P95')).toBeInTheDocument();
  345. expect(screen.getByText('P50')).toBeInTheDocument();
  346. expect(screen.getByText('Failure Rate')).toBeInTheDocument();
  347. });
  348. });
  349. it('hide from list menu action modifies query', async function () {
  350. const projects = [TestStubs.Project({id: 1, slug: 'internal'}), TestStubs.Project()];
  351. const data = initializeTrendsData(projects, {project: ['1']});
  352. render(
  353. <TrendsIndex location={data.router.location} organization={data.organization} />,
  354. {
  355. context: data.routerContext,
  356. organization: data.organization,
  357. }
  358. );
  359. const transactions = await screen.findAllByTestId('trends-list-item-improved');
  360. expect(transactions).toHaveLength(2);
  361. const firstTransaction = transactions[0];
  362. const menuActions = within(firstTransaction).getAllByTestId('menu-action');
  363. expect(menuActions).toHaveLength(3);
  364. const menuAction = menuActions[2];
  365. await clickEl(menuAction);
  366. expect(browserHistory.push).toHaveBeenCalledWith({
  367. query: expect.objectContaining({
  368. project: expect.anything(),
  369. query: `tpm():>0.01 transaction.duration:>0 transaction.duration:<${DEFAULT_MAX_DURATION} !transaction:/organizations/:orgId/performance/`,
  370. }),
  371. });
  372. });
  373. it('Changing search causes cursors to be reset', async function () {
  374. const projects = [TestStubs.Project({id: 1, slug: 'internal'}), TestStubs.Project()];
  375. const data = initializeTrendsData(projects, {project: ['1']});
  376. render(
  377. <TrendsIndex location={data.router.location} organization={data.organization} />,
  378. {
  379. context: data.routerContext,
  380. organization: data.organization,
  381. }
  382. );
  383. const input = await screen.findByTestId('smart-search-input');
  384. enterSearch(input, 'transaction.duration:>9000');
  385. expect(browserHistory.push).toHaveBeenCalledWith({
  386. pathname: undefined,
  387. query: expect.objectContaining({
  388. project: ['1'],
  389. query: 'transaction.duration:>9000',
  390. improvedCursor: undefined,
  391. regressionCursor: undefined,
  392. }),
  393. });
  394. });
  395. it('exclude greater than list menu action modifies query', async function () {
  396. const projects = [TestStubs.Project({id: 1, slug: 'internal'}), TestStubs.Project()];
  397. const data = initializeTrendsData(projects, {project: ['1']});
  398. render(
  399. <TrendsIndex location={data.router.location} organization={data.organization} />,
  400. {
  401. context: data.routerContext,
  402. organization: data.organization,
  403. }
  404. );
  405. const transactions = await screen.findAllByTestId('trends-list-item-improved');
  406. expect(transactions).toHaveLength(2);
  407. const firstTransaction = transactions[0];
  408. const menuActions = within(firstTransaction).getAllByTestId('menu-action');
  409. expect(menuActions).toHaveLength(3);
  410. const menuAction = menuActions[0];
  411. await clickEl(menuAction);
  412. expect(browserHistory.push).toHaveBeenCalledWith({
  413. query: expect.objectContaining({
  414. project: expect.anything(),
  415. query: 'tpm():>0.01 transaction.duration:>0 transaction.duration:<=863',
  416. }),
  417. });
  418. });
  419. it('exclude less than list menu action modifies query', async function () {
  420. const projects = [TestStubs.Project({id: 1, slug: 'internal'}), TestStubs.Project()];
  421. const data = initializeTrendsData(projects, {project: ['1']});
  422. render(
  423. <TrendsIndex location={data.router.location} organization={data.organization} />,
  424. {
  425. context: data.routerContext,
  426. organization: data.organization,
  427. }
  428. );
  429. const transactions = await screen.findAllByTestId('trends-list-item-improved');
  430. expect(transactions).toHaveLength(2);
  431. const firstTransaction = transactions[0];
  432. const menuActions = within(firstTransaction).getAllByTestId('menu-action');
  433. expect(menuActions).toHaveLength(3);
  434. const menuAction = menuActions[1];
  435. await clickEl(menuAction);
  436. expect(browserHistory.push).toHaveBeenCalledWith({
  437. query: expect.objectContaining({
  438. project: expect.anything(),
  439. query: 'tpm():>0.01 transaction.duration:<15min transaction.duration:>=863',
  440. }),
  441. });
  442. });
  443. it('choosing a trend function changes location', async function () {
  444. const projects = [TestStubs.Project()];
  445. const data = initializeTrendsData(projects, {project: ['-1']});
  446. render(
  447. <TrendsIndex location={data.router.location} organization={data.organization} />,
  448. {
  449. context: data.routerContext,
  450. organization: data.organization,
  451. }
  452. );
  453. for (const trendFunction of TRENDS_FUNCTIONS) {
  454. // Open dropdown
  455. const dropdown = await getTrendDropdown();
  456. await clickEl(dropdown);
  457. // Select function
  458. const option = screen.getByRole('option', {name: trendFunction.label});
  459. await clickEl(option);
  460. expect(browserHistory.push).toHaveBeenCalledWith({
  461. query: expect.objectContaining({
  462. regressionCursor: undefined,
  463. improvedCursor: undefined,
  464. trendFunction: trendFunction.field,
  465. }),
  466. });
  467. }
  468. });
  469. it('sets LCP as a default trend parameter for frontend project if query does not specify trend parameter', async function () {
  470. const projects = [TestStubs.Project({id: 1, platform: 'javascript'})];
  471. const data = initializeTrendsData(projects, {project: [1]});
  472. render(
  473. <TrendsIndex location={data.router.location} organization={data.organization} />,
  474. {
  475. context: data.routerContext,
  476. organization: data.organization,
  477. }
  478. );
  479. const trendDropdownButton = await getTrendDropdown();
  480. expect(trendDropdownButton).toHaveTextContent('Percentilep50');
  481. });
  482. it('sets duration as a default trend parameter for backend project if query does not specify trend parameter', async function () {
  483. const projects = [TestStubs.Project({id: 1, platform: 'python'})];
  484. const data = initializeTrendsData(projects, {project: [1]});
  485. render(
  486. <TrendsIndex location={data.router.location} organization={data.organization} />,
  487. {
  488. context: data.routerContext,
  489. organization: data.organization,
  490. }
  491. );
  492. const parameterDropdownButton = await getParameterDropdown();
  493. expect(parameterDropdownButton).toHaveTextContent('ParameterDuration');
  494. });
  495. it('sets trend parameter from query and ignores default trend parameter', async function () {
  496. const projects = [TestStubs.Project({id: 1, platform: 'javascript'})];
  497. const data = initializeTrendsData(projects, {project: [1], trendParameter: 'FCP'});
  498. render(
  499. <TrendsIndex location={data.router.location} organization={data.organization} />,
  500. {
  501. context: data.routerContext,
  502. organization: data.organization,
  503. }
  504. );
  505. const parameterDropdownButton = await getParameterDropdown();
  506. expect(parameterDropdownButton).toHaveTextContent('ParameterFCP');
  507. });
  508. it('choosing a parameter changes location', async function () {
  509. const projects = [TestStubs.Project()];
  510. const data = initializeTrendsData(projects, {project: ['-1']});
  511. render(
  512. <TrendsIndex location={data.router.location} organization={data.organization} />,
  513. {
  514. context: data.routerContext,
  515. organization: data.organization,
  516. }
  517. );
  518. for (const parameter of TRENDS_PARAMETERS) {
  519. // Open dropdown
  520. const dropdown = await getParameterDropdown();
  521. await clickEl(dropdown);
  522. // Select parameter
  523. const option = screen.getByRole('option', {name: parameter.label});
  524. await clickEl(option);
  525. expect(browserHistory.push).toHaveBeenCalledWith({
  526. query: expect.objectContaining({
  527. trendParameter: parameter.label,
  528. }),
  529. });
  530. }
  531. });
  532. it('choosing a web vitals parameter adds it as an additional condition to the query', async function () {
  533. const projects = [TestStubs.Project()];
  534. const data = initializeTrendsData(projects, {project: ['-1']});
  535. const {rerender} = render(
  536. <TrendsIndex location={data.router.location} organization={data.organization} />,
  537. {
  538. context: data.routerContext,
  539. organization: data.organization,
  540. }
  541. );
  542. for (const parameter of TRENDS_PARAMETERS) {
  543. if (Object.values(WebVital).includes(parameter.column as string as WebVital)) {
  544. trendsStatsMock.mockReset();
  545. const newLocation = {
  546. query: {...trendsViewQuery, trendParameter: parameter.label},
  547. };
  548. rerender(
  549. <TrendsIndex
  550. location={newLocation as unknown as Location}
  551. organization={data.organization}
  552. />
  553. );
  554. await waitForMockCall(trendsStatsMock);
  555. expect(trendsStatsMock).toHaveBeenCalledTimes(2);
  556. // Improved transactions call
  557. expect(trendsStatsMock).toHaveBeenNthCalledWith(
  558. 1,
  559. expect.anything(),
  560. expect.objectContaining({
  561. query: expect.objectContaining({
  562. query: expect.stringContaining(`has:${parameter.column}`),
  563. }),
  564. })
  565. );
  566. // Regression transactions call
  567. expect(trendsStatsMock).toHaveBeenNthCalledWith(
  568. 2,
  569. expect.anything(),
  570. expect.objectContaining({
  571. query: expect.objectContaining({
  572. query: expect.stringContaining(`has:${parameter.column}`),
  573. }),
  574. })
  575. );
  576. }
  577. }
  578. });
  579. it('trend functions in location make api calls', async function () {
  580. const projects = [TestStubs.Project(), TestStubs.Project()];
  581. const data = initializeTrendsData(projects, {project: ['-1']});
  582. const {rerender} = render(
  583. <TrendsIndex location={data.router.location} organization={data.organization} />,
  584. {
  585. context: data.routerContext,
  586. organization: data.organization,
  587. }
  588. );
  589. for (const trendFunction of TRENDS_FUNCTIONS) {
  590. trendsStatsMock.mockReset();
  591. const newLocation = {
  592. query: {...trendsViewQuery, trendFunction: trendFunction.field},
  593. };
  594. rerender(
  595. <TrendsIndex
  596. location={newLocation as unknown as Location}
  597. organization={data.organization}
  598. />
  599. );
  600. await waitForMockCall(trendsStatsMock);
  601. expect(trendsStatsMock).toHaveBeenCalledTimes(2);
  602. const sort = 'trend_percentage()';
  603. const defaultTrendsFields = ['project'];
  604. const transactionFields = ['transaction', ...defaultTrendsFields];
  605. const projectFields = [...defaultTrendsFields];
  606. expect(transactionFields).toHaveLength(2);
  607. expect(projectFields).toHaveLength(transactionFields.length - 1);
  608. // Improved transactions call
  609. expect(trendsStatsMock).toHaveBeenNthCalledWith(
  610. 1,
  611. expect.anything(),
  612. expect.objectContaining({
  613. query: expect.objectContaining({
  614. trendFunction: `${trendFunction.field}(transaction.duration)`,
  615. sort,
  616. query: expect.stringContaining('trend_percentage():>0%'),
  617. interval: '1h',
  618. field: transactionFields,
  619. statsPeriod: '14d',
  620. }),
  621. })
  622. );
  623. // Regression transactions call
  624. expect(trendsStatsMock).toHaveBeenNthCalledWith(
  625. 2,
  626. expect.anything(),
  627. expect.objectContaining({
  628. query: expect.objectContaining({
  629. trendFunction: `${trendFunction.field}(transaction.duration)`,
  630. sort: '-' + sort,
  631. query: expect.stringContaining('trend_percentage():>0%'),
  632. interval: '1h',
  633. field: transactionFields,
  634. statsPeriod: '14d',
  635. }),
  636. })
  637. );
  638. }
  639. });
  640. it('Visiting trends with trends feature will update filters if none are set', function () {
  641. const data = initializeTrendsData(undefined, {}, false);
  642. render(
  643. <TrendsIndex location={data.router.location} organization={data.organization} />,
  644. {
  645. context: data.routerContext,
  646. organization: data.organization,
  647. }
  648. );
  649. expect(browserHistory.push).toHaveBeenNthCalledWith(
  650. 1,
  651. expect.objectContaining({
  652. query: {
  653. query: `tpm():>0.01 transaction.duration:>0 transaction.duration:<${DEFAULT_MAX_DURATION}`,
  654. },
  655. })
  656. );
  657. });
  658. it('Navigating away from trends will remove extra tags from query', async function () {
  659. const data = initializeTrendsData(
  660. undefined,
  661. {
  662. query: `device.family:Mac tpm():>0.01 transaction.duration:>0 transaction.duration:<${DEFAULT_MAX_DURATION}`,
  663. },
  664. false
  665. );
  666. render(
  667. <TrendsIndex location={data.router.location} organization={data.organization} />,
  668. {
  669. context: data.routerContext,
  670. organization: data.organization,
  671. }
  672. );
  673. (browserHistory.push as any).mockReset();
  674. const byTransactionLink = await screen.findByTestId('breadcrumb-link');
  675. expect(byTransactionLink.closest('a')).toHaveAttribute(
  676. 'href',
  677. '/organizations/org-slug/performance/?query=device.family%3AMac'
  678. );
  679. });
  680. });