tableView.spec.jsx 13 KB

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