tableView.spec.tsx 15 KB

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