index.spec.tsx 24 KB

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