index.spec.tsx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. import {browserHistory} from 'react-router';
  2. import type {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 type {InitializeDataSettings} from 'sentry-test/performance/initializePerformanceData';
  7. import {initializeData} 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('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=p50&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. expect(browserHistory.push).toHaveBeenCalledWith({
  380. pathname: undefined,
  381. query: expect.objectContaining({
  382. project: ['1'],
  383. query: 'transaction.duration:>9000',
  384. improvedCursor: undefined,
  385. regressionCursor: undefined,
  386. }),
  387. });
  388. });
  389. it('exclude greater than list menu action modifies query', async function () {
  390. const projects = [ProjectFixture({id: '1', slug: 'internal'}), ProjectFixture()];
  391. const data = initializeTrendsData(projects, {project: ['1']});
  392. render(
  393. <TrendsIndex location={data.router.location} organization={data.organization} />,
  394. {
  395. context: data.routerContext,
  396. organization: data.organization,
  397. }
  398. );
  399. const transactions = await screen.findAllByTestId('trends-list-item-improved');
  400. expect(transactions).toHaveLength(2);
  401. const firstTransaction = transactions[0];
  402. const menuActions = within(firstTransaction).getAllByTestId('menu-action');
  403. expect(menuActions).toHaveLength(3);
  404. const menuAction = menuActions[0];
  405. await clickEl(menuAction);
  406. expect(browserHistory.push).toHaveBeenCalledWith({
  407. query: expect.objectContaining({
  408. project: expect.anything(),
  409. query: 'tpm():>0.01 transaction.duration:>0 transaction.duration:<=863',
  410. }),
  411. });
  412. });
  413. it('exclude less than list menu action modifies query', async function () {
  414. const projects = [ProjectFixture({id: '1', slug: 'internal'}), ProjectFixture()];
  415. const data = initializeTrendsData(projects, {project: ['1']});
  416. render(
  417. <TrendsIndex location={data.router.location} organization={data.organization} />,
  418. {
  419. context: data.routerContext,
  420. organization: data.organization,
  421. }
  422. );
  423. const transactions = await screen.findAllByTestId('trends-list-item-improved');
  424. expect(transactions).toHaveLength(2);
  425. const firstTransaction = transactions[0];
  426. const menuActions = within(firstTransaction).getAllByTestId('menu-action');
  427. expect(menuActions).toHaveLength(3);
  428. const menuAction = menuActions[1];
  429. await clickEl(menuAction);
  430. expect(browserHistory.push).toHaveBeenCalledWith({
  431. query: expect.objectContaining({
  432. project: expect.anything(),
  433. query: 'tpm():>0.01 transaction.duration:<15min transaction.duration:>=863',
  434. }),
  435. });
  436. });
  437. it('choosing a trend function changes location', async function () {
  438. const projects = [ProjectFixture()];
  439. const data = initializeTrendsData(projects, {project: ['-1']});
  440. render(
  441. <TrendsIndex location={data.router.location} organization={data.organization} />,
  442. {
  443. context: data.routerContext,
  444. organization: data.organization,
  445. }
  446. );
  447. for (const trendFunction of TRENDS_FUNCTIONS) {
  448. // Open dropdown
  449. const dropdown = await getTrendDropdown();
  450. await clickEl(dropdown);
  451. // Select function
  452. const option = screen.getByRole('option', {name: trendFunction.label});
  453. await clickEl(option);
  454. expect(browserHistory.push).toHaveBeenCalledWith({
  455. query: expect.objectContaining({
  456. regressionCursor: undefined,
  457. improvedCursor: undefined,
  458. trendFunction: trendFunction.field,
  459. }),
  460. });
  461. }
  462. });
  463. it('sets LCP as a default trend parameter for frontend project if query does not specify trend parameter', async function () {
  464. const projects = [ProjectFixture({id: '1', platform: 'javascript'})];
  465. const data = initializeTrendsData(projects, {project: [1]});
  466. render(
  467. <TrendsIndex location={data.router.location} organization={data.organization} />,
  468. {
  469. context: data.routerContext,
  470. organization: data.organization,
  471. }
  472. );
  473. const trendDropdownButton = await getTrendDropdown();
  474. expect(trendDropdownButton).toHaveTextContent('Percentilep50');
  475. });
  476. it('sets duration as a default trend parameter for backend project if query does not specify trend parameter', async function () {
  477. const projects = [ProjectFixture({id: '1', platform: 'python'})];
  478. const data = initializeTrendsData(projects, {project: [1]});
  479. render(
  480. <TrendsIndex location={data.router.location} organization={data.organization} />,
  481. {
  482. context: data.routerContext,
  483. organization: data.organization,
  484. }
  485. );
  486. const parameterDropdownButton = await getParameterDropdown();
  487. expect(parameterDropdownButton).toHaveTextContent('ParameterDuration');
  488. });
  489. it('sets trend parameter from query and ignores default trend parameter', async function () {
  490. const projects = [ProjectFixture({id: '1', platform: 'javascript'})];
  491. const data = initializeTrendsData(projects, {project: [1], trendParameter: 'FCP'});
  492. render(
  493. <TrendsIndex location={data.router.location} organization={data.organization} />,
  494. {
  495. context: data.routerContext,
  496. organization: data.organization,
  497. }
  498. );
  499. const parameterDropdownButton = await getParameterDropdown();
  500. expect(parameterDropdownButton).toHaveTextContent('ParameterFCP');
  501. });
  502. it('choosing a parameter changes location', async function () {
  503. const projects = [ProjectFixture()];
  504. const data = initializeTrendsData(projects, {project: ['-1']});
  505. render(
  506. <TrendsIndex location={data.router.location} organization={data.organization} />,
  507. {
  508. context: data.routerContext,
  509. organization: data.organization,
  510. }
  511. );
  512. for (const parameter of TRENDS_PARAMETERS) {
  513. // Open dropdown
  514. const dropdown = await getParameterDropdown();
  515. await clickEl(dropdown);
  516. // Select parameter
  517. const option = screen.getByRole('option', {name: parameter.label});
  518. await clickEl(option);
  519. expect(browserHistory.push).toHaveBeenCalledWith({
  520. query: expect.objectContaining({
  521. trendParameter: parameter.label,
  522. }),
  523. });
  524. }
  525. });
  526. it('choosing a web vitals parameter adds it as an additional condition to the query', async function () {
  527. const projects = [ProjectFixture()];
  528. const data = initializeTrendsData(projects, {project: ['-1']});
  529. const {rerender} = render(
  530. <TrendsIndex location={data.router.location} organization={data.organization} />,
  531. {
  532. context: data.routerContext,
  533. organization: data.organization,
  534. }
  535. );
  536. for (const parameter of TRENDS_PARAMETERS) {
  537. if (Object.values(WebVital).includes(parameter.column as string as WebVital)) {
  538. trendsStatsMock.mockReset();
  539. const newLocation = {
  540. query: {...trendsViewQuery, trendParameter: parameter.label},
  541. };
  542. rerender(
  543. <TrendsIndex
  544. location={newLocation as unknown as Location}
  545. organization={data.organization}
  546. />
  547. );
  548. await waitForMockCall(trendsStatsMock);
  549. expect(trendsStatsMock).toHaveBeenCalledTimes(2);
  550. // Improved transactions call
  551. expect(trendsStatsMock).toHaveBeenNthCalledWith(
  552. 1,
  553. expect.anything(),
  554. expect.objectContaining({
  555. query: expect.objectContaining({
  556. query: expect.stringContaining(`has:${parameter.column}`),
  557. }),
  558. })
  559. );
  560. // Regression transactions call
  561. expect(trendsStatsMock).toHaveBeenNthCalledWith(
  562. 2,
  563. expect.anything(),
  564. expect.objectContaining({
  565. query: expect.objectContaining({
  566. query: expect.stringContaining(`has:${parameter.column}`),
  567. }),
  568. })
  569. );
  570. }
  571. }
  572. });
  573. it('trend functions in location make api calls', async function () {
  574. const projects = [ProjectFixture(), ProjectFixture()];
  575. const data = initializeTrendsData(projects, {project: ['-1']});
  576. const {rerender} = render(
  577. <TrendsIndex location={data.router.location} organization={data.organization} />,
  578. {
  579. context: data.routerContext,
  580. organization: data.organization,
  581. }
  582. );
  583. for (const trendFunction of TRENDS_FUNCTIONS) {
  584. trendsStatsMock.mockReset();
  585. const newLocation = {
  586. query: {...trendsViewQuery, trendFunction: trendFunction.field},
  587. };
  588. rerender(
  589. <TrendsIndex
  590. location={newLocation as unknown as Location}
  591. organization={data.organization}
  592. />
  593. );
  594. await waitForMockCall(trendsStatsMock);
  595. expect(trendsStatsMock).toHaveBeenCalledTimes(2);
  596. const sort = 'trend_percentage()';
  597. const defaultTrendsFields = ['project'];
  598. const transactionFields = ['transaction', ...defaultTrendsFields];
  599. const projectFields = [...defaultTrendsFields];
  600. expect(transactionFields).toHaveLength(2);
  601. expect(projectFields).toHaveLength(transactionFields.length - 1);
  602. // Improved transactions call
  603. expect(trendsStatsMock).toHaveBeenNthCalledWith(
  604. 1,
  605. expect.anything(),
  606. expect.objectContaining({
  607. query: expect.objectContaining({
  608. trendFunction: `${trendFunction.field}(transaction.duration)`,
  609. sort,
  610. query: expect.stringContaining('trend_percentage():>0%'),
  611. interval: '1h',
  612. field: transactionFields,
  613. statsPeriod: '14d',
  614. }),
  615. })
  616. );
  617. // Regression transactions call
  618. expect(trendsStatsMock).toHaveBeenNthCalledWith(
  619. 2,
  620. expect.anything(),
  621. expect.objectContaining({
  622. query: expect.objectContaining({
  623. trendFunction: `${trendFunction.field}(transaction.duration)`,
  624. sort: '-' + sort,
  625. query: expect.stringContaining('trend_percentage():>0%'),
  626. interval: '1h',
  627. field: transactionFields,
  628. statsPeriod: '14d',
  629. }),
  630. })
  631. );
  632. }
  633. });
  634. it('Visiting trends with trends feature will update filters if none are set', function () {
  635. const data = initializeTrendsData(undefined, {}, false);
  636. render(
  637. <TrendsIndex location={data.router.location} organization={data.organization} />,
  638. {
  639. context: data.routerContext,
  640. organization: data.organization,
  641. }
  642. );
  643. expect(browserHistory.push).toHaveBeenNthCalledWith(
  644. 1,
  645. expect.objectContaining({
  646. query: {
  647. query: `tpm():>0.01 transaction.duration:>0 transaction.duration:<${DEFAULT_MAX_DURATION}`,
  648. },
  649. })
  650. );
  651. });
  652. it('Navigating away from trends will remove extra tags from query', async function () {
  653. const data = initializeTrendsData(
  654. undefined,
  655. {
  656. query: `device.family:Mac tpm():>0.01 transaction.duration:>0 transaction.duration:<${DEFAULT_MAX_DURATION}`,
  657. },
  658. false
  659. );
  660. render(
  661. <TrendsIndex location={data.router.location} organization={data.organization} />,
  662. {
  663. context: data.routerContext,
  664. organization: data.organization,
  665. }
  666. );
  667. (browserHistory.push as any).mockReset();
  668. const byTransactionLink = await screen.findByTestId('breadcrumb-link');
  669. expect(byTransactionLink.closest('a')).toHaveAttribute(
  670. 'href',
  671. '/organizations/org-slug/performance/?query=device.family%3AMac'
  672. );
  673. });
  674. });