index.spec.tsx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. import {browserHistory} from 'react-router';
  2. import {Location} from 'history';
  3. import {OrganizationFixture} from 'sentry-fixture/organization';
  4. import {ProjectFixture} from 'sentry-fixture/project';
  5. import {initializeOrg} from 'sentry-test/initializeOrg';
  6. import {
  7. initializeData,
  8. InitializeDataSettings,
  9. } from 'sentry-test/performance/initializePerformanceData';
  10. import {
  11. act,
  12. fireEvent,
  13. render,
  14. screen,
  15. userEvent,
  16. waitFor,
  17. within,
  18. } from 'sentry-test/reactTestingLibrary';
  19. import PageFiltersStore from 'sentry/stores/pageFiltersStore';
  20. import ProjectsStore from 'sentry/stores/projectsStore';
  21. import {WebVital} from 'sentry/utils/fields';
  22. import TrendsIndex from 'sentry/views/performance/trends/';
  23. import {defaultTrendsSelectionDate} from 'sentry/views/performance/trends/content';
  24. import {
  25. DEFAULT_MAX_DURATION,
  26. TRENDS_FUNCTIONS,
  27. TRENDS_PARAMETERS,
  28. } from 'sentry/views/performance/trends/utils';
  29. const trendsViewQuery = {
  30. query: `tpm():>0.01 transaction.duration:>0 transaction.duration:<${DEFAULT_MAX_DURATION}`,
  31. };
  32. jest.mock('moment', () => {
  33. const moment = jest.requireActual('moment');
  34. moment.now = jest.fn().mockReturnValue(1601251200000);
  35. return moment;
  36. });
  37. async function getTrendDropdown() {
  38. const dropdown = await screen.findByRole('button', {name: /Percentile.+/});
  39. expect(dropdown).toBeInTheDocument();
  40. return dropdown;
  41. }
  42. async function getParameterDropdown() {
  43. const dropdown = await screen.findByRole('button', {name: /Parameter.+/});
  44. expect(dropdown).toBeInTheDocument();
  45. return dropdown;
  46. }
  47. async function waitForMockCall(mock: any) {
  48. await waitFor(() => {
  49. expect(mock).toHaveBeenCalled();
  50. });
  51. }
  52. function enterSearch(el, text) {
  53. fireEvent.change(el, {target: {value: text}});
  54. fireEvent.submit(el);
  55. }
  56. // Might swap on/off the skiphover to check perf later.
  57. async function clickEl(el) {
  58. await userEvent.click(el, {skipHover: true});
  59. }
  60. function _initializeData(
  61. settings: InitializeDataSettings,
  62. options?: {selectedProjectId?: string}
  63. ) {
  64. const newSettings = {...settings};
  65. newSettings.projects = settings.projects ?? [
  66. ProjectFixture({id: '1', firstTransactionEvent: false}),
  67. ProjectFixture({id: '2', firstTransactionEvent: true}),
  68. ];
  69. if (options?.selectedProjectId) {
  70. const selectedProject = newSettings.projects.find(
  71. p => p.id === options.selectedProjectId
  72. );
  73. if (!selectedProject) {
  74. throw new Error("Test is selecting project that isn't loaded");
  75. } else {
  76. PageFiltersStore.updateProjects(
  77. settings.selectedProject ? [Number(selectedProject)] : [],
  78. []
  79. );
  80. }
  81. newSettings.selectedProject = selectedProject.id;
  82. }
  83. newSettings.selectedProject = settings.selectedProject ?? newSettings.projects[0].id;
  84. const data = initializeData(newSettings);
  85. // Modify page filters store to stop rerendering due to the test harness.
  86. PageFiltersStore.onInitializeUrlState(
  87. {
  88. projects: [],
  89. environments: [],
  90. datetime: {start: null, end: null, period: '24h', utc: null},
  91. },
  92. new Set()
  93. );
  94. PageFiltersStore.updateDateTime(defaultTrendsSelectionDate);
  95. if (!options?.selectedProjectId) {
  96. PageFiltersStore.updateProjects(
  97. settings.selectedProject ? [Number(newSettings.projects[0].id)] : [],
  98. []
  99. );
  100. }
  101. act(() => ProjectsStore.loadInitialData(data.projects));
  102. return data;
  103. }
  104. function initializeTrendsData(
  105. projects: null | any[] = null,
  106. query = {},
  107. includeDefaultQuery = true,
  108. extraFeatures?: string[]
  109. ) {
  110. const _projects = Array.isArray(projects)
  111. ? projects
  112. : [
  113. ProjectFixture({id: '1', firstTransactionEvent: false}),
  114. ProjectFixture({id: '2', firstTransactionEvent: true}),
  115. ];
  116. const features = extraFeatures
  117. ? ['transaction-event', 'performance-view', ...extraFeatures]
  118. : ['transaction-event', 'performance-view'];
  119. const organization = OrganizationFixture({
  120. features,
  121. projects: _projects,
  122. });
  123. const newQuery = {...(includeDefaultQuery ? trendsViewQuery : {}), ...query};
  124. const initialData = initializeOrg({
  125. organization,
  126. router: {
  127. location: {
  128. query: newQuery,
  129. },
  130. },
  131. projects: _projects,
  132. project: projects ? projects[0] : undefined,
  133. });
  134. act(() => ProjectsStore.loadInitialData(initialData.organization.projects));
  135. return initialData;
  136. }
  137. describe('Performance > Trends', function () {
  138. let trendsStatsMock;
  139. beforeEach(function () {
  140. browserHistory.push = jest.fn();
  141. MockApiClient.addMockResponse({
  142. url: '/organizations/org-slug/projects/',
  143. body: [],
  144. });
  145. MockApiClient.addMockResponse({
  146. url: '/organizations/org-slug/tags/',
  147. body: [],
  148. });
  149. MockApiClient.addMockResponse({
  150. url: '/organizations/org-slug/users/',
  151. body: [],
  152. });
  153. MockApiClient.addMockResponse({
  154. url: '/organizations/org-slug/recent-searches/',
  155. body: [],
  156. });
  157. MockApiClient.addMockResponse({
  158. url: '/organizations/org-slug/recent-searches/',
  159. method: 'POST',
  160. body: [],
  161. });
  162. MockApiClient.addMockResponse({
  163. url: '/organizations/org-slug/sdk-updates/',
  164. body: [],
  165. });
  166. MockApiClient.addMockResponse({
  167. url: '/prompts-activity/',
  168. body: {},
  169. });
  170. MockApiClient.addMockResponse({
  171. url: '/organizations/org-slug/releases/stats/',
  172. body: [],
  173. });
  174. MockApiClient.addMockResponse({
  175. url: '/organizations/org-slug/tags/transaction.duration/values/',
  176. body: [],
  177. });
  178. trendsStatsMock = MockApiClient.addMockResponse({
  179. url: '/organizations/org-slug/events-trends-stats/',
  180. body: {
  181. stats: {
  182. 'internal,/organizations/:orgId/performance/': {
  183. data: [[123, []]],
  184. },
  185. order: 0,
  186. },
  187. events: {
  188. meta: {
  189. count_range_1: 'integer',
  190. count_range_2: 'integer',
  191. count_percentage: 'percentage',
  192. breakpoint: 'number',
  193. trend_percentage: 'percentage',
  194. trend_difference: 'number',
  195. aggregate_range_1: 'duration',
  196. aggregate_range_2: 'duration',
  197. transaction: 'string',
  198. },
  199. data: [
  200. {
  201. count: 8,
  202. project: 'internal',
  203. count_range_1: 2,
  204. count_range_2: 6,
  205. count_percentage: 3,
  206. breakpoint: 1686967200,
  207. trend_percentage: 1.9235225955967554,
  208. trend_difference: 797,
  209. aggregate_range_1: 863,
  210. aggregate_range_2: 1660,
  211. transaction: '/organizations/:orgId/performance/',
  212. },
  213. {
  214. count: 60,
  215. project: 'internal',
  216. count_range_1: 20,
  217. count_range_2: 40,
  218. count_percentage: 2,
  219. breakpoint: 1686967200,
  220. trend_percentage: 1.204968944099379,
  221. trend_difference: 66,
  222. aggregate_range_1: 322,
  223. aggregate_range_2: 388,
  224. transaction: '/api/0/internal/health/',
  225. },
  226. ],
  227. },
  228. },
  229. });
  230. MockApiClient.addMockResponse({
  231. url: '/organizations/org-slug/events/',
  232. body: {
  233. data: [
  234. {
  235. 'p95()': 1010.9232499999998,
  236. 'p50()': 47.34580982348902,
  237. 'tps()': 3.7226926286168966,
  238. 'count()': 34872349,
  239. 'failure_rate()': 0.43428379,
  240. 'examples()': ['djk3w308er', '3298a9ui3h'],
  241. },
  242. ],
  243. meta: {
  244. fields: {
  245. 'p95()': 'duration',
  246. '950()': 'duration',
  247. 'tps()': 'number',
  248. 'count()': 'number',
  249. 'failure_rate()': 'number',
  250. 'examples()': 'Array',
  251. },
  252. units: {
  253. 'p95()': 'millisecond',
  254. 'p50()': 'millisecond',
  255. 'tps()': null,
  256. 'count()': null,
  257. 'failure_rate()': null,
  258. 'examples()': null,
  259. },
  260. isMetricsData: true,
  261. tips: {},
  262. dataset: 'metrics',
  263. },
  264. },
  265. });
  266. MockApiClient.addMockResponse({
  267. url: '/organizations/org-slug/events-spans-performance/',
  268. body: [],
  269. });
  270. });
  271. afterEach(function () {
  272. MockApiClient.clearMockResponses();
  273. act(() => ProjectsStore.reset());
  274. });
  275. it('renders basic UI elements', async function () {
  276. const data = _initializeData({});
  277. render(
  278. <TrendsIndex location={data.router.location} organization={data.organization} />,
  279. {
  280. context: data.routerContext,
  281. organization: data.organization,
  282. }
  283. );
  284. expect(await getTrendDropdown()).toBeInTheDocument();
  285. expect(await getParameterDropdown()).toBeInTheDocument();
  286. expect(screen.getAllByTestId('changed-transactions')).toHaveLength(2);
  287. });
  288. it('transaction list items are rendered', async function () {
  289. const data = _initializeData({});
  290. render(
  291. <TrendsIndex location={data.router.location} organization={data.organization} />,
  292. {
  293. context: data.routerContext,
  294. organization: data.organization,
  295. }
  296. );
  297. expect(await screen.findAllByTestId('trends-list-item-regression')).toHaveLength(2);
  298. expect(await screen.findAllByTestId('trends-list-item-improved')).toHaveLength(2);
  299. });
  300. it('view summary menu action links to the correct view', async function () {
  301. const projects = [ProjectFixture({id: '1', slug: 'internal'}), ProjectFixture()];
  302. const data = initializeTrendsData(projects, {project: ['1']});
  303. render(
  304. <TrendsIndex location={data.router.location} organization={data.organization} />,
  305. {
  306. context: data.routerContext,
  307. organization: data.organization,
  308. }
  309. );
  310. const transactions = await screen.findAllByTestId('trends-list-item-improved');
  311. expect(transactions).toHaveLength(2);
  312. const firstTransaction = transactions[0];
  313. const summaryLink = within(firstTransaction).getByTestId('item-transaction-name');
  314. expect(summaryLink.closest('a')).toHaveAttribute(
  315. 'href',
  316. '/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'
  317. );
  318. });
  319. it('view summary menu action opens performance change explorer with feature flag', async function () {
  320. const projects = [ProjectFixture({id: '1', slug: 'internal'}), ProjectFixture()];
  321. const data = initializeTrendsData(projects, {project: ['1']}, true, [
  322. 'performance-change-explorer',
  323. ]);
  324. render(
  325. <TrendsIndex location={data.router.location} organization={data.organization} />,
  326. {
  327. context: data.routerContext,
  328. organization: data.organization,
  329. }
  330. );
  331. const transactions = await screen.findAllByTestId('trends-list-item-improved');
  332. expect(transactions).toHaveLength(2);
  333. const firstTransaction = transactions[0];
  334. const summaryLink = within(firstTransaction).getByTestId('item-transaction-name');
  335. expect(summaryLink.closest('a')).not.toHaveAttribute('href');
  336. await clickEl(summaryLink);
  337. await waitFor(() => {
  338. expect(screen.getByText('Ongoing Improvement')).toBeInTheDocument();
  339. expect(screen.getByText('Throughput')).toBeInTheDocument();
  340. expect(screen.getByText('P95')).toBeInTheDocument();
  341. expect(screen.getByText('P50')).toBeInTheDocument();
  342. expect(screen.getByText('Failure Rate')).toBeInTheDocument();
  343. });
  344. });
  345. it('hide from list menu action modifies query', async function () {
  346. const projects = [ProjectFixture({id: '1', slug: 'internal'}), ProjectFixture()];
  347. const data = initializeTrendsData(projects, {project: ['1']});
  348. render(
  349. <TrendsIndex location={data.router.location} organization={data.organization} />,
  350. {
  351. context: data.routerContext,
  352. organization: data.organization,
  353. }
  354. );
  355. const transactions = await screen.findAllByTestId('trends-list-item-improved');
  356. expect(transactions).toHaveLength(2);
  357. const firstTransaction = transactions[0];
  358. const menuActions = within(firstTransaction).getAllByTestId('menu-action');
  359. expect(menuActions).toHaveLength(3);
  360. const menuAction = menuActions[2];
  361. await clickEl(menuAction);
  362. expect(browserHistory.push).toHaveBeenCalledWith({
  363. query: expect.objectContaining({
  364. project: expect.anything(),
  365. query: `tpm():>0.01 transaction.duration:>0 transaction.duration:<${DEFAULT_MAX_DURATION} !transaction:/organizations/:orgId/performance/`,
  366. }),
  367. });
  368. });
  369. it('Changing search causes cursors to be reset', async function () {
  370. const projects = [ProjectFixture({id: '1', slug: 'internal'}), ProjectFixture()];
  371. const data = initializeTrendsData(projects, {project: ['1']});
  372. render(
  373. <TrendsIndex location={data.router.location} organization={data.organization} />,
  374. {
  375. context: data.routerContext,
  376. organization: data.organization,
  377. }
  378. );
  379. const input = await screen.findByTestId('smart-search-input');
  380. enterSearch(input, 'transaction.duration:>9000');
  381. expect(browserHistory.push).toHaveBeenCalledWith({
  382. pathname: undefined,
  383. query: expect.objectContaining({
  384. project: ['1'],
  385. query: 'transaction.duration:>9000',
  386. improvedCursor: undefined,
  387. regressionCursor: undefined,
  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('Percentilep50');
  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', 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. expect(browserHistory.push).toHaveBeenNthCalledWith(
  646. 1,
  647. expect.objectContaining({
  648. query: {
  649. query: `tpm():>0.01 transaction.duration:>0 transaction.duration:<${DEFAULT_MAX_DURATION}`,
  650. },
  651. })
  652. );
  653. });
  654. it('Navigating away from trends will remove extra tags from query', async function () {
  655. const data = initializeTrendsData(
  656. undefined,
  657. {
  658. query: `device.family:Mac tpm():>0.01 transaction.duration:>0 transaction.duration:<${DEFAULT_MAX_DURATION}`,
  659. },
  660. false
  661. );
  662. render(
  663. <TrendsIndex location={data.router.location} organization={data.organization} />,
  664. {
  665. context: data.routerContext,
  666. organization: data.organization,
  667. }
  668. );
  669. (browserHistory.push as any).mockReset();
  670. const byTransactionLink = await screen.findByTestId('breadcrumb-link');
  671. expect(byTransactionLink.closest('a')).toHaveAttribute(
  672. 'href',
  673. '/organizations/org-slug/performance/?query=device.family%3AMac'
  674. );
  675. });
  676. });