tableView.spec.tsx 14 KB

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