index.spec.tsx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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({features});
  118. const newQuery = {...(includeDefaultQuery ? trendsViewQuery : {}), ...query};
  119. const initialData = initializeOrg({
  120. organization,
  121. router: {
  122. location: {
  123. query: newQuery,
  124. },
  125. },
  126. projects: _projects,
  127. });
  128. act(() => ProjectsStore.loadInitialData(initialData.projects));
  129. return initialData;
  130. }
  131. describe('Performance > Trends', function () {
  132. let trendsStatsMock;
  133. beforeEach(function () {
  134. browserHistory.push = jest.fn();
  135. MockApiClient.addMockResponse({
  136. url: '/organizations/org-slug/projects/',
  137. body: [],
  138. });
  139. MockApiClient.addMockResponse({
  140. url: '/organizations/org-slug/tags/',
  141. body: [],
  142. });
  143. MockApiClient.addMockResponse({
  144. url: '/organizations/org-slug/users/',
  145. body: [],
  146. });
  147. MockApiClient.addMockResponse({
  148. url: '/organizations/org-slug/recent-searches/',
  149. body: [],
  150. });
  151. MockApiClient.addMockResponse({
  152. url: '/organizations/org-slug/recent-searches/',
  153. method: 'POST',
  154. body: [],
  155. });
  156. MockApiClient.addMockResponse({
  157. url: '/organizations/org-slug/sdk-updates/',
  158. body: [],
  159. });
  160. MockApiClient.addMockResponse({
  161. url: '/organizations/org-slug/prompts-activity/',
  162. body: {},
  163. });
  164. MockApiClient.addMockResponse({
  165. url: '/organizations/org-slug/releases/stats/',
  166. body: [],
  167. });
  168. MockApiClient.addMockResponse({
  169. url: '/organizations/org-slug/tags/transaction.duration/values/',
  170. body: [],
  171. });
  172. trendsStatsMock = MockApiClient.addMockResponse({
  173. url: '/organizations/org-slug/events-trends-stats/',
  174. body: {
  175. stats: {
  176. 'internal,/organizations/:orgId/performance/': {
  177. data: [[123, []]],
  178. },
  179. order: 0,
  180. },
  181. events: {
  182. meta: {
  183. count_range_1: 'integer',
  184. count_range_2: 'integer',
  185. count_percentage: 'percentage',
  186. breakpoint: 'number',
  187. trend_percentage: 'percentage',
  188. trend_difference: 'number',
  189. aggregate_range_1: 'duration',
  190. aggregate_range_2: 'duration',
  191. transaction: 'string',
  192. },
  193. data: [
  194. {
  195. count: 8,
  196. project: 'internal',
  197. count_range_1: 2,
  198. count_range_2: 6,
  199. count_percentage: 3,
  200. breakpoint: 1686967200,
  201. trend_percentage: 1.9235225955967554,
  202. trend_difference: 797,
  203. aggregate_range_1: 863,
  204. aggregate_range_2: 1660,
  205. transaction: '/organizations/:orgId/performance/',
  206. },
  207. {
  208. count: 60,
  209. project: 'internal',
  210. count_range_1: 20,
  211. count_range_2: 40,
  212. count_percentage: 2,
  213. breakpoint: 1686967200,
  214. trend_percentage: 1.204968944099379,
  215. trend_difference: 66,
  216. aggregate_range_1: 322,
  217. aggregate_range_2: 388,
  218. transaction: '/api/0/internal/health/',
  219. },
  220. ],
  221. },
  222. },
  223. });
  224. MockApiClient.addMockResponse({
  225. url: '/organizations/org-slug/events/',
  226. body: {
  227. data: [
  228. {
  229. 'p95()': 1010.9232499999998,
  230. 'p50()': 47.34580982348902,
  231. 'tps()': 3.7226926286168966,
  232. 'count()': 34872349,
  233. 'failure_rate()': 0.43428379,
  234. 'examples()': ['djk3w308er', '3298a9ui3h'],
  235. },
  236. ],
  237. meta: {
  238. fields: {
  239. 'p95()': 'duration',
  240. '950()': 'duration',
  241. 'tps()': 'number',
  242. 'count()': 'number',
  243. 'failure_rate()': 'number',
  244. 'examples()': 'Array',
  245. },
  246. units: {
  247. 'p95()': 'millisecond',
  248. 'p50()': 'millisecond',
  249. 'tps()': null,
  250. 'count()': null,
  251. 'failure_rate()': null,
  252. 'examples()': null,
  253. },
  254. isMetricsData: true,
  255. tips: {},
  256. dataset: 'metrics',
  257. },
  258. },
  259. });
  260. MockApiClient.addMockResponse({
  261. url: '/organizations/org-slug/events-spans-performance/',
  262. body: [],
  263. });
  264. });
  265. afterEach(function () {
  266. MockApiClient.clearMockResponses();
  267. act(() => ProjectsStore.reset());
  268. });
  269. it('renders basic UI elements', async function () {
  270. const data = _initializeData({});
  271. render(
  272. <TrendsIndex location={data.router.location} organization={data.organization} />,
  273. {
  274. router: data.router,
  275. organization: data.organization,
  276. }
  277. );
  278. expect(await getTrendDropdown()).toBeInTheDocument();
  279. expect(await getParameterDropdown()).toBeInTheDocument();
  280. expect(screen.getAllByTestId('changed-transactions')).toHaveLength(2);
  281. });
  282. it('transaction list items are rendered', async function () {
  283. const data = _initializeData({});
  284. render(
  285. <TrendsIndex location={data.router.location} organization={data.organization} />,
  286. {
  287. router: data.router,
  288. organization: data.organization,
  289. }
  290. );
  291. expect(await screen.findAllByTestId('trends-list-item-regression')).toHaveLength(2);
  292. expect(await screen.findAllByTestId('trends-list-item-improved')).toHaveLength(2);
  293. });
  294. it('view summary menu action links to the correct view', async function () {
  295. const projects = [ProjectFixture({id: '1', slug: 'internal'}), ProjectFixture()];
  296. const data = initializeTrendsData(projects, {project: ['1']});
  297. render(
  298. <TrendsIndex location={data.router.location} organization={data.organization} />,
  299. {
  300. router: data.router,
  301. organization: data.organization,
  302. }
  303. );
  304. const transactions = await screen.findAllByTestId('trends-list-item-improved');
  305. expect(transactions).toHaveLength(2);
  306. const firstTransaction = transactions[0];
  307. const summaryLink = within(firstTransaction).getByTestId('item-transaction-name');
  308. expect(summaryLink.closest('a')).toHaveAttribute(
  309. 'href',
  310. '/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'
  311. );
  312. });
  313. it('view summary menu action opens performance change explorer with feature flag', async function () {
  314. const projects = [ProjectFixture({id: '1', slug: 'internal'}), ProjectFixture()];
  315. const data = initializeTrendsData(projects, {project: ['1']}, true, [
  316. 'performance-change-explorer',
  317. ]);
  318. render(
  319. <TrendsIndex location={data.router.location} organization={data.organization} />,
  320. {
  321. router: data.router,
  322. organization: data.organization,
  323. }
  324. );
  325. const transactions = await screen.findAllByTestId('trends-list-item-improved');
  326. expect(transactions).toHaveLength(2);
  327. const firstTransaction = transactions[0];
  328. const summaryLink = within(firstTransaction).getByTestId('item-transaction-name');
  329. expect(summaryLink.closest('a')).not.toHaveAttribute('href');
  330. await clickEl(summaryLink);
  331. await waitFor(() => {
  332. expect(screen.getByText('Ongoing Improvement')).toBeInTheDocument();
  333. expect(screen.getByText('Throughput')).toBeInTheDocument();
  334. expect(screen.getByText('P95')).toBeInTheDocument();
  335. expect(screen.getByText('P50')).toBeInTheDocument();
  336. expect(screen.getByText('Failure Rate')).toBeInTheDocument();
  337. });
  338. });
  339. it('hide from list menu action modifies query', async function () {
  340. const projects = [ProjectFixture({id: '1', slug: 'internal'}), ProjectFixture()];
  341. const data = initializeTrendsData(projects, {project: ['1']});
  342. render(
  343. <TrendsIndex location={data.router.location} organization={data.organization} />,
  344. {
  345. router: data.router,
  346. organization: data.organization,
  347. }
  348. );
  349. const transactions = await screen.findAllByTestId('trends-list-item-improved');
  350. expect(transactions).toHaveLength(2);
  351. const firstTransaction = transactions[0];
  352. const menuActions = within(firstTransaction).getAllByTestId('menu-action');
  353. expect(menuActions).toHaveLength(3);
  354. const menuAction = menuActions[2];
  355. await clickEl(menuAction);
  356. expect(browserHistory.push).toHaveBeenCalledWith({
  357. query: expect.objectContaining({
  358. project: expect.anything(),
  359. query: `tpm():>0.01 transaction.duration:>0 transaction.duration:<${DEFAULT_MAX_DURATION} !transaction:/organizations/:orgId/performance/`,
  360. }),
  361. });
  362. });
  363. it('Changing search causes cursors to be reset', async function () {
  364. const projects = [ProjectFixture({id: '1', slug: 'internal'}), ProjectFixture()];
  365. const data = initializeTrendsData(projects, {project: ['1']});
  366. render(
  367. <TrendsIndex location={data.router.location} organization={data.organization} />,
  368. {
  369. router: data.router,
  370. organization: data.organization,
  371. }
  372. );
  373. const input = await screen.findByTestId('smart-search-input');
  374. enterSearch(input, 'transaction.duration:>9000');
  375. await waitFor(() =>
  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. });
  387. it('exclude greater than list menu action modifies query', async function () {
  388. const projects = [ProjectFixture({id: '1', slug: 'internal'}), ProjectFixture()];
  389. const data = initializeTrendsData(projects, {project: ['1']});
  390. render(
  391. <TrendsIndex location={data.router.location} organization={data.organization} />,
  392. {
  393. router: data.router,
  394. organization: data.organization,
  395. }
  396. );
  397. const transactions = await screen.findAllByTestId('trends-list-item-improved');
  398. expect(transactions).toHaveLength(2);
  399. const firstTransaction = transactions[0];
  400. const menuActions = within(firstTransaction).getAllByTestId('menu-action');
  401. expect(menuActions).toHaveLength(3);
  402. const menuAction = menuActions[0];
  403. await clickEl(menuAction);
  404. expect(browserHistory.push).toHaveBeenCalledWith({
  405. query: expect.objectContaining({
  406. project: expect.anything(),
  407. query: 'tpm():>0.01 transaction.duration:>0 transaction.duration:<=863',
  408. }),
  409. });
  410. });
  411. it('exclude less than list menu action modifies query', async function () {
  412. const projects = [ProjectFixture({id: '1', slug: 'internal'}), ProjectFixture()];
  413. const data = initializeTrendsData(projects, {project: ['1']});
  414. render(
  415. <TrendsIndex location={data.router.location} organization={data.organization} />,
  416. {
  417. router: data.router,
  418. organization: data.organization,
  419. }
  420. );
  421. const transactions = await screen.findAllByTestId('trends-list-item-improved');
  422. expect(transactions).toHaveLength(2);
  423. const firstTransaction = transactions[0];
  424. const menuActions = within(firstTransaction).getAllByTestId('menu-action');
  425. expect(menuActions).toHaveLength(3);
  426. const menuAction = menuActions[1];
  427. await clickEl(menuAction);
  428. expect(browserHistory.push).toHaveBeenCalledWith({
  429. query: expect.objectContaining({
  430. project: expect.anything(),
  431. query: 'tpm():>0.01 transaction.duration:<15min transaction.duration:>=863',
  432. }),
  433. });
  434. });
  435. it('choosing a trend function changes location', async function () {
  436. const projects = [ProjectFixture()];
  437. const data = initializeTrendsData(projects, {project: ['-1']});
  438. render(
  439. <TrendsIndex location={data.router.location} organization={data.organization} />,
  440. {
  441. router: data.router,
  442. organization: data.organization,
  443. }
  444. );
  445. for (const trendFunction of TRENDS_FUNCTIONS) {
  446. // Open dropdown
  447. const dropdown = await getTrendDropdown();
  448. await clickEl(dropdown);
  449. // Select function
  450. const option = screen.getByRole('option', {name: trendFunction.label});
  451. await clickEl(option);
  452. expect(browserHistory.push).toHaveBeenCalledWith({
  453. query: expect.objectContaining({
  454. regressionCursor: undefined,
  455. improvedCursor: undefined,
  456. trendFunction: trendFunction.field,
  457. }),
  458. });
  459. }
  460. });
  461. it('sets LCP as a default trend parameter for frontend project if query does not specify trend parameter', async function () {
  462. const projects = [ProjectFixture({id: '1', platform: 'javascript'})];
  463. const data = initializeTrendsData(projects, {project: [1]});
  464. render(
  465. <TrendsIndex location={data.router.location} organization={data.organization} />,
  466. {
  467. router: data.router,
  468. organization: data.organization,
  469. }
  470. );
  471. const trendDropdownButton = await getTrendDropdown();
  472. expect(trendDropdownButton).toHaveTextContent('Percentilep95');
  473. });
  474. it('sets duration as a default trend parameter for backend project if query does not specify trend parameter', async function () {
  475. const projects = [ProjectFixture({id: '1', platform: 'python'})];
  476. const data = initializeTrendsData(projects, {project: [1]});
  477. render(
  478. <TrendsIndex location={data.router.location} organization={data.organization} />,
  479. {
  480. router: data.router,
  481. organization: data.organization,
  482. }
  483. );
  484. const parameterDropdownButton = await getParameterDropdown();
  485. expect(parameterDropdownButton).toHaveTextContent('ParameterDuration');
  486. });
  487. it('sets trend parameter from query and ignores default trend parameter', async function () {
  488. const projects = [ProjectFixture({id: '1', platform: 'javascript'})];
  489. const data = initializeTrendsData(projects, {project: [1], trendParameter: 'FCP'});
  490. render(
  491. <TrendsIndex location={data.router.location} organization={data.organization} />,
  492. {
  493. router: data.router,
  494. organization: data.organization,
  495. }
  496. );
  497. const parameterDropdownButton = await getParameterDropdown();
  498. expect(parameterDropdownButton).toHaveTextContent('ParameterFCP');
  499. });
  500. it('choosing a parameter changes location', async function () {
  501. const projects = [ProjectFixture()];
  502. const data = initializeTrendsData(projects, {project: ['-1']});
  503. render(
  504. <TrendsIndex location={data.router.location} organization={data.organization} />,
  505. {
  506. router: data.router,
  507. organization: data.organization,
  508. }
  509. );
  510. for (const parameter of TRENDS_PARAMETERS) {
  511. // Open dropdown
  512. const dropdown = await getParameterDropdown();
  513. await clickEl(dropdown);
  514. // Select parameter
  515. const option = screen.getByRole('option', {name: parameter.label});
  516. await clickEl(option);
  517. expect(browserHistory.push).toHaveBeenCalledWith({
  518. query: expect.objectContaining({
  519. trendParameter: parameter.label,
  520. }),
  521. });
  522. }
  523. });
  524. it('choosing a web vitals parameter adds it as an additional condition to the query', async function () {
  525. const projects = [ProjectFixture()];
  526. const data = initializeTrendsData(projects, {project: ['-1']});
  527. const {rerender} = render(
  528. <TrendsIndex location={data.router.location} organization={data.organization} />,
  529. {
  530. router: data.router,
  531. organization: data.organization,
  532. }
  533. );
  534. for (const parameter of TRENDS_PARAMETERS) {
  535. if (Object.values(WebVital).includes(parameter.column as string as WebVital)) {
  536. trendsStatsMock.mockReset();
  537. const newLocation = {
  538. query: {...trendsViewQuery, trendParameter: parameter.label},
  539. };
  540. rerender(
  541. <TrendsIndex
  542. location={newLocation as unknown as Location}
  543. organization={data.organization}
  544. />
  545. );
  546. await waitForMockCall(trendsStatsMock);
  547. expect(trendsStatsMock).toHaveBeenCalledTimes(2);
  548. // Improved transactions call
  549. expect(trendsStatsMock).toHaveBeenNthCalledWith(
  550. 1,
  551. expect.anything(),
  552. expect.objectContaining({
  553. query: expect.objectContaining({
  554. query: expect.stringContaining(`has:${parameter.column}`),
  555. }),
  556. })
  557. );
  558. // Regression transactions call
  559. expect(trendsStatsMock).toHaveBeenNthCalledWith(
  560. 2,
  561. expect.anything(),
  562. expect.objectContaining({
  563. query: expect.objectContaining({
  564. query: expect.stringContaining(`has:${parameter.column}`),
  565. }),
  566. })
  567. );
  568. }
  569. }
  570. });
  571. it('trend functions in location make api calls', async function () {
  572. const projects = [ProjectFixture(), ProjectFixture()];
  573. const data = initializeTrendsData(projects, {project: ['-1']});
  574. const {rerender} = render(
  575. <TrendsIndex location={data.router.location} organization={data.organization} />,
  576. {
  577. router: data.router,
  578. organization: data.organization,
  579. }
  580. );
  581. for (const trendFunction of TRENDS_FUNCTIONS) {
  582. trendsStatsMock.mockReset();
  583. const newLocation = {
  584. query: {...trendsViewQuery, trendFunction: trendFunction.field},
  585. };
  586. rerender(
  587. <TrendsIndex
  588. location={newLocation as unknown as Location}
  589. organization={data.organization}
  590. />
  591. );
  592. await waitForMockCall(trendsStatsMock);
  593. expect(trendsStatsMock).toHaveBeenCalledTimes(2);
  594. const sort = 'trend_percentage()';
  595. const defaultTrendsFields = ['project'];
  596. const transactionFields = ['transaction', ...defaultTrendsFields];
  597. const projectFields = [...defaultTrendsFields];
  598. expect(transactionFields).toHaveLength(2);
  599. expect(projectFields).toHaveLength(transactionFields.length - 1);
  600. // Improved transactions call
  601. expect(trendsStatsMock).toHaveBeenNthCalledWith(
  602. 1,
  603. expect.anything(),
  604. expect.objectContaining({
  605. query: expect.objectContaining({
  606. trendFunction: `${trendFunction.field}(transaction.duration)`,
  607. sort,
  608. query: expect.stringContaining('trend_percentage():>0%'),
  609. interval: '1h',
  610. field: transactionFields,
  611. statsPeriod: '14d',
  612. }),
  613. })
  614. );
  615. // Regression transactions call
  616. expect(trendsStatsMock).toHaveBeenNthCalledWith(
  617. 2,
  618. expect.anything(),
  619. expect.objectContaining({
  620. query: expect.objectContaining({
  621. trendFunction: `${trendFunction.field}(transaction.duration)`,
  622. sort: '-' + sort,
  623. query: expect.stringContaining('trend_percentage():>0%'),
  624. interval: '1h',
  625. field: transactionFields,
  626. statsPeriod: '14d',
  627. }),
  628. })
  629. );
  630. }
  631. });
  632. it('Visiting trends with trends feature will update filters if none are set', async function () {
  633. const data = initializeTrendsData(undefined, {}, false);
  634. render(
  635. <TrendsIndex location={data.router.location} organization={data.organization} />,
  636. {
  637. router: data.router,
  638. organization: data.organization,
  639. }
  640. );
  641. await waitFor(() =>
  642. expect(browserHistory.push).toHaveBeenNthCalledWith(
  643. 1,
  644. expect.objectContaining({
  645. query: {
  646. query: `tpm():>0.01 transaction.duration:>0 transaction.duration:<${DEFAULT_MAX_DURATION}`,
  647. },
  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. router: data.router,
  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. });