index.spec.tsx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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-timezone', () => {
  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. pathname: '/trends/',
  124. query: newQuery,
  125. },
  126. },
  127. projects: _projects,
  128. });
  129. act(() => ProjectsStore.loadInitialData(initialData.projects));
  130. return initialData;
  131. }
  132. describe('Performance > Trends', function () {
  133. let trendsStatsMock;
  134. beforeEach(function () {
  135. browserHistory.push = jest.fn();
  136. MockApiClient.addMockResponse({
  137. url: '/organizations/org-slug/projects/',
  138. body: [],
  139. });
  140. MockApiClient.addMockResponse({
  141. url: '/organizations/org-slug/tags/',
  142. body: [],
  143. });
  144. MockApiClient.addMockResponse({
  145. url: '/organizations/org-slug/users/',
  146. body: [],
  147. });
  148. MockApiClient.addMockResponse({
  149. url: '/organizations/org-slug/recent-searches/',
  150. body: [],
  151. });
  152. MockApiClient.addMockResponse({
  153. url: '/organizations/org-slug/recent-searches/',
  154. method: 'POST',
  155. body: [],
  156. });
  157. MockApiClient.addMockResponse({
  158. url: '/organizations/org-slug/sdk-updates/',
  159. body: [],
  160. });
  161. MockApiClient.addMockResponse({
  162. url: '/organizations/org-slug/prompts-activity/',
  163. body: {},
  164. });
  165. MockApiClient.addMockResponse({
  166. url: '/organizations/org-slug/releases/stats/',
  167. body: [],
  168. });
  169. MockApiClient.addMockResponse({
  170. url: '/organizations/org-slug/tags/transaction.duration/values/',
  171. body: [],
  172. });
  173. trendsStatsMock = MockApiClient.addMockResponse({
  174. url: '/organizations/org-slug/events-trends-stats/',
  175. body: {
  176. stats: {
  177. 'internal,/organizations/:orgId/performance/': {
  178. data: [[123, []]],
  179. },
  180. order: 0,
  181. },
  182. events: {
  183. meta: {
  184. count_range_1: 'integer',
  185. count_range_2: 'integer',
  186. count_percentage: 'percentage',
  187. breakpoint: 'number',
  188. trend_percentage: 'percentage',
  189. trend_difference: 'number',
  190. aggregate_range_1: 'duration',
  191. aggregate_range_2: 'duration',
  192. transaction: 'string',
  193. },
  194. data: [
  195. {
  196. count: 8,
  197. project: 'internal',
  198. count_range_1: 2,
  199. count_range_2: 6,
  200. count_percentage: 3,
  201. breakpoint: 1686967200,
  202. trend_percentage: 1.9235225955967554,
  203. trend_difference: 797,
  204. aggregate_range_1: 863,
  205. aggregate_range_2: 1660,
  206. transaction: '/organizations/:orgId/performance/',
  207. },
  208. {
  209. count: 60,
  210. project: 'internal',
  211. count_range_1: 20,
  212. count_range_2: 40,
  213. count_percentage: 2,
  214. breakpoint: 1686967200,
  215. trend_percentage: 1.204968944099379,
  216. trend_difference: 66,
  217. aggregate_range_1: 322,
  218. aggregate_range_2: 388,
  219. transaction: '/api/0/internal/health/',
  220. },
  221. ],
  222. },
  223. },
  224. });
  225. MockApiClient.addMockResponse({
  226. url: '/organizations/org-slug/events/',
  227. body: {
  228. data: [
  229. {
  230. 'p95()': 1010.9232499999998,
  231. 'p50()': 47.34580982348902,
  232. 'tps()': 3.7226926286168966,
  233. 'count()': 34872349,
  234. 'failure_rate()': 0.43428379,
  235. 'examples()': ['djk3w308er', '3298a9ui3h'],
  236. },
  237. ],
  238. meta: {
  239. fields: {
  240. 'p95()': 'duration',
  241. '950()': 'duration',
  242. 'tps()': 'number',
  243. 'count()': 'number',
  244. 'failure_rate()': 'number',
  245. 'examples()': 'Array',
  246. },
  247. units: {
  248. 'p95()': 'millisecond',
  249. 'p50()': 'millisecond',
  250. 'tps()': null,
  251. 'count()': null,
  252. 'failure_rate()': null,
  253. 'examples()': 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. router: data.router,
  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. router: data.router,
  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 = [ProjectFixture({id: '1', slug: 'internal'}), ProjectFixture()];
  297. const data = initializeTrendsData(projects, {project: ['1']});
  298. render(
  299. <TrendsIndex location={data.router.location} organization={data.organization} />,
  300. {
  301. router: data.router,
  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=p95&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 = [ProjectFixture({id: '1', slug: 'internal'}), ProjectFixture()];
  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. router: data.router,
  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')).toHaveAttribute(
  331. 'href',
  332. '/trends/?project=1&query=tpm%28%29%3A%3E0.01%20transaction.duration%3A%3E0%20transaction.duration%3A%3C15min'
  333. );
  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. router: data.router,
  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. pathname: '/trends/',
  362. query: expect.objectContaining({
  363. project: expect.anything(),
  364. query: `tpm():>0.01 transaction.duration:>0 transaction.duration:<${DEFAULT_MAX_DURATION} !transaction:/organizations/:orgId/performance/`,
  365. }),
  366. });
  367. });
  368. it('Changing search causes cursors to be reset', async function () {
  369. const projects = [ProjectFixture({id: '1', slug: 'internal'}), ProjectFixture()];
  370. const data = initializeTrendsData(projects, {project: ['1']});
  371. render(
  372. <TrendsIndex location={data.router.location} organization={data.organization} />,
  373. {
  374. router: data.router,
  375. organization: data.organization,
  376. }
  377. );
  378. const input = await screen.findByTestId('smart-search-input');
  379. enterSearch(input, 'transaction.duration:>9000');
  380. await waitFor(() =>
  381. expect(browserHistory.push).toHaveBeenCalledWith({
  382. pathname: '/trends/',
  383. query: expect.objectContaining({
  384. project: ['1'],
  385. query: 'transaction.duration:>9000',
  386. improvedCursor: undefined,
  387. regressionCursor: undefined,
  388. }),
  389. })
  390. );
  391. });
  392. it('exclude greater than list menu action modifies query', async function () {
  393. const projects = [ProjectFixture({id: '1', slug: 'internal'}), ProjectFixture()];
  394. const data = initializeTrendsData(projects, {project: ['1']});
  395. render(
  396. <TrendsIndex location={data.router.location} organization={data.organization} />,
  397. {
  398. router: data.router,
  399. organization: data.organization,
  400. }
  401. );
  402. const transactions = await screen.findAllByTestId('trends-list-item-improved');
  403. expect(transactions).toHaveLength(2);
  404. const firstTransaction = transactions[0];
  405. const menuActions = within(firstTransaction).getAllByTestId('menu-action');
  406. expect(menuActions).toHaveLength(3);
  407. const menuAction = menuActions[0];
  408. await clickEl(menuAction);
  409. expect(browserHistory.push).toHaveBeenCalledWith({
  410. pathname: '/trends/',
  411. query: expect.objectContaining({
  412. project: expect.anything(),
  413. query: 'tpm():>0.01 transaction.duration:>0 transaction.duration:<=863',
  414. }),
  415. });
  416. });
  417. it('exclude less than list menu action modifies query', async function () {
  418. const projects = [ProjectFixture({id: '1', slug: 'internal'}), ProjectFixture()];
  419. const data = initializeTrendsData(projects, {project: ['1']});
  420. render(
  421. <TrendsIndex location={data.router.location} organization={data.organization} />,
  422. {
  423. router: data.router,
  424. organization: data.organization,
  425. }
  426. );
  427. const transactions = await screen.findAllByTestId('trends-list-item-improved');
  428. expect(transactions).toHaveLength(2);
  429. const firstTransaction = transactions[0];
  430. const menuActions = within(firstTransaction).getAllByTestId('menu-action');
  431. expect(menuActions).toHaveLength(3);
  432. const menuAction = menuActions[1];
  433. await clickEl(menuAction);
  434. expect(browserHistory.push).toHaveBeenCalledWith({
  435. pathname: '/trends/',
  436. query: expect.objectContaining({
  437. project: expect.anything(),
  438. query: 'tpm():>0.01 transaction.duration:<15min transaction.duration:>=863',
  439. }),
  440. });
  441. });
  442. it('choosing a trend function changes location', async function () {
  443. const projects = [ProjectFixture()];
  444. const data = initializeTrendsData(projects, {project: ['-1']});
  445. render(
  446. <TrendsIndex location={data.router.location} organization={data.organization} />,
  447. {
  448. router: data.router,
  449. organization: data.organization,
  450. }
  451. );
  452. for (const trendFunction of TRENDS_FUNCTIONS) {
  453. // Open dropdown
  454. const dropdown = await getTrendDropdown();
  455. await clickEl(dropdown);
  456. // Select function
  457. const option = screen.getByRole('option', {name: trendFunction.label});
  458. await clickEl(option);
  459. expect(browserHistory.push).toHaveBeenCalledWith({
  460. pathname: '/trends/',
  461. query: expect.objectContaining({
  462. regressionCursor: undefined,
  463. improvedCursor: undefined,
  464. trendFunction: trendFunction.field,
  465. }),
  466. });
  467. }
  468. });
  469. it('sets LCP as a default trend parameter for frontend project if query does not specify trend parameter', async function () {
  470. const projects = [ProjectFixture({id: '1', platform: 'javascript'})];
  471. const data = initializeTrendsData(projects, {project: [1]});
  472. render(
  473. <TrendsIndex location={data.router.location} organization={data.organization} />,
  474. {
  475. router: data.router,
  476. organization: data.organization,
  477. }
  478. );
  479. const trendDropdownButton = await getTrendDropdown();
  480. expect(trendDropdownButton).toHaveTextContent('Percentilep95');
  481. });
  482. it('sets duration as a default trend parameter for backend project if query does not specify trend parameter', async function () {
  483. const projects = [ProjectFixture({id: '1', platform: 'python'})];
  484. const data = initializeTrendsData(projects, {project: [1]});
  485. render(
  486. <TrendsIndex location={data.router.location} organization={data.organization} />,
  487. {
  488. router: data.router,
  489. organization: data.organization,
  490. }
  491. );
  492. const parameterDropdownButton = await getParameterDropdown();
  493. expect(parameterDropdownButton).toHaveTextContent('ParameterDuration');
  494. });
  495. it('sets trend parameter from query and ignores default trend parameter', async function () {
  496. const projects = [ProjectFixture({id: '1', platform: 'javascript'})];
  497. const data = initializeTrendsData(projects, {project: [1], trendParameter: 'FCP'});
  498. render(
  499. <TrendsIndex location={data.router.location} organization={data.organization} />,
  500. {
  501. router: data.router,
  502. organization: data.organization,
  503. }
  504. );
  505. const parameterDropdownButton = await getParameterDropdown();
  506. expect(parameterDropdownButton).toHaveTextContent('ParameterFCP');
  507. });
  508. it('choosing a parameter changes location', async function () {
  509. const projects = [ProjectFixture()];
  510. const data = initializeTrendsData(projects, {project: ['-1']});
  511. render(
  512. <TrendsIndex location={data.router.location} organization={data.organization} />,
  513. {
  514. router: data.router,
  515. organization: data.organization,
  516. }
  517. );
  518. for (const parameter of TRENDS_PARAMETERS) {
  519. // Open dropdown
  520. const dropdown = await getParameterDropdown();
  521. await clickEl(dropdown);
  522. // Select parameter
  523. const option = screen.getByRole('option', {name: parameter.label});
  524. await clickEl(option);
  525. expect(browserHistory.push).toHaveBeenCalledWith({
  526. pathname: '/trends/',
  527. query: expect.objectContaining({
  528. trendParameter: parameter.label,
  529. }),
  530. });
  531. }
  532. });
  533. it('choosing a web vitals parameter adds it as an additional condition to the query', async function () {
  534. const projects = [ProjectFixture()];
  535. const data = initializeTrendsData(projects, {project: ['-1']});
  536. const {rerender} = render(
  537. <TrendsIndex location={data.router.location} organization={data.organization} />,
  538. {
  539. router: data.router,
  540. organization: data.organization,
  541. }
  542. );
  543. for (const parameter of TRENDS_PARAMETERS) {
  544. if (Object.values(WebVital).includes(parameter.column as string as WebVital)) {
  545. trendsStatsMock.mockReset();
  546. const newLocation = {
  547. query: {...trendsViewQuery, trendParameter: parameter.label},
  548. };
  549. rerender(
  550. <TrendsIndex
  551. location={newLocation as unknown as Location}
  552. organization={data.organization}
  553. />
  554. );
  555. await waitForMockCall(trendsStatsMock);
  556. expect(trendsStatsMock).toHaveBeenCalledTimes(2);
  557. // Improved transactions call
  558. expect(trendsStatsMock).toHaveBeenNthCalledWith(
  559. 1,
  560. expect.anything(),
  561. expect.objectContaining({
  562. query: expect.objectContaining({
  563. query: expect.stringContaining(`has:${parameter.column}`),
  564. }),
  565. })
  566. );
  567. // Regression transactions call
  568. expect(trendsStatsMock).toHaveBeenNthCalledWith(
  569. 2,
  570. expect.anything(),
  571. expect.objectContaining({
  572. query: expect.objectContaining({
  573. query: expect.stringContaining(`has:${parameter.column}`),
  574. }),
  575. })
  576. );
  577. }
  578. }
  579. });
  580. it('trend functions in location make api calls', async function () {
  581. const projects = [ProjectFixture(), ProjectFixture()];
  582. const data = initializeTrendsData(projects, {project: ['-1']});
  583. const {rerender} = render(
  584. <TrendsIndex location={data.router.location} organization={data.organization} />,
  585. {
  586. router: data.router,
  587. organization: data.organization,
  588. }
  589. );
  590. for (const trendFunction of TRENDS_FUNCTIONS) {
  591. trendsStatsMock.mockReset();
  592. const newLocation = {
  593. query: {...trendsViewQuery, trendFunction: trendFunction.field},
  594. };
  595. rerender(
  596. <TrendsIndex
  597. location={newLocation as unknown as Location}
  598. organization={data.organization}
  599. />
  600. );
  601. await waitForMockCall(trendsStatsMock);
  602. expect(trendsStatsMock).toHaveBeenCalledTimes(2);
  603. const sort = 'trend_percentage()';
  604. const defaultTrendsFields = ['project'];
  605. const transactionFields = ['transaction', ...defaultTrendsFields];
  606. const projectFields = [...defaultTrendsFields];
  607. expect(transactionFields).toHaveLength(2);
  608. expect(projectFields).toHaveLength(transactionFields.length - 1);
  609. // Improved transactions call
  610. expect(trendsStatsMock).toHaveBeenNthCalledWith(
  611. 1,
  612. expect.anything(),
  613. expect.objectContaining({
  614. query: expect.objectContaining({
  615. trendFunction: `${trendFunction.field}(transaction.duration)`,
  616. sort,
  617. query: expect.stringContaining('trend_percentage():>0%'),
  618. interval: '1h',
  619. field: transactionFields,
  620. statsPeriod: '14d',
  621. }),
  622. })
  623. );
  624. // Regression transactions call
  625. expect(trendsStatsMock).toHaveBeenNthCalledWith(
  626. 2,
  627. expect.anything(),
  628. expect.objectContaining({
  629. query: expect.objectContaining({
  630. trendFunction: `${trendFunction.field}(transaction.duration)`,
  631. sort: '-' + sort,
  632. query: expect.stringContaining('trend_percentage():>0%'),
  633. interval: '1h',
  634. field: transactionFields,
  635. statsPeriod: '14d',
  636. }),
  637. })
  638. );
  639. }
  640. });
  641. it('Visiting trends with trends feature will update filters if none are set', async function () {
  642. const data = initializeTrendsData(undefined, {}, false);
  643. render(
  644. <TrendsIndex location={data.router.location} organization={data.organization} />,
  645. {
  646. router: data.router,
  647. organization: data.organization,
  648. }
  649. );
  650. await waitFor(() =>
  651. expect(browserHistory.push).toHaveBeenNthCalledWith(
  652. 1,
  653. expect.objectContaining({
  654. query: {
  655. query: `tpm():>0.01 transaction.duration:>0 transaction.duration:<${DEFAULT_MAX_DURATION}`,
  656. },
  657. })
  658. )
  659. );
  660. });
  661. it('Navigating away from trends will remove extra tags from query', async function () {
  662. const data = initializeTrendsData(
  663. undefined,
  664. {
  665. query: `device.family:Mac tpm():>0.01 transaction.duration:>0 transaction.duration:<${DEFAULT_MAX_DURATION}`,
  666. },
  667. false
  668. );
  669. render(
  670. <TrendsIndex location={data.router.location} organization={data.organization} />,
  671. {
  672. router: data.router,
  673. organization: data.organization,
  674. }
  675. );
  676. (browserHistory.push as any).mockReset();
  677. const byTransactionLink = await screen.findByTestId('breadcrumb-link');
  678. expect(byTransactionLink.closest('a')).toHaveAttribute(
  679. 'href',
  680. '/organizations/org-slug/performance/?query=device.family%3AMac'
  681. );
  682. });
  683. });