tableView.spec.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. import {LocationFixture} from 'sentry-fixture/locationFixture';
  2. import {OrganizationFixture} from 'sentry-fixture/organization';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {act, render, screen, userEvent, within} from 'sentry-test/reactTestingLibrary';
  5. import ProjectsStore from 'sentry/stores/projectsStore';
  6. import TagStore from 'sentry/stores/tagStore';
  7. import {browserHistory} from 'sentry/utils/browserHistory';
  8. import EventView from 'sentry/utils/discover/eventView';
  9. import {SavedQueryDatasets} from 'sentry/utils/discover/types';
  10. import TableView from 'sentry/views/discover/table/tableView';
  11. describe('TableView > CellActions', function () {
  12. let initialData, rows, onChangeShowTags;
  13. const location = LocationFixture({
  14. pathname: '/organizations/org-slug/discover/results/',
  15. query: {
  16. id: '42',
  17. name: 'best query',
  18. field: [
  19. 'title',
  20. 'transaction',
  21. 'count()',
  22. 'timestamp',
  23. 'release',
  24. 'equation|count() + 100',
  25. ],
  26. sort: ['title'],
  27. query: '',
  28. project: ['123'],
  29. statsPeriod: '14d',
  30. environment: ['staging'],
  31. yAxis: 'p95',
  32. },
  33. });
  34. const eventView = EventView.fromLocation(location);
  35. function renderComponent(context, tableData, view) {
  36. return render(
  37. <TableView
  38. organization={context.organization}
  39. location={location}
  40. eventView={view}
  41. isLoading={false}
  42. tableData={tableData}
  43. onChangeShowTags={onChangeShowTags}
  44. error={null}
  45. isFirstPage
  46. measurementKeys={null}
  47. showTags={false}
  48. title=""
  49. queryDataset={SavedQueryDatasets.TRANSACTIONS}
  50. />,
  51. {router: context.router}
  52. );
  53. }
  54. async function openContextMenu(cellIndex) {
  55. const firstRow = screen.getAllByRole('row')[1];
  56. const emptyValueCell = within(firstRow).getAllByRole('cell')[cellIndex];
  57. await userEvent.click(within(emptyValueCell).getByRole('button', {name: 'Actions'}));
  58. }
  59. beforeEach(function () {
  60. jest.mocked(browserHistory.push).mockReset();
  61. jest.mocked(browserHistory.replace).mockReset();
  62. const organization = OrganizationFixture({
  63. features: ['discover-basic'],
  64. });
  65. initialData = initializeOrg({
  66. organization,
  67. router: {location},
  68. });
  69. act(() => {
  70. ProjectsStore.loadInitialData(initialData.projects);
  71. TagStore.reset();
  72. TagStore.loadTagsSuccess([
  73. {name: 'size', key: 'size'},
  74. {name: 'shape', key: 'shape'},
  75. {name: 'direction', key: 'direction'},
  76. ]);
  77. });
  78. onChangeShowTags = jest.fn();
  79. rows = {
  80. meta: {
  81. title: 'string',
  82. transaction: 'string',
  83. 'count()': 'integer',
  84. timestamp: 'date',
  85. release: 'string',
  86. 'equation[0]': 'integer',
  87. },
  88. data: [
  89. {
  90. title: 'some title',
  91. transaction: '/organizations/',
  92. 'count()': 9,
  93. timestamp: '2019-05-23T22:12:48+00:00',
  94. release: 'v1.0.2',
  95. 'equation[0]': 109,
  96. },
  97. ],
  98. };
  99. MockApiClient.addMockResponse({
  100. url: '/organizations/org-slug/dynamic-sampling/custom-rules/',
  101. method: 'GET',
  102. statusCode: 204,
  103. body: '',
  104. });
  105. });
  106. afterEach(() => {
  107. ProjectsStore.reset();
  108. });
  109. it('updates sort order on equation fields', function () {
  110. const view = eventView.clone();
  111. renderComponent(initialData, rows, view);
  112. const equationCell = screen.getByRole('columnheader', {name: 'count() + 100'});
  113. const sortLink = within(equationCell).getByRole('link');
  114. expect(sortLink).toHaveAttribute(
  115. 'href',
  116. '/organizations/org-slug/discover/results/?environment=staging&field=title&field=transaction&field=count%28%29&field=timestamp&field=release&field=equation%7Ccount%28%29%20%2B%20100&id=42&name=best%20query&project=123&query=&sort=-equation%7Ccount%28%29%20%2B%20100&statsPeriod=14d&yAxis=p95'
  117. );
  118. });
  119. it('updates sort order on non-equation fields', function () {
  120. const view = eventView.clone();
  121. renderComponent(initialData, rows, view);
  122. const transactionCell = screen.getByRole('columnheader', {name: 'transaction'});
  123. const sortLink = within(transactionCell).getByRole('link');
  124. expect(sortLink).toHaveAttribute(
  125. 'href',
  126. '/organizations/org-slug/discover/results/?environment=staging&field=title&field=transaction&field=count%28%29&field=timestamp&field=release&field=equation%7Ccount%28%29%20%2B%20100&id=42&name=best%20query&project=123&query=&sort=-transaction&statsPeriod=14d&yAxis=p95'
  127. );
  128. });
  129. it('handles add cell action on null value', async function () {
  130. rows.data[0].title = null;
  131. renderComponent(initialData, rows, eventView);
  132. await openContextMenu(1);
  133. await userEvent.click(screen.getByRole('menuitemradio', {name: 'Add to filter'}));
  134. expect(browserHistory.push).toHaveBeenCalledWith({
  135. pathname: location.pathname,
  136. query: expect.objectContaining({
  137. query: '!has:title',
  138. }),
  139. });
  140. });
  141. it('handles add cell action on null value replace has condition', async function () {
  142. rows.data[0].title = null;
  143. const view = eventView.clone();
  144. view.query = 'tag:value has:title';
  145. renderComponent(initialData, rows, view);
  146. await openContextMenu(1);
  147. await userEvent.click(screen.getByRole('menuitemradio', {name: 'Add to filter'}));
  148. expect(browserHistory.push).toHaveBeenCalledWith({
  149. pathname: location.pathname,
  150. query: expect.objectContaining({
  151. query: 'tag:value !has:title',
  152. }),
  153. });
  154. });
  155. it('handles add cell action on string value replace negation', async function () {
  156. const view = eventView.clone();
  157. view.query = 'tag:value !title:nope';
  158. renderComponent(initialData, rows, view);
  159. await openContextMenu(1);
  160. await userEvent.click(screen.getByRole('menuitemradio', {name: 'Add to filter'}));
  161. expect(browserHistory.push).toHaveBeenCalledWith({
  162. pathname: location.pathname,
  163. query: expect.objectContaining({
  164. query: 'tag:value title:"some title"',
  165. }),
  166. });
  167. });
  168. it('handles add cell action with multiple y axis', async function () {
  169. location.query.yAxis = ['count()', 'failure_count()'];
  170. renderComponent(initialData, rows, eventView);
  171. await openContextMenu(1);
  172. await userEvent.click(screen.getByRole('menuitemradio', {name: 'Add to filter'}));
  173. expect(browserHistory.push).toHaveBeenCalledWith({
  174. pathname: location.pathname,
  175. query: expect.objectContaining({
  176. query: 'title:"some title"',
  177. yAxis: ['count()', 'failure_count()'],
  178. }),
  179. });
  180. });
  181. it('handles exclude cell action on string value', async function () {
  182. renderComponent(initialData, rows, eventView);
  183. await openContextMenu(1);
  184. await userEvent.click(
  185. screen.getByRole('menuitemradio', {name: 'Exclude from filter'})
  186. );
  187. expect(browserHistory.push).toHaveBeenCalledWith({
  188. pathname: location.pathname,
  189. query: expect.objectContaining({
  190. query: '!title:"some title"',
  191. }),
  192. });
  193. });
  194. it('handles exclude cell action on string value replace inclusion', async function () {
  195. const view = eventView.clone();
  196. view.query = 'tag:value title:nope';
  197. renderComponent(initialData, rows, view);
  198. await openContextMenu(1);
  199. await userEvent.click(
  200. screen.getByRole('menuitemradio', {name: 'Exclude from filter'})
  201. );
  202. expect(browserHistory.push).toHaveBeenCalledWith({
  203. pathname: location.pathname,
  204. query: expect.objectContaining({
  205. query: 'tag:value !title:"some title"',
  206. }),
  207. });
  208. });
  209. it('handles exclude cell action on null value', async function () {
  210. rows.data[0].title = null;
  211. renderComponent(initialData, rows, eventView);
  212. await openContextMenu(1);
  213. await userEvent.click(
  214. screen.getByRole('menuitemradio', {name: 'Exclude from filter'})
  215. );
  216. expect(browserHistory.push).toHaveBeenCalledWith({
  217. pathname: location.pathname,
  218. query: expect.objectContaining({
  219. query: 'has:title',
  220. }),
  221. });
  222. });
  223. it('handles exclude cell action on null value replace condition', async function () {
  224. const view = eventView.clone();
  225. view.query = 'tag:value !has:title';
  226. rows.data[0].title = null;
  227. renderComponent(initialData, rows, view);
  228. await openContextMenu(1);
  229. await userEvent.click(
  230. screen.getByRole('menuitemradio', {name: 'Exclude from filter'})
  231. );
  232. expect(browserHistory.push).toHaveBeenCalledWith({
  233. pathname: location.pathname,
  234. query: expect.objectContaining({
  235. query: 'tag:value has:title',
  236. }),
  237. });
  238. });
  239. it('handles greater than cell action on number value', async function () {
  240. renderComponent(initialData, rows, eventView);
  241. await openContextMenu(3);
  242. await userEvent.click(
  243. screen.getByRole('menuitemradio', {name: 'Show values greater than'})
  244. );
  245. expect(browserHistory.push).toHaveBeenCalledWith({
  246. pathname: location.pathname,
  247. query: expect.objectContaining({
  248. query: 'count():>9',
  249. }),
  250. });
  251. });
  252. it('handles less than cell action on number value', async function () {
  253. renderComponent(initialData, rows, eventView);
  254. await openContextMenu(3);
  255. await userEvent.click(
  256. screen.getByRole('menuitemradio', {name: 'Show values less than'})
  257. );
  258. expect(browserHistory.push).toHaveBeenCalledWith({
  259. pathname: location.pathname,
  260. query: expect.objectContaining({
  261. query: 'count():<9',
  262. }),
  263. });
  264. });
  265. it('renders transaction summary link', function () {
  266. rows.data[0].project = 'project-slug';
  267. renderComponent(initialData, rows, eventView);
  268. const firstRow = screen.getAllByRole('row')[1];
  269. const link = within(firstRow).getByTestId('tableView-transaction-link');
  270. expect(link).toHaveAttribute(
  271. 'href',
  272. expect.stringMatching(
  273. RegExp(
  274. '/organizations/org-slug/performance/summary/?.*project=2&referrer=performance-transaction-summary.*transaction=%2.*'
  275. )
  276. )
  277. );
  278. });
  279. it('renders trace view link', function () {
  280. const org = OrganizationFixture({
  281. features: [
  282. 'discover-basic',
  283. 'performance-discover-dataset-selector',
  284. 'trace-view-v1',
  285. ],
  286. });
  287. rows = {
  288. meta: {
  289. trace: 'string',
  290. id: 'string',
  291. transaction: 'string',
  292. timestamp: 'date',
  293. project: 'string',
  294. 'event.type': 'string',
  295. },
  296. data: [
  297. {
  298. trace: '7fdf8efed85a4f9092507063ced1995b',
  299. id: '509663014077465b8981b65225bdec0f',
  300. transaction: '/organizations/',
  301. timestamp: '2019-05-23T22:12:48+00:00',
  302. project: 'project-slug',
  303. 'event.type': '',
  304. },
  305. ],
  306. };
  307. const loc = LocationFixture({
  308. pathname: '/organizations/org-slug/discover/results/',
  309. query: {
  310. id: '42',
  311. name: 'best query',
  312. field: ['id', 'transaction', 'timestamp'],
  313. queryDataset: 'transaction-like',
  314. sort: ['transaction'],
  315. query: '',
  316. project: ['123'],
  317. statsPeriod: '14d',
  318. environment: ['staging'],
  319. yAxis: 'p95',
  320. },
  321. });
  322. initialData = initializeOrg({
  323. organization: org,
  324. router: {location: loc},
  325. });
  326. renderComponent(initialData, rows, EventView.fromLocation(loc));
  327. const firstRow = screen.getAllByRole('row')[1];
  328. const link = within(firstRow).getByTestId('view-event');
  329. expect(link).toHaveAttribute(
  330. 'href',
  331. expect.stringMatching(
  332. RegExp(
  333. '/organizations/org-slug/performance/trace/7fdf8efed85a4f9092507063ced1995b/?.*'
  334. )
  335. )
  336. );
  337. });
  338. it('handles go to release', async function () {
  339. renderComponent(initialData, rows, eventView);
  340. await openContextMenu(5);
  341. await userEvent.click(screen.getByRole('menuitemradio', {name: 'Go to release'}));
  342. expect(browserHistory.push).toHaveBeenCalledWith({
  343. pathname: '/organizations/org-slug/releases/v1.0.2/',
  344. query: expect.objectContaining({
  345. environment: eventView.environment,
  346. }),
  347. });
  348. });
  349. it('has title on integer value greater than 999', function () {
  350. rows.data[0]['count()'] = 1000;
  351. renderComponent(initialData, rows, eventView);
  352. const firstRow = screen.getAllByRole('row')[1];
  353. const emptyValueCell = within(firstRow).getAllByRole('cell')[3];
  354. expect(within(emptyValueCell).getByText('1k')).toHaveAttribute('title', '1,000');
  355. });
  356. it('renders size columns correctly', function () {
  357. const orgWithFeature = OrganizationFixture();
  358. render(
  359. <TableView
  360. organization={orgWithFeature}
  361. location={location}
  362. eventView={EventView.fromLocation({
  363. ...location,
  364. query: {
  365. ...location.query,
  366. field: [
  367. 'title',
  368. 'p99(measurements.custom.kibibyte)',
  369. 'p99(measurements.custom.kilobyte)',
  370. ],
  371. },
  372. })}
  373. isLoading={false}
  374. tableData={{
  375. data: [
  376. {
  377. id: '1',
  378. title: '/random/transaction/name',
  379. 'p99(measurements.custom.kibibyte)': 222.3,
  380. 'p99(measurements.custom.kilobyte)': 444.3,
  381. },
  382. ],
  383. meta: {
  384. title: 'string',
  385. 'p99(measurements.custom.kibibyte)': 'size',
  386. 'p99(measurements.custom.kilobyte)': 'size',
  387. units: {
  388. 'p99(measurements.custom.kibibyte)': 'kibibyte',
  389. 'p99(measurements.custom.kilobyte)': 'kilobyte',
  390. },
  391. },
  392. }}
  393. onChangeShowTags={onChangeShowTags}
  394. error={null}
  395. isFirstPage
  396. measurementKeys={null}
  397. showTags={false}
  398. title=""
  399. />
  400. );
  401. expect(screen.getByText('222.3 KiB')).toBeInTheDocument();
  402. expect(screen.getByText('444.3 KB')).toBeInTheDocument();
  403. });
  404. it('shows events with value less than selected custom performance metric', async function () {
  405. const orgWithFeature = OrganizationFixture();
  406. render(
  407. <TableView
  408. organization={orgWithFeature}
  409. location={location}
  410. eventView={EventView.fromLocation({
  411. ...location,
  412. query: {
  413. ...location.query,
  414. field: ['title', 'p99(measurements.custom.kilobyte)'],
  415. },
  416. })}
  417. isLoading={false}
  418. tableData={{
  419. data: [
  420. {
  421. id: '1',
  422. title: '/random/transaction/name',
  423. 'p99(measurements.custom.kilobyte)': 444.3,
  424. },
  425. ],
  426. meta: {
  427. title: 'string',
  428. 'p99(measurements.custom.kilobyte)': 'size',
  429. units: {'p99(measurements.custom.kilobyte)': 'kilobyte'},
  430. },
  431. }}
  432. onChangeShowTags={onChangeShowTags}
  433. error={null}
  434. isFirstPage
  435. measurementKeys={null}
  436. showTags={false}
  437. title=""
  438. />
  439. );
  440. await userEvent.hover(screen.getByText('444.3 KB'));
  441. const buttons = screen.getAllByRole('button');
  442. await userEvent.click(buttons[buttons.length - 1]);
  443. await userEvent.click(screen.getByText('Show values less than'));
  444. expect(browserHistory.push).toHaveBeenCalledWith({
  445. pathname: location.pathname,
  446. query: expect.objectContaining({
  447. query: 'p99(measurements.custom.kilobyte):<444300',
  448. }),
  449. });
  450. });
  451. });