tableView.spec.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. },
  302. data: [
  303. {
  304. trace: '7fdf8efed85a4f9092507063ced1995b',
  305. id: '509663014077465b8981b65225bdec0f',
  306. transaction: '/organizations/',
  307. timestamp: '2019-05-23T22:12:48+00:00',
  308. project: 'project-slug',
  309. },
  310. ],
  311. };
  312. const loc = LocationFixture({
  313. pathname: '/organizations/org-slug/discover/results/',
  314. query: {
  315. id: '42',
  316. name: 'best query',
  317. field: ['id', 'transaction', 'timestamp'],
  318. queryDataset: 'transaction-like',
  319. sort: ['transaction'],
  320. query: '',
  321. project: ['123'],
  322. statsPeriod: '14d',
  323. environment: ['staging'],
  324. yAxis: 'p95',
  325. },
  326. });
  327. initialData = initializeOrg({
  328. organization: org,
  329. router: {location: loc},
  330. });
  331. renderComponent(initialData, rows, EventView.fromLocation(loc));
  332. const firstRow = screen.getAllByRole('row')[1];
  333. const link = within(firstRow).getByTestId('view-event');
  334. expect(link).toHaveAttribute(
  335. 'href',
  336. expect.stringMatching(
  337. RegExp(
  338. '/organizations/org-slug/performance/trace/7fdf8efed85a4f9092507063ced1995b/?.*'
  339. )
  340. )
  341. );
  342. });
  343. it('handles go to release', async function () {
  344. renderComponent(initialData, rows, eventView);
  345. await openContextMenu(5);
  346. await userEvent.click(screen.getByRole('menuitemradio', {name: 'Go to release'}));
  347. expect(browserHistory.push).toHaveBeenCalledWith({
  348. pathname: '/organizations/org-slug/releases/v1.0.2/',
  349. query: expect.objectContaining({
  350. environment: eventView.environment,
  351. }),
  352. });
  353. });
  354. it('has title on integer value greater than 999', function () {
  355. rows.data[0]['count()'] = 1000;
  356. renderComponent(initialData, rows, eventView);
  357. const firstRow = screen.getAllByRole('row')[1];
  358. const emptyValueCell = within(firstRow).getAllByRole('cell')[3];
  359. expect(within(emptyValueCell).getByText('1k')).toHaveAttribute('title', '1,000');
  360. });
  361. it('renders size columns correctly', function () {
  362. const orgWithFeature = OrganizationFixture();
  363. render(
  364. <TableView
  365. organization={orgWithFeature}
  366. location={location}
  367. eventView={EventView.fromLocation({
  368. ...location,
  369. query: {
  370. ...location.query,
  371. field: [
  372. 'title',
  373. 'p99(measurements.custom.kibibyte)',
  374. 'p99(measurements.custom.kilobyte)',
  375. ],
  376. },
  377. })}
  378. isLoading={false}
  379. tableData={{
  380. data: [
  381. {
  382. id: '1',
  383. title: '/random/transaction/name',
  384. 'p99(measurements.custom.kibibyte)': 222.3,
  385. 'p99(measurements.custom.kilobyte)': 444.3,
  386. },
  387. ],
  388. meta: {
  389. title: 'string',
  390. 'p99(measurements.custom.kibibyte)': 'size',
  391. 'p99(measurements.custom.kilobyte)': 'size',
  392. units: {
  393. 'p99(measurements.custom.kibibyte)': 'kibibyte',
  394. 'p99(measurements.custom.kilobyte)': 'kilobyte',
  395. },
  396. },
  397. }}
  398. onChangeShowTags={onChangeShowTags}
  399. error={null}
  400. isFirstPage
  401. measurementKeys={null}
  402. showTags={false}
  403. title=""
  404. />
  405. );
  406. expect(screen.getByText('222.3 KiB')).toBeInTheDocument();
  407. expect(screen.getByText('444.3 KB')).toBeInTheDocument();
  408. });
  409. it('shows events with value less than selected custom performance metric', async function () {
  410. const orgWithFeature = OrganizationFixture();
  411. render(
  412. <TableView
  413. organization={orgWithFeature}
  414. location={location}
  415. eventView={EventView.fromLocation({
  416. ...location,
  417. query: {
  418. ...location.query,
  419. field: ['title', 'p99(measurements.custom.kilobyte)'],
  420. },
  421. })}
  422. isLoading={false}
  423. tableData={{
  424. data: [
  425. {
  426. id: '1',
  427. title: '/random/transaction/name',
  428. 'p99(measurements.custom.kilobyte)': 444.3,
  429. },
  430. ],
  431. meta: {
  432. title: 'string',
  433. 'p99(measurements.custom.kilobyte)': 'size',
  434. units: {'p99(measurements.custom.kilobyte)': 'kilobyte'},
  435. },
  436. }}
  437. onChangeShowTags={onChangeShowTags}
  438. error={null}
  439. isFirstPage
  440. measurementKeys={null}
  441. showTags={false}
  442. title=""
  443. />
  444. );
  445. await userEvent.hover(screen.getByText('444.3 KB'));
  446. const buttons = screen.getAllByRole('button');
  447. await userEvent.click(buttons[buttons.length - 1]);
  448. await userEvent.click(screen.getByText('Show values less than'));
  449. expect(browserHistory.push).toHaveBeenCalledWith({
  450. pathname: location.pathname,
  451. query: expect.objectContaining({
  452. query: 'p99(measurements.custom.kilobyte):<444300',
  453. }),
  454. });
  455. });
  456. });