tableView.spec.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 EventView from 'sentry/utils/discover/eventView';
  9. import TableView from 'sentry/views/discover/table/tableView';
  10. describe('TableView > CellActions', function () {
  11. let initialData, rows, onChangeShowTags;
  12. const location = LocationFixture({
  13. pathname: '/organizations/org-slug/discover/results/',
  14. query: {
  15. id: '42',
  16. name: 'best query',
  17. field: [
  18. 'title',
  19. 'transaction',
  20. 'count()',
  21. 'timestamp',
  22. 'release',
  23. 'equation|count() + 100',
  24. ],
  25. sort: ['title'],
  26. query: '',
  27. project: ['123'],
  28. statsPeriod: '14d',
  29. environment: ['staging'],
  30. yAxis: 'p95',
  31. },
  32. });
  33. const eventView = EventView.fromLocation(location);
  34. function renderComponent(context, tableData, view) {
  35. return render(
  36. <TableView
  37. organization={context.organization}
  38. location={location}
  39. eventView={view}
  40. isLoading={false}
  41. tableData={tableData}
  42. onChangeShowTags={onChangeShowTags}
  43. error={null}
  44. isFirstPage
  45. measurementKeys={null}
  46. showTags={false}
  47. title=""
  48. />,
  49. {router: context.router}
  50. );
  51. }
  52. async function openContextMenu(cellIndex) {
  53. const firstRow = screen.getAllByRole('row')[1];
  54. const emptyValueCell = within(firstRow).getAllByRole('cell')[cellIndex];
  55. await userEvent.click(within(emptyValueCell).getByRole('button', {name: 'Actions'}));
  56. }
  57. beforeEach(function () {
  58. jest.mocked(browserHistory.push).mockReset();
  59. jest.mocked(browserHistory.replace).mockReset();
  60. const organization = OrganizationFixture({
  61. features: ['discover-basic'],
  62. });
  63. initialData = initializeOrg({
  64. organization,
  65. router: {location},
  66. });
  67. act(() => {
  68. ProjectsStore.loadInitialData(initialData.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 = OrganizationFixture();
  297. render(
  298. <TableView
  299. organization={orgWithFeature}
  300. location={location}
  301. eventView={EventView.fromLocation({
  302. ...location,
  303. query: {
  304. ...location.query,
  305. field: [
  306. 'title',
  307. 'p99(measurements.custom.kibibyte)',
  308. 'p99(measurements.custom.kilobyte)',
  309. ],
  310. },
  311. })}
  312. isLoading={false}
  313. tableData={{
  314. data: [
  315. {
  316. id: '1',
  317. title: '/random/transaction/name',
  318. 'p99(measurements.custom.kibibyte)': 222.3,
  319. 'p99(measurements.custom.kilobyte)': 444.3,
  320. },
  321. ],
  322. meta: {
  323. title: 'string',
  324. 'p99(measurements.custom.kibibyte)': 'size',
  325. 'p99(measurements.custom.kilobyte)': 'size',
  326. units: {
  327. 'p99(measurements.custom.kibibyte)': 'kibibyte',
  328. 'p99(measurements.custom.kilobyte)': 'kilobyte',
  329. },
  330. },
  331. }}
  332. onChangeShowTags={onChangeShowTags}
  333. error={null}
  334. isFirstPage
  335. measurementKeys={null}
  336. showTags={false}
  337. title=""
  338. />
  339. );
  340. expect(screen.getByText('222.3 KiB')).toBeInTheDocument();
  341. expect(screen.getByText('444.3 KB')).toBeInTheDocument();
  342. });
  343. it('shows events with value less than selected custom performance metric', async function () {
  344. const orgWithFeature = OrganizationFixture();
  345. render(
  346. <TableView
  347. organization={orgWithFeature}
  348. location={location}
  349. eventView={EventView.fromLocation({
  350. ...location,
  351. query: {
  352. ...location.query,
  353. field: ['title', 'p99(measurements.custom.kilobyte)'],
  354. },
  355. })}
  356. isLoading={false}
  357. tableData={{
  358. data: [
  359. {
  360. id: '1',
  361. title: '/random/transaction/name',
  362. 'p99(measurements.custom.kilobyte)': 444.3,
  363. },
  364. ],
  365. meta: {
  366. title: 'string',
  367. 'p99(measurements.custom.kilobyte)': 'size',
  368. units: {'p99(measurements.custom.kilobyte)': 'kilobyte'},
  369. },
  370. }}
  371. onChangeShowTags={onChangeShowTags}
  372. error={null}
  373. isFirstPage
  374. measurementKeys={null}
  375. showTags={false}
  376. title=""
  377. />
  378. );
  379. await userEvent.hover(screen.getByText('444.3 KB'));
  380. const buttons = screen.getAllByRole('button');
  381. await userEvent.click(buttons[buttons.length - 1]);
  382. await userEvent.click(screen.getByText('Show values less than'));
  383. expect(browserHistory.push).toHaveBeenCalledWith({
  384. pathname: location.pathname,
  385. query: expect.objectContaining({
  386. query: 'p99(measurements.custom.kilobyte):<444300',
  387. }),
  388. });
  389. });
  390. });