index.spec.tsx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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. },
  243. ],
  244. meta: {
  245. fields: {
  246. 'p95()': 'duration',
  247. '950()': 'duration',
  248. 'tps()': 'number',
  249. },
  250. units: {
  251. 'p95()': 'millisecond',
  252. 'p50()': 'millisecond',
  253. 'tps()': null,
  254. },
  255. isMetricsData: true,
  256. tips: {},
  257. dataset: 'metrics',
  258. },
  259. },
  260. });
  261. MockApiClient.addMockResponse({
  262. url: '/organizations/org-slug/events-spans-performance/',
  263. body: [],
  264. });
  265. });
  266. afterEach(function () {
  267. MockApiClient.clearMockResponses();
  268. act(() => ProjectsStore.reset());
  269. });
  270. it('renders basic UI elements', async function () {
  271. const data = _initializeData({});
  272. render(
  273. <TrendsIndex location={data.router.location} organization={data.organization} />,
  274. {
  275. context: data.routerContext,
  276. organization: data.organization,
  277. }
  278. );
  279. expect(await getTrendDropdown()).toBeInTheDocument();
  280. expect(await getParameterDropdown()).toBeInTheDocument();
  281. expect(screen.getAllByTestId('changed-transactions')).toHaveLength(2);
  282. });
  283. it('transaction list items are rendered', async function () {
  284. const data = _initializeData({});
  285. render(
  286. <TrendsIndex location={data.router.location} organization={data.organization} />,
  287. {
  288. context: data.routerContext,
  289. organization: data.organization,
  290. }
  291. );
  292. expect(await screen.findAllByTestId('trends-list-item-regression')).toHaveLength(2);
  293. expect(await screen.findAllByTestId('trends-list-item-improved')).toHaveLength(2);
  294. });
  295. it('view summary menu action links to the correct view', async function () {
  296. const projects = [TestStubs.Project({id: 1, slug: 'internal'}), TestStubs.Project()];
  297. const data = initializeTrendsData(projects, {project: ['1']});
  298. render(
  299. <TrendsIndex location={data.router.location} organization={data.organization} />,
  300. {
  301. context: data.routerContext,
  302. organization: data.organization,
  303. }
  304. );
  305. const transactions = await screen.findAllByTestId('trends-list-item-improved');
  306. expect(transactions).toHaveLength(2);
  307. const firstTransaction = transactions[0];
  308. const summaryLink = within(firstTransaction).getByTestId('item-transaction-name');
  309. expect(summaryLink.closest('a')).toHaveAttribute(
  310. 'href',
  311. '/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'
  312. );
  313. });
  314. it('view summary menu action opens performance change explorer with feature flag', async function () {
  315. const projects = [TestStubs.Project({id: 1, slug: 'internal'}), TestStubs.Project()];
  316. const data = initializeTrendsData(projects, {project: ['1']}, true, [
  317. 'performance-change-explorer',
  318. ]);
  319. render(
  320. <TrendsIndex location={data.router.location} organization={data.organization} />,
  321. {
  322. context: data.routerContext,
  323. organization: data.organization,
  324. }
  325. );
  326. const transactions = await screen.findAllByTestId('trends-list-item-improved');
  327. expect(transactions).toHaveLength(2);
  328. const firstTransaction = transactions[0];
  329. const summaryLink = within(firstTransaction).getByTestId('item-transaction-name');
  330. expect(summaryLink.closest('a')).not.toHaveAttribute('href');
  331. await clickEl(summaryLink);
  332. await waitFor(() => {
  333. expect(screen.getByText('Ongoing Improvement')).toBeInTheDocument();
  334. expect(screen.getByText('Throughput')).toBeInTheDocument();
  335. expect(screen.getByText('P95')).toBeInTheDocument();
  336. expect(screen.getByText('P50')).toBeInTheDocument();
  337. expect(screen.getByText('Errors')).toBeInTheDocument();
  338. });
  339. });
  340. it('hide from list menu action modifies query', async function () {
  341. const projects = [TestStubs.Project({id: 1, slug: 'internal'}), TestStubs.Project()];
  342. const data = initializeTrendsData(projects, {project: ['1']});
  343. render(
  344. <TrendsIndex location={data.router.location} organization={data.organization} />,
  345. {
  346. context: data.routerContext,
  347. organization: data.organization,
  348. }
  349. );
  350. const transactions = await screen.findAllByTestId('trends-list-item-improved');
  351. expect(transactions).toHaveLength(2);
  352. const firstTransaction = transactions[0];
  353. const menuActions = within(firstTransaction).getAllByTestId('menu-action');
  354. expect(menuActions).toHaveLength(3);
  355. const menuAction = menuActions[2];
  356. await clickEl(menuAction);
  357. expect(browserHistory.push).toHaveBeenCalledWith({
  358. query: expect.objectContaining({
  359. project: expect.anything(),
  360. query: `tpm():>0.01 transaction.duration:>0 transaction.duration:<${DEFAULT_MAX_DURATION} !transaction:/organizations/:orgId/performance/`,
  361. }),
  362. });
  363. });
  364. it('Changing search causes cursors to be reset', async function () {
  365. const projects = [TestStubs.Project({id: 1, slug: 'internal'}), TestStubs.Project()];
  366. const data = initializeTrendsData(projects, {project: ['1']});
  367. render(
  368. <TrendsIndex location={data.router.location} organization={data.organization} />,
  369. {
  370. context: data.routerContext,
  371. organization: data.organization,
  372. }
  373. );
  374. const input = await screen.findByTestId('smart-search-input');
  375. enterSearch(input, 'transaction.duration:>9000');
  376. expect(browserHistory.push).toHaveBeenCalledWith({
  377. pathname: undefined,
  378. query: expect.objectContaining({
  379. project: ['1'],
  380. query: 'transaction.duration:>9000',
  381. improvedCursor: undefined,
  382. regressionCursor: undefined,
  383. }),
  384. });
  385. });
  386. it('exclude greater than list menu action modifies query', async function () {
  387. const projects = [TestStubs.Project({id: 1, slug: 'internal'}), TestStubs.Project()];
  388. const data = initializeTrendsData(projects, {project: ['1']});
  389. render(
  390. <TrendsIndex location={data.router.location} organization={data.organization} />,
  391. {
  392. context: data.routerContext,
  393. organization: data.organization,
  394. }
  395. );
  396. const transactions = await screen.findAllByTestId('trends-list-item-improved');
  397. expect(transactions).toHaveLength(2);
  398. const firstTransaction = transactions[0];
  399. const menuActions = within(firstTransaction).getAllByTestId('menu-action');
  400. expect(menuActions).toHaveLength(3);
  401. const menuAction = menuActions[0];
  402. await clickEl(menuAction);
  403. expect(browserHistory.push).toHaveBeenCalledWith({
  404. query: expect.objectContaining({
  405. project: expect.anything(),
  406. query: 'tpm():>0.01 transaction.duration:>0 transaction.duration:<=863',
  407. }),
  408. });
  409. });
  410. it('exclude less than list menu action modifies query', async function () {
  411. const projects = [TestStubs.Project({id: 1, slug: 'internal'}), TestStubs.Project()];
  412. const data = initializeTrendsData(projects, {project: ['1']});
  413. render(
  414. <TrendsIndex location={data.router.location} organization={data.organization} />,
  415. {
  416. context: data.routerContext,
  417. organization: data.organization,
  418. }
  419. );
  420. const transactions = await screen.findAllByTestId('trends-list-item-improved');
  421. expect(transactions).toHaveLength(2);
  422. const firstTransaction = transactions[0];
  423. const menuActions = within(firstTransaction).getAllByTestId('menu-action');
  424. expect(menuActions).toHaveLength(3);
  425. const menuAction = menuActions[1];
  426. await clickEl(menuAction);
  427. expect(browserHistory.push).toHaveBeenCalledWith({
  428. query: expect.objectContaining({
  429. project: expect.anything(),
  430. query: 'tpm():>0.01 transaction.duration:<15min transaction.duration:>=863',
  431. }),
  432. });
  433. });
  434. it('choosing a trend function changes location', async function () {
  435. const projects = [TestStubs.Project()];
  436. const data = initializeTrendsData(projects, {project: ['-1']});
  437. render(
  438. <TrendsIndex location={data.router.location} organization={data.organization} />,
  439. {
  440. context: data.routerContext,
  441. organization: data.organization,
  442. }
  443. );
  444. for (const trendFunction of TRENDS_FUNCTIONS) {
  445. // Open dropdown
  446. const dropdown = await getTrendDropdown();
  447. await clickEl(dropdown);
  448. // Select function
  449. const option = screen.getByRole('option', {name: trendFunction.label});
  450. await clickEl(option);
  451. expect(browserHistory.push).toHaveBeenCalledWith({
  452. query: expect.objectContaining({
  453. regressionCursor: undefined,
  454. improvedCursor: undefined,
  455. trendFunction: trendFunction.field,
  456. }),
  457. });
  458. }
  459. });
  460. it('sets LCP as a default trend parameter for frontend project if query does not specify trend parameter', async function () {
  461. const projects = [TestStubs.Project({id: 1, platform: 'javascript'})];
  462. const data = initializeTrendsData(projects, {project: [1]});
  463. render(
  464. <TrendsIndex location={data.router.location} organization={data.organization} />,
  465. {
  466. context: data.routerContext,
  467. organization: data.organization,
  468. }
  469. );
  470. const trendDropdownButton = await getTrendDropdown();
  471. expect(trendDropdownButton).toHaveTextContent('Percentilep50');
  472. });
  473. it('sets duration as a default trend parameter for backend project if query does not specify trend parameter', async function () {
  474. const projects = [TestStubs.Project({id: 1, platform: 'python'})];
  475. const data = initializeTrendsData(projects, {project: [1]});
  476. render(
  477. <TrendsIndex location={data.router.location} organization={data.organization} />,
  478. {
  479. context: data.routerContext,
  480. organization: data.organization,
  481. }
  482. );
  483. const parameterDropdownButton = await getParameterDropdown();
  484. expect(parameterDropdownButton).toHaveTextContent('ParameterDuration');
  485. });
  486. it('sets trend parameter from query and ignores default trend parameter', async function () {
  487. const projects = [TestStubs.Project({id: 1, platform: 'javascript'})];
  488. const data = initializeTrendsData(projects, {project: [1], trendParameter: 'FCP'});
  489. render(
  490. <TrendsIndex location={data.router.location} organization={data.organization} />,
  491. {
  492. context: data.routerContext,
  493. organization: data.organization,
  494. }
  495. );
  496. const parameterDropdownButton = await getParameterDropdown();
  497. expect(parameterDropdownButton).toHaveTextContent('ParameterFCP');
  498. });
  499. it('choosing a parameter changes location', async function () {
  500. const projects = [TestStubs.Project()];
  501. const data = initializeTrendsData(projects, {project: ['-1']});
  502. render(
  503. <TrendsIndex location={data.router.location} organization={data.organization} />,
  504. {
  505. context: data.routerContext,
  506. organization: data.organization,
  507. }
  508. );
  509. for (const parameter of TRENDS_PARAMETERS) {
  510. // Open dropdown
  511. const dropdown = await getParameterDropdown();
  512. await clickEl(dropdown);
  513. // Select parameter
  514. const option = screen.getByRole('option', {name: parameter.label});
  515. await clickEl(option);
  516. expect(browserHistory.push).toHaveBeenCalledWith({
  517. query: expect.objectContaining({
  518. trendParameter: parameter.label,
  519. }),
  520. });
  521. }
  522. });
  523. it('choosing a web vitals parameter adds it as an additional condition to the query', async function () {
  524. const projects = [TestStubs.Project()];
  525. const data = initializeTrendsData(projects, {project: ['-1']});
  526. const {rerender} = render(
  527. <TrendsIndex location={data.router.location} organization={data.organization} />,
  528. {
  529. context: data.routerContext,
  530. organization: data.organization,
  531. }
  532. );
  533. for (const parameter of TRENDS_PARAMETERS) {
  534. if (Object.values(WebVital).includes(parameter.column as string as WebVital)) {
  535. trendsStatsMock.mockReset();
  536. const newLocation = {
  537. query: {...trendsViewQuery, trendParameter: parameter.label},
  538. };
  539. rerender(
  540. <TrendsIndex
  541. location={newLocation as unknown as Location}
  542. organization={data.organization}
  543. />
  544. );
  545. await waitForMockCall(trendsStatsMock);
  546. expect(trendsStatsMock).toHaveBeenCalledTimes(2);
  547. // Improved transactions call
  548. expect(trendsStatsMock).toHaveBeenNthCalledWith(
  549. 1,
  550. expect.anything(),
  551. expect.objectContaining({
  552. query: expect.objectContaining({
  553. query: expect.stringContaining(`has:${parameter.column}`),
  554. }),
  555. })
  556. );
  557. // Regression transactions call
  558. expect(trendsStatsMock).toHaveBeenNthCalledWith(
  559. 2,
  560. expect.anything(),
  561. expect.objectContaining({
  562. query: expect.objectContaining({
  563. query: expect.stringContaining(`has:${parameter.column}`),
  564. }),
  565. })
  566. );
  567. }
  568. }
  569. });
  570. it('trend functions in location make api calls', async function () {
  571. const projects = [TestStubs.Project(), TestStubs.Project()];
  572. const data = initializeTrendsData(projects, {project: ['-1']});
  573. const {rerender} = render(
  574. <TrendsIndex location={data.router.location} organization={data.organization} />,
  575. {
  576. context: data.routerContext,
  577. organization: data.organization,
  578. }
  579. );
  580. for (const trendFunction of TRENDS_FUNCTIONS) {
  581. trendsStatsMock.mockReset();
  582. const newLocation = {
  583. query: {...trendsViewQuery, trendFunction: trendFunction.field},
  584. };
  585. rerender(
  586. <TrendsIndex
  587. location={newLocation as unknown as Location}
  588. organization={data.organization}
  589. />
  590. );
  591. await waitForMockCall(trendsStatsMock);
  592. expect(trendsStatsMock).toHaveBeenCalledTimes(2);
  593. const sort = 'trend_percentage()';
  594. const defaultTrendsFields = ['project'];
  595. const transactionFields = ['transaction', ...defaultTrendsFields];
  596. const projectFields = [...defaultTrendsFields];
  597. expect(transactionFields).toHaveLength(2);
  598. expect(projectFields).toHaveLength(transactionFields.length - 1);
  599. // Improved transactions call
  600. expect(trendsStatsMock).toHaveBeenNthCalledWith(
  601. 1,
  602. expect.anything(),
  603. expect.objectContaining({
  604. query: expect.objectContaining({
  605. trendFunction: `${trendFunction.field}(transaction.duration)`,
  606. sort,
  607. query: expect.stringContaining('trend_percentage():>0%'),
  608. interval: '1h',
  609. field: transactionFields,
  610. statsPeriod: '14d',
  611. }),
  612. })
  613. );
  614. // Regression transactions call
  615. expect(trendsStatsMock).toHaveBeenNthCalledWith(
  616. 2,
  617. expect.anything(),
  618. expect.objectContaining({
  619. query: expect.objectContaining({
  620. trendFunction: `${trendFunction.field}(transaction.duration)`,
  621. sort: '-' + sort,
  622. query: expect.stringContaining('trend_percentage():>0%'),
  623. interval: '1h',
  624. field: transactionFields,
  625. statsPeriod: '14d',
  626. }),
  627. })
  628. );
  629. }
  630. });
  631. it('Visiting trends with trends feature will update filters if none are set', function () {
  632. const data = initializeTrendsData(undefined, {}, false);
  633. render(
  634. <TrendsIndex location={data.router.location} organization={data.organization} />,
  635. {
  636. context: data.routerContext,
  637. organization: data.organization,
  638. }
  639. );
  640. expect(browserHistory.push).toHaveBeenNthCalledWith(
  641. 1,
  642. expect.objectContaining({
  643. query: {
  644. query: `tpm():>0.01 transaction.duration:>0 transaction.duration:<${DEFAULT_MAX_DURATION}`,
  645. },
  646. })
  647. );
  648. });
  649. it('Navigating away from trends will remove extra tags from query', async function () {
  650. const data = initializeTrendsData(
  651. undefined,
  652. {
  653. query: `device.family:Mac tpm():>0.01 transaction.duration:>0 transaction.duration:<${DEFAULT_MAX_DURATION}`,
  654. },
  655. false
  656. );
  657. render(
  658. <TrendsIndex location={data.router.location} organization={data.organization} />,
  659. {
  660. context: data.routerContext,
  661. organization: data.organization,
  662. }
  663. );
  664. (browserHistory.push as any).mockReset();
  665. const byTransactionLink = await screen.findByTestId('breadcrumb-link');
  666. expect(byTransactionLink.closest('a')).toHaveAttribute(
  667. 'href',
  668. '/organizations/org-slug/performance/?query=device.family%3AMac'
  669. );
  670. });
  671. });