tableView.spec.tsx 14 KB

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