tableView.spec.tsx 13 KB

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