addDashboardWidgetModal.spec.jsx 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595
  1. import {browserHistory} from 'react-router';
  2. import selectEvent from 'react-select-event';
  3. import {mountWithTheme} from 'sentry-test/enzyme';
  4. import {initializeOrg} from 'sentry-test/initializeOrg';
  5. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  6. import {getOptionByLabel, openMenu, selectByLabel} from 'sentry-test/select-new';
  7. import {openDashboardWidgetLibraryModal} from 'sentry/actionCreators/modal';
  8. import AddDashboardWidgetModal from 'sentry/components/modals/addDashboardWidgetModal';
  9. import {t} from 'sentry/locale';
  10. import TagStore from 'sentry/stores/tagStore';
  11. import {SessionField} from 'sentry/types';
  12. import * as types from 'sentry/views/dashboardsV2/types';
  13. jest.mock('sentry/actionCreators/modal', () => ({
  14. openDashboardWidgetLibraryModal: jest.fn(),
  15. }));
  16. // Mocking worldMapChart to avoid act warnings
  17. jest.mock('sentry/components/charts/worldMapChart');
  18. const stubEl = props => <div>{props.children}</div>;
  19. function mountModal({
  20. initialData,
  21. onAddWidget,
  22. onUpdateWidget,
  23. widget,
  24. dashboard,
  25. source,
  26. defaultWidgetQuery,
  27. displayType,
  28. defaultTableColumns,
  29. selectedWidgets,
  30. onAddLibraryWidget,
  31. }) {
  32. return mountWithTheme(
  33. <AddDashboardWidgetModal
  34. Header={stubEl}
  35. Footer={stubEl}
  36. Body={stubEl}
  37. organization={initialData.organization}
  38. onAddWidget={onAddWidget}
  39. onUpdateWidget={onUpdateWidget}
  40. widget={widget}
  41. dashboard={dashboard}
  42. closeModal={() => void 0}
  43. source={source || types.DashboardWidgetSource.DASHBOARDS}
  44. defaultWidgetQuery={defaultWidgetQuery}
  45. displayType={displayType}
  46. defaultTableColumns={defaultTableColumns}
  47. selectedWidgets={selectedWidgets}
  48. onAddLibraryWidget={onAddLibraryWidget}
  49. />,
  50. initialData.routerContext
  51. );
  52. }
  53. function mountModalWithRtl({initialData, onAddWidget, onUpdateWidget, widget, source}) {
  54. return render(
  55. <AddDashboardWidgetModal
  56. Header={stubEl}
  57. Body={stubEl}
  58. Footer={stubEl}
  59. CloseButton={stubEl}
  60. organization={initialData.organization}
  61. onAddWidget={onAddWidget}
  62. onUpdateWidget={onUpdateWidget}
  63. widget={widget}
  64. closeModal={() => void 0}
  65. source={source || types.DashboardWidgetSource.DASHBOARDS}
  66. />,
  67. {
  68. organization: initialData.organization,
  69. }
  70. );
  71. }
  72. function clickSubmit(wrapper) {
  73. // Click on submit.
  74. const button = wrapper.find('Button[data-test-id="add-widget"] button');
  75. button.simulate('click');
  76. // Wait for xhr to complete.
  77. return tick();
  78. }
  79. function getDisplayType(wrapper) {
  80. return wrapper.find('input[name="displayType"]');
  81. }
  82. function selectDashboard(wrapper, dashboard) {
  83. const input = wrapper.find('SelectControl[name="dashboard"]');
  84. input.props().onChange(dashboard);
  85. }
  86. async function setSearchConditions(el, query) {
  87. el.find('textarea')
  88. .simulate('change', {target: {value: query}})
  89. .getDOMNode()
  90. .setSelectionRange(query.length, query.length);
  91. await tick();
  92. await el.update();
  93. el.find('textarea').simulate('keydown', {key: 'Enter'});
  94. }
  95. describe('Modals -> AddDashboardWidgetModal', function () {
  96. const initialData = initializeOrg({
  97. organization: {
  98. features: ['performance-view', 'discover-query'],
  99. apdexThreshold: 400,
  100. },
  101. });
  102. const tags = [
  103. {name: 'browser.name', key: 'browser.name'},
  104. {name: 'custom-field', key: 'custom-field'},
  105. ];
  106. const dashboard = TestStubs.Dashboard([], {
  107. id: '1',
  108. title: 'Test Dashboard',
  109. widgetDisplay: ['area'],
  110. });
  111. let eventsStatsMock, metricsDataMock;
  112. beforeEach(function () {
  113. TagStore.loadTagsSuccess(tags);
  114. MockApiClient.addMockResponse({
  115. url: '/organizations/org-slug/dashboards/widgets/',
  116. method: 'POST',
  117. statusCode: 200,
  118. body: [],
  119. });
  120. eventsStatsMock = MockApiClient.addMockResponse({
  121. url: '/organizations/org-slug/events-stats/',
  122. body: [],
  123. });
  124. MockApiClient.addMockResponse({
  125. url: '/organizations/org-slug/eventsv2/',
  126. body: {data: [{'event.type': 'error'}], meta: {'event.type': 'string'}},
  127. });
  128. MockApiClient.addMockResponse({
  129. url: '/organizations/org-slug/events-geo/',
  130. body: {data: [], meta: {}},
  131. });
  132. MockApiClient.addMockResponse({
  133. url: '/organizations/org-slug/recent-searches/',
  134. body: [],
  135. });
  136. MockApiClient.addMockResponse({
  137. url: '/organizations/org-slug/dashboards/',
  138. body: [dashboard],
  139. });
  140. MockApiClient.addMockResponse({
  141. url: '/organizations/org-slug/issues/',
  142. body: [],
  143. });
  144. MockApiClient.addMockResponse({
  145. url: '/organizations/org-slug/metrics/tags/',
  146. body: [{key: 'environment'}, {key: 'release'}, {key: 'session.status'}],
  147. });
  148. metricsDataMock = MockApiClient.addMockResponse({
  149. method: 'GET',
  150. url: '/organizations/org-slug/metrics/data/',
  151. body: TestStubs.MetricsField({field: SessionField.USER}),
  152. });
  153. MockApiClient.addMockResponse({
  154. url: `/organizations/org-slug/metrics/meta/`,
  155. body: TestStubs.MetricsMeta(),
  156. });
  157. });
  158. afterEach(() => {
  159. MockApiClient.clearMockResponses();
  160. });
  161. it('redirects correctly when creating a new dashboard', async function () {
  162. const wrapper = mountModal({
  163. initialData,
  164. source: types.DashboardWidgetSource.DISCOVERV2,
  165. });
  166. await tick();
  167. wrapper.update();
  168. selectDashboard(wrapper, {label: t('+ Create New Dashboard'), value: 'new'});
  169. await clickSubmit(wrapper);
  170. expect(browserHistory.push).toHaveBeenCalledWith(
  171. expect.objectContaining({
  172. pathname: '/organizations/org-slug/dashboards/new/',
  173. })
  174. );
  175. wrapper.unmount();
  176. });
  177. it('redirects correctly when choosing an existing dashboard', async function () {
  178. const wrapper = mountModal({
  179. initialData,
  180. source: types.DashboardWidgetSource.DISCOVERV2,
  181. });
  182. await tick();
  183. wrapper.update();
  184. selectDashboard(wrapper, {label: t('Test Dashboard'), value: '1'});
  185. await clickSubmit(wrapper);
  186. expect(browserHistory.push).toHaveBeenCalledWith(
  187. expect.objectContaining({
  188. pathname: '/organizations/org-slug/dashboard/1/',
  189. })
  190. );
  191. wrapper.unmount();
  192. });
  193. it('disables dashboards with max widgets', async function () {
  194. types.MAX_WIDGETS = 1;
  195. const wrapper = mountModal({
  196. initialData,
  197. source: types.DashboardWidgetSource.DISCOVERV2,
  198. });
  199. await tick();
  200. wrapper.update();
  201. openMenu(wrapper, {name: 'dashboard', control: true});
  202. const input = wrapper.find('SelectControl[name="dashboard"]');
  203. expect(input.find('Option')).toHaveLength(2);
  204. expect(input.find('Option').at(0).props().isDisabled).toBe(false);
  205. expect(input.find('Option').at(1).props().isDisabled).toBe(true);
  206. wrapper.unmount();
  207. });
  208. it('can update the title', async function () {
  209. let widget = undefined;
  210. const wrapper = mountModal({
  211. initialData,
  212. onAddWidget: data => (widget = data),
  213. });
  214. const input = wrapper.find('Input[name="title"] input');
  215. input.simulate('change', {target: {value: 'Unique Users'}});
  216. await clickSubmit(wrapper);
  217. expect(widget.title).toEqual('Unique Users');
  218. wrapper.unmount();
  219. });
  220. it('can add conditions', async function () {
  221. jest.useFakeTimers();
  222. let widget = undefined;
  223. const wrapper = mountModal({
  224. initialData,
  225. onAddWidget: data => (widget = data),
  226. });
  227. // Change the search text on the first query.
  228. const input = wrapper.find('#smart-search-input').first();
  229. input.simulate('change', {target: {value: 'color:blue'}}).simulate('blur');
  230. jest.runAllTimers();
  231. jest.useRealTimers();
  232. await clickSubmit(wrapper);
  233. expect(widget.queries).toHaveLength(1);
  234. expect(widget.queries[0].conditions).toEqual('color:blue');
  235. wrapper.unmount();
  236. });
  237. it('can choose a field', async function () {
  238. let widget = undefined;
  239. const wrapper = mountModal({
  240. initialData,
  241. onAddWidget: data => (widget = data),
  242. });
  243. // No delete button as there is only one field.
  244. expect(wrapper.find('IconDelete')).toHaveLength(0);
  245. selectByLabel(wrapper, 'p95(\u2026)', {name: 'field', at: 0, control: true});
  246. await clickSubmit(wrapper);
  247. expect(widget.queries).toHaveLength(1);
  248. expect(widget.queries[0].fields).toEqual(['p95(transaction.duration)']);
  249. wrapper.unmount();
  250. });
  251. it('updates widget aggregate', async function () {
  252. let widget = undefined;
  253. const wrapper = mountModal({
  254. initialData,
  255. onAddWidget: data => (widget = data),
  256. });
  257. // No delete button as there is only one field.
  258. expect(wrapper.find('IconDelete')).toHaveLength(0);
  259. selectByLabel(wrapper, 'p95(\u2026)', {name: 'field', at: 0, control: true});
  260. await clickSubmit(wrapper);
  261. expect(widget.queries).toHaveLength(1);
  262. expect(widget.queries[0].fields).toEqual(['p95(transaction.duration)']);
  263. expect(widget.queries[0].aggregates).toEqual(['p95(transaction.duration)']);
  264. wrapper.unmount();
  265. });
  266. it('can add additional fields', async function () {
  267. let widget = undefined;
  268. const wrapper = mountModal({
  269. initialData,
  270. onAddWidget: data => (widget = data),
  271. });
  272. // Select Line chart display
  273. selectByLabel(wrapper, 'Line Chart', {name: 'displayType', at: 0, control: true});
  274. // Click the add button
  275. const add = wrapper.find('button[aria-label="Add Overlay"]');
  276. add.simulate('click');
  277. wrapper.update();
  278. // Should be another field input.
  279. expect(wrapper.find('QueryField')).toHaveLength(2);
  280. selectByLabel(wrapper, 'p95(\u2026)', {name: 'field', at: 1, control: true});
  281. await clickSubmit(wrapper);
  282. expect(widget.queries).toHaveLength(1);
  283. expect(widget.queries[0].fields).toEqual(['count()', 'p95(transaction.duration)']);
  284. expect(widget.queries[0].aggregates).toEqual([
  285. 'count()',
  286. 'p95(transaction.duration)',
  287. ]);
  288. expect(widget.queries[0].columns).toEqual([]);
  289. wrapper.unmount();
  290. });
  291. it('can add equation fields', async function () {
  292. let widget = undefined;
  293. const wrapper = mountModal({
  294. initialData,
  295. onAddWidget: data => (widget = data),
  296. });
  297. // Select Line chart display
  298. selectByLabel(wrapper, 'Line Chart', {name: 'displayType', at: 0, control: true});
  299. // Click the add button
  300. const add = wrapper.find('button[aria-label="Add an Equation"]');
  301. add.simulate('click');
  302. wrapper.update();
  303. // Should be another field input.
  304. expect(wrapper.find('QueryField')).toHaveLength(2);
  305. expect(wrapper.find('ArithmeticInput')).toHaveLength(1);
  306. wrapper
  307. .find('QueryFieldWrapper input[name="arithmetic"]')
  308. .simulate('change', {target: {value: 'count() + 100'}})
  309. .simulate('blur');
  310. wrapper.update();
  311. await clickSubmit(wrapper);
  312. expect(widget.queries).toHaveLength(1);
  313. expect(widget.queries[0].fields).toEqual(['count()', 'equation|count() + 100']);
  314. expect(widget.queries[0].aggregates).toEqual(['count()', 'equation|count() + 100']);
  315. wrapper.unmount();
  316. });
  317. it('additional fields get added to new seach filters', async function () {
  318. MockApiClient.addMockResponse({
  319. url: '/organizations/org-slug/recent-searches/',
  320. method: 'POST',
  321. body: [],
  322. });
  323. let widget = undefined;
  324. const wrapper = mountModal({
  325. initialData,
  326. onAddWidget: data => (widget = data),
  327. });
  328. // Select Line chart display
  329. selectByLabel(wrapper, 'Line Chart', {name: 'displayType', at: 0, control: true});
  330. // Click the add button
  331. const add = wrapper.find('button[aria-label="Add Overlay"]');
  332. add.simulate('click');
  333. wrapper.update();
  334. // Should be another field input.
  335. expect(wrapper.find('QueryField')).toHaveLength(2);
  336. selectByLabel(wrapper, 'p95(\u2026)', {name: 'field', at: 1, control: true});
  337. await clickSubmit(wrapper);
  338. expect(widget.queries).toHaveLength(1);
  339. expect(widget.queries[0].fields).toEqual(['count()', 'p95(transaction.duration)']);
  340. expect(widget.queries[0].aggregates).toEqual([
  341. 'count()',
  342. 'p95(transaction.duration)',
  343. ]);
  344. // Add another search filter
  345. const addQuery = wrapper.find('button[aria-label="Add Query"]');
  346. addQuery.simulate('click');
  347. wrapper.update();
  348. // Set second query search conditions
  349. const secondSearchBar = wrapper.find('SearchConditionsWrapper StyledSearchBar').at(1);
  350. await setSearchConditions(secondSearchBar, 'event.type:error');
  351. // Set second query legend alias
  352. wrapper
  353. .find('SearchConditionsWrapper input[placeholder="Legend Alias"]')
  354. .at(1)
  355. .simulate('change', {target: {value: 'Errors'}});
  356. // Save widget
  357. await clickSubmit(wrapper);
  358. expect(widget.queries[0]).toMatchObject({
  359. name: '',
  360. conditions: '',
  361. fields: ['count()', 'p95(transaction.duration)'],
  362. aggregates: ['count()', 'p95(transaction.duration)'],
  363. });
  364. expect(widget.queries[1]).toMatchObject({
  365. name: 'Errors',
  366. conditions: 'event.type:error',
  367. fields: ['count()', 'p95(transaction.duration)'],
  368. aggregates: ['count()', 'p95(transaction.duration)'],
  369. });
  370. wrapper.unmount();
  371. });
  372. it('can add and delete additional queries', async function () {
  373. MockApiClient.addMockResponse({
  374. url: '/organizations/org-slug/tags/event.type/values/',
  375. body: [{count: 2, name: 'Nvidia 1080ti'}],
  376. });
  377. MockApiClient.addMockResponse({
  378. url: '/organizations/org-slug/recent-searches/',
  379. method: 'POST',
  380. body: [],
  381. });
  382. let widget = undefined;
  383. const wrapper = mountModal({
  384. initialData,
  385. onAddWidget: data => (widget = data),
  386. });
  387. // Select Line chart display
  388. selectByLabel(wrapper, 'Line Chart', {name: 'displayType', at: 0, control: true});
  389. // Set first query search conditions
  390. await setSearchConditions(
  391. wrapper.find('SearchConditionsWrapper StyledSearchBar'),
  392. 'event.type:transaction'
  393. );
  394. // Set first query legend alias
  395. wrapper
  396. .find('SearchConditionsWrapper input[placeholder="Legend Alias"]')
  397. .simulate('change', {target: {value: 'Transactions'}});
  398. // Click the "Add Query" button twice
  399. const addQuery = wrapper.find('button[aria-label="Add Query"]');
  400. addQuery.simulate('click');
  401. wrapper.update();
  402. addQuery.simulate('click');
  403. wrapper.update();
  404. // Expect three search bars
  405. expect(wrapper.find('StyledSearchBar')).toHaveLength(3);
  406. // Expect "Add Query" button to be hidden since we're limited to at most 3 search conditions
  407. expect(wrapper.find('button[aria-label="Add Query"]')).toHaveLength(0);
  408. // Delete second query
  409. expect(wrapper.find('button[aria-label="Remove query"]')).toHaveLength(3);
  410. wrapper.find('button[aria-label="Remove query"]').at(1).simulate('click');
  411. wrapper.update();
  412. // Expect "Add Query" button to be shown again
  413. expect(wrapper.find('button[aria-label="Add Query"]')).toHaveLength(1);
  414. // Set second query search conditions
  415. const secondSearchBar = wrapper.find('SearchConditionsWrapper StyledSearchBar').at(1);
  416. await setSearchConditions(secondSearchBar, 'event.type:error');
  417. // Set second query legend alias
  418. wrapper
  419. .find('SearchConditionsWrapper input[placeholder="Legend Alias"]')
  420. .at(1)
  421. .simulate('change', {target: {value: 'Errors'}});
  422. // Save widget
  423. await clickSubmit(wrapper);
  424. expect(widget.queries).toHaveLength(2);
  425. expect(widget.queries[0]).toMatchObject({
  426. name: 'Transactions',
  427. conditions: 'event.type:transaction',
  428. fields: ['count()'],
  429. });
  430. expect(widget.queries[1]).toMatchObject({
  431. name: 'Errors',
  432. conditions: 'event.type:error',
  433. fields: ['count()'],
  434. });
  435. wrapper.unmount();
  436. });
  437. it('can respond to validation feedback', async function () {
  438. MockApiClient.addMockResponse({
  439. url: '/organizations/org-slug/dashboards/widgets/',
  440. method: 'POST',
  441. statusCode: 400,
  442. body: {
  443. title: ['This field is required'],
  444. queries: [{conditions: ['Invalid value']}],
  445. },
  446. });
  447. let widget = undefined;
  448. const wrapper = mountModal({
  449. initialData,
  450. onAddWidget: data => (widget = data),
  451. });
  452. await clickSubmit(wrapper);
  453. wrapper.update();
  454. // API request should fail and not add widget.
  455. expect(widget).toBeUndefined();
  456. const errors = wrapper.find('FieldErrorReason');
  457. expect(errors).toHaveLength(2);
  458. // Nested object error should display
  459. const conditionError = wrapper.find('WidgetQueriesForm FieldErrorReason');
  460. expect(conditionError).toHaveLength(1);
  461. wrapper.unmount();
  462. });
  463. it('can edit a widget', async function () {
  464. let widget = {
  465. id: '9',
  466. title: 'Errors over time',
  467. interval: '5m',
  468. displayType: 'line',
  469. queries: [
  470. {
  471. id: '9',
  472. name: 'errors',
  473. conditions: 'event.type:error',
  474. fields: ['count()', 'count_unique(id)'],
  475. aggregates: ['count()', 'count_unique(id)'],
  476. columns: [],
  477. },
  478. {
  479. id: '9',
  480. name: 'csp',
  481. conditions: 'event.type:csp',
  482. fields: ['count()', 'count_unique(id)'],
  483. aggregates: ['count()', 'count_unique(id)'],
  484. columns: [],
  485. },
  486. ],
  487. };
  488. const onAdd = jest.fn();
  489. const wrapper = mountModal({
  490. initialData,
  491. widget,
  492. onAddWidget: onAdd,
  493. onUpdateWidget: data => {
  494. widget = data;
  495. },
  496. });
  497. // Should be in edit 'mode'
  498. const heading = wrapper.find('h4');
  499. expect(heading.text()).toContain('Edit');
  500. // Should set widget data up.
  501. const title = wrapper.find('Input[name="title"]');
  502. expect(title.props().value).toEqual(widget.title);
  503. expect(wrapper.find('input[name="displayType"]').props().value).toEqual(
  504. widget.displayType
  505. );
  506. expect(wrapper.find('WidgetQueriesForm')).toHaveLength(1);
  507. expect(wrapper.find('StyledSearchBar')).toHaveLength(2);
  508. expect(wrapper.find('QueryField')).toHaveLength(2);
  509. // Expect events-stats endpoint to be called for each search conditions with
  510. // the same y-axis parameters
  511. expect(eventsStatsMock).toHaveBeenNthCalledWith(
  512. 1,
  513. '/organizations/org-slug/events-stats/',
  514. expect.objectContaining({
  515. query: expect.objectContaining({
  516. query: 'event.type:error',
  517. yAxis: ['count()', 'count_unique(id)'],
  518. }),
  519. })
  520. );
  521. expect(eventsStatsMock).toHaveBeenNthCalledWith(
  522. 2,
  523. '/organizations/org-slug/events-stats/',
  524. expect.objectContaining({
  525. query: expect.objectContaining({
  526. query: 'event.type:csp',
  527. yAxis: ['count()', 'count_unique(id)'],
  528. }),
  529. })
  530. );
  531. title.simulate('change', {target: {value: 'New title'}});
  532. await clickSubmit(wrapper);
  533. expect(onAdd).not.toHaveBeenCalled();
  534. expect(widget.title).toEqual('New title');
  535. expect(eventsStatsMock).toHaveBeenCalledTimes(2);
  536. wrapper.unmount();
  537. });
  538. it('renders column inputs for table widgets', async function () {
  539. MockApiClient.addMockResponse({
  540. url: '/organizations/org-slug/eventsv2/',
  541. method: 'GET',
  542. statusCode: 200,
  543. body: {
  544. meta: {},
  545. data: [],
  546. },
  547. });
  548. let widget = {
  549. id: '9',
  550. title: 'sdk usage',
  551. interval: '5m',
  552. displayType: 'table',
  553. queries: [
  554. {
  555. id: '9',
  556. name: 'errors',
  557. conditions: 'event.type:error',
  558. fields: ['sdk.name', 'count()'],
  559. aggregates: ['count()'],
  560. columns: ['sdk.name'],
  561. orderby: 'count()',
  562. },
  563. ],
  564. };
  565. const wrapper = mountModal({
  566. initialData,
  567. widget,
  568. onAddWidget: jest.fn(),
  569. onUpdateWidget: data => {
  570. widget = data;
  571. },
  572. });
  573. // Should be in edit 'mode'
  574. const heading = wrapper.find('h4').first();
  575. expect(heading.text()).toContain('Edit');
  576. // Should set widget data up.
  577. const title = wrapper.find('Input[name="title"]');
  578. expect(title.props().value).toEqual(widget.title);
  579. expect(wrapper.find('input[name="displayType"]').props().value).toEqual(
  580. widget.displayType
  581. );
  582. expect(wrapper.find('WidgetQueriesForm')).toHaveLength(1);
  583. // Should have an orderby select
  584. expect(wrapper.find('WidgetQueriesForm SelectControl[name="orderby"]')).toHaveLength(
  585. 1
  586. );
  587. expect(
  588. wrapper
  589. .find('WidgetQueriesForm SelectControl[name="orderby"] SingleValue')
  590. .first()
  591. .text()
  592. ).toEqual('count() asc');
  593. // Add a column, and choose a value,
  594. wrapper.find('button[aria-label="Add a Column"]').simulate('click');
  595. wrapper.update();
  596. selectByLabel(wrapper, 'trace', {name: 'field', at: 2, control: true});
  597. wrapper.update();
  598. await clickSubmit(wrapper);
  599. // A new field should be added.
  600. expect(widget.queries[0].fields).toHaveLength(3);
  601. expect(widget.queries[0].fields).toEqual(['sdk.name', 'count()', 'trace']);
  602. expect(widget.queries[0].aggregates).toEqual(['count()']);
  603. expect(widget.queries[0].columns).toEqual(['sdk.name', 'trace']);
  604. wrapper.unmount();
  605. });
  606. it('uses count() columns if there are no aggregate fields remaining when switching from table to chart', async function () {
  607. let widget = undefined;
  608. const wrapper = mountModal({
  609. initialData,
  610. onAddWidget: data => (widget = data),
  611. });
  612. // No delete button as there is only one field.
  613. expect(wrapper.find('IconDelete')).toHaveLength(0);
  614. // Select Table display
  615. selectByLabel(wrapper, 'Table', {name: 'displayType', at: 0, control: true});
  616. expect(getDisplayType(wrapper).props().value).toEqual('table');
  617. // Add field column
  618. selectByLabel(wrapper, 'event.type', {name: 'field', at: 0, control: true});
  619. let fieldColumn = wrapper.find('input[name="field"]');
  620. expect(fieldColumn.props().value).toEqual({
  621. kind: 'field',
  622. meta: {dataType: 'string', name: 'event.type'},
  623. });
  624. // Select Line chart display
  625. selectByLabel(wrapper, 'Line Chart', {name: 'displayType', at: 0, control: true});
  626. expect(getDisplayType(wrapper).props().value).toEqual('line');
  627. // Expect event.type field to be converted to count()
  628. fieldColumn = wrapper.find('input[name="field"]');
  629. expect(fieldColumn.props().value).toEqual({
  630. kind: 'function',
  631. meta: {name: 'count', parameters: []},
  632. });
  633. await clickSubmit(wrapper);
  634. expect(widget.queries).toHaveLength(1);
  635. expect(widget.queries[0].fields).toEqual(['count()']);
  636. wrapper.unmount();
  637. });
  638. it('should filter out non-aggregate fields when switching from table to chart', async function () {
  639. let widget = undefined;
  640. const wrapper = mountModal({
  641. initialData,
  642. onAddWidget: data => (widget = data),
  643. });
  644. // No delete button as there is only one field.
  645. expect(wrapper.find('IconDelete')).toHaveLength(0);
  646. // Select Table display
  647. selectByLabel(wrapper, 'Table', {name: 'displayType', at: 0, control: true});
  648. expect(getDisplayType(wrapper).props().value).toEqual('table');
  649. // Click the add button
  650. const add = wrapper.find('button[aria-label="Add a Column"]');
  651. add.simulate('click');
  652. wrapper.update();
  653. // Add columns
  654. selectByLabel(wrapper, 'event.type', {name: 'field', at: 0, control: true});
  655. let fieldColumn = wrapper.find('input[name="field"]').at(0);
  656. expect(fieldColumn.props().value).toEqual({
  657. kind: 'field',
  658. meta: {dataType: 'string', name: 'event.type'},
  659. });
  660. selectByLabel(wrapper, 'p95(\u2026)', {name: 'field', at: 1, control: true});
  661. fieldColumn = wrapper.find('input[name="field"]').at(1);
  662. expect(fieldColumn.props().value).toMatchObject({
  663. kind: 'function',
  664. meta: {
  665. name: 'p95',
  666. parameters: [{defaultValue: 'transaction.duration', kind: 'column'}],
  667. },
  668. });
  669. // Select Line chart display
  670. selectByLabel(wrapper, 'Line Chart', {name: 'displayType', at: 0, control: true});
  671. expect(getDisplayType(wrapper).props().value).toEqual('line');
  672. // Expect event.type field to be converted to count()
  673. fieldColumn = wrapper.find('input[name="field"]');
  674. expect(fieldColumn.length).toEqual(1);
  675. expect(fieldColumn.props().value).toMatchObject({
  676. kind: 'function',
  677. meta: {
  678. name: 'p95',
  679. parameters: [{defaultValue: 'transaction.duration', kind: 'column'}],
  680. },
  681. });
  682. await clickSubmit(wrapper);
  683. expect(widget.queries).toHaveLength(1);
  684. expect(widget.queries[0].fields).toEqual(['p95(transaction.duration)']);
  685. expect(widget.queries[0].aggregates).toEqual(['p95(transaction.duration)']);
  686. expect(widget.queries[0].columns).toEqual([]);
  687. wrapper.unmount();
  688. });
  689. it('should filter non-legal y-axis choices for timeseries widget charts', async function () {
  690. let widget = undefined;
  691. const wrapper = mountModal({
  692. initialData,
  693. onAddWidget: data => (widget = data),
  694. });
  695. // Select Line chart display
  696. selectByLabel(wrapper, 'Line Chart', {name: 'displayType', at: 0, control: true});
  697. // No delete button as there is only one field.
  698. expect(wrapper.find('IconDelete')).toHaveLength(0);
  699. selectByLabel(wrapper, 'any(\u2026)', {
  700. name: 'field',
  701. at: 0,
  702. control: true,
  703. });
  704. // Expect user.display to not be an available parameter option for any()
  705. // for line (timeseries) widget charts
  706. const option = getOptionByLabel(wrapper, 'user.display', {
  707. name: 'parameter',
  708. at: 0,
  709. control: true,
  710. });
  711. expect(option.exists()).toEqual(false);
  712. // Be able to choose a numeric-like option for any()
  713. selectByLabel(wrapper, 'measurements.lcp', {
  714. name: 'parameter',
  715. at: 0,
  716. control: true,
  717. });
  718. await clickSubmit(wrapper);
  719. expect(widget.displayType).toEqual('line');
  720. expect(widget.queries).toHaveLength(1);
  721. expect(widget.queries[0].fields).toEqual(['any(measurements.lcp)']);
  722. expect(widget.queries[0].aggregates).toEqual(['any(measurements.lcp)']);
  723. expect(widget.queries[0].columns).toEqual([]);
  724. wrapper.unmount();
  725. });
  726. it('should not filter y-axis choices for big number widget charts', async function () {
  727. let widget = undefined;
  728. const wrapper = mountModal({
  729. initialData,
  730. onAddWidget: data => (widget = data),
  731. });
  732. // No delete button as there is only one field.
  733. expect(wrapper.find('IconDelete')).toHaveLength(0);
  734. // Select Big number display
  735. selectByLabel(wrapper, 'Big Number', {name: 'displayType', at: 0, control: true});
  736. expect(getDisplayType(wrapper).props().value).toEqual('big_number');
  737. selectByLabel(wrapper, 'count_unique(\u2026)', {
  738. name: 'field',
  739. at: 0,
  740. control: true,
  741. });
  742. // Be able to choose a non numeric-like option for count_unique()
  743. selectByLabel(wrapper, 'user.display', {
  744. name: 'parameter',
  745. at: 0,
  746. control: true,
  747. });
  748. await clickSubmit(wrapper);
  749. expect(widget.displayType).toEqual('big_number');
  750. expect(widget.queries).toHaveLength(1);
  751. expect(widget.queries[0].fields).toEqual(['count_unique(user.display)']);
  752. expect(widget.queries[0].aggregates).toEqual(['count_unique(user.display)']);
  753. expect(widget.queries[0].columns).toEqual([]);
  754. wrapper.unmount();
  755. });
  756. it('should filter y-axis choices for world map widget charts', async function () {
  757. let widget = undefined;
  758. const wrapper = mountModal({
  759. initialData,
  760. onAddWidget: data => (widget = data),
  761. });
  762. // No delete button as there is only one field.
  763. expect(wrapper.find('IconDelete')).toHaveLength(0);
  764. // Select World Map display
  765. selectByLabel(wrapper, 'World Map', {name: 'displayType', at: 0, control: true});
  766. expect(getDisplayType(wrapper).props().value).toEqual('world_map');
  767. // Choose any()
  768. selectByLabel(wrapper, 'any(\u2026)', {
  769. name: 'field',
  770. at: 0,
  771. control: true,
  772. });
  773. // user.display should be filtered out for any()
  774. const option = getOptionByLabel(wrapper, 'user.display', {
  775. name: 'parameter',
  776. at: 0,
  777. control: true,
  778. });
  779. expect(option.exists()).toEqual(false);
  780. selectByLabel(wrapper, 'measurements.lcp', {
  781. name: 'parameter',
  782. at: 0,
  783. control: true,
  784. });
  785. // Choose count_unique()
  786. selectByLabel(wrapper, 'count_unique(\u2026)', {
  787. name: 'field',
  788. at: 0,
  789. control: true,
  790. });
  791. // user.display not should be filtered out for count_unique()
  792. selectByLabel(wrapper, 'user.display', {
  793. name: 'parameter',
  794. at: 0,
  795. control: true,
  796. });
  797. // Be able to choose a numeric-like option
  798. selectByLabel(wrapper, 'measurements.lcp', {
  799. name: 'parameter',
  800. at: 0,
  801. control: true,
  802. });
  803. await clickSubmit(wrapper);
  804. expect(widget.displayType).toEqual('world_map');
  805. expect(widget.queries).toHaveLength(1);
  806. expect(widget.queries[0].fields).toEqual(['count_unique(measurements.lcp)']);
  807. expect(widget.queries[0].aggregates).toEqual(['count_unique(measurements.lcp)']);
  808. expect(widget.queries[0].columns).toEqual([]);
  809. wrapper.unmount();
  810. });
  811. it('should filter y-axis choices by output type when switching from big number to line chart', async function () {
  812. let widget = undefined;
  813. const wrapper = mountModal({
  814. initialData,
  815. onAddWidget: data => (widget = data),
  816. });
  817. // No delete button as there is only one field.
  818. expect(wrapper.find('IconDelete')).toHaveLength(0);
  819. // Select Big Number display
  820. selectByLabel(wrapper, 'Big Number', {name: 'displayType', at: 0, control: true});
  821. expect(getDisplayType(wrapper).props().value).toEqual('big_number');
  822. // Choose any()
  823. selectByLabel(wrapper, 'any(\u2026)', {
  824. name: 'field',
  825. at: 0,
  826. control: true,
  827. });
  828. selectByLabel(wrapper, 'id', {
  829. name: 'parameter',
  830. at: 0,
  831. control: true,
  832. });
  833. // Select Line chart display
  834. selectByLabel(wrapper, 'Line Chart', {name: 'displayType', at: 0, control: true});
  835. expect(getDisplayType(wrapper).props().value).toEqual('line');
  836. // Expect event.type field to be converted to count()
  837. const fieldColumn = wrapper.find('input[name="field"]');
  838. expect(fieldColumn.length).toEqual(1);
  839. expect(fieldColumn.props().value).toMatchObject({
  840. kind: 'function',
  841. meta: {
  842. name: 'count',
  843. parameters: [],
  844. },
  845. });
  846. await clickSubmit(wrapper);
  847. expect(widget.displayType).toEqual('line');
  848. expect(widget.queries).toHaveLength(1);
  849. expect(widget.queries[0].fields).toEqual(['count()']);
  850. expect(widget.queries[0].aggregates).toEqual(['count()']);
  851. expect(widget.queries[0].columns).toEqual([]);
  852. wrapper.unmount();
  853. });
  854. it('should automatically add columns for top n widget charts', async function () {
  855. const wrapper = mountModal({
  856. initialData,
  857. onAddWidget: data => (widget = data),
  858. displayType: types.DisplayType.TOP_N,
  859. defaultTableColumns: ['title', 'count()', 'count_unique(user)', 'epm()'],
  860. defaultWidgetQuery: {
  861. name: '',
  862. fields: ['title', 'count()', 'count_unique(user)', 'epm()', 'count()'],
  863. columns: ['title'],
  864. aggregates: ['count()', 'count_unique(user)', 'epm()', 'count()'],
  865. conditions: 'tag:value',
  866. orderby: '',
  867. },
  868. });
  869. // Select Top n display
  870. expect(getDisplayType(wrapper).props().value).toEqual('top_n');
  871. // No delete button as there is only one field.
  872. expect(wrapper.find('IconDelete')).toHaveLength(0);
  873. // Restricting to a single query
  874. expect(wrapper.find('button[aria-label="Add Query"]')).toHaveLength(0);
  875. // Restricting to a single y-axis
  876. expect(wrapper.find('button[aria-label="Add Overlay"]')).toHaveLength(0);
  877. const titleColumn = wrapper.find('input[name="field"]').at(0);
  878. expect(titleColumn.props().value).toEqual({
  879. kind: 'field',
  880. meta: {dataType: 'string', name: 'title'},
  881. });
  882. const countColumn = wrapper.find('input[name="field"]').at(1);
  883. expect(countColumn.props().value).toEqual({
  884. kind: 'function',
  885. meta: {parameters: [], name: 'count'},
  886. });
  887. expect(wrapper.find('WidgetQueriesForm Field[data-test-id="y-axis"]')).toHaveLength(
  888. 1
  889. );
  890. expect(wrapper.find('WidgetQueriesForm SelectControl[name="orderby"]')).toHaveLength(
  891. 1
  892. );
  893. await tick();
  894. wrapper.unmount();
  895. });
  896. it('should use defaultWidgetQuery Y-Axis and Conditions if given a defaultWidgetQuery', async function () {
  897. const wrapper = mountModal({
  898. initialData,
  899. onAddWidget: () => undefined,
  900. onUpdateWidget: () => undefined,
  901. widget: undefined,
  902. source: types.DashboardWidgetSource.DISCOVERV2,
  903. defaultWidgetQuery: {
  904. name: '',
  905. fields: ['count()', 'failure_count()', 'count_unique(user)'],
  906. aggregates: ['count()', 'failure_count()', 'count_unique(user)'],
  907. columns: [],
  908. conditions: 'tag:value',
  909. orderby: '',
  910. },
  911. });
  912. expect(wrapper.find('SearchBar').props().query).toEqual('tag:value');
  913. const queryFields = wrapper.find('QueryField');
  914. expect(queryFields.length).toEqual(3);
  915. expect(queryFields.at(0).props().fieldValue.function[0]).toEqual('count');
  916. expect(queryFields.at(1).props().fieldValue.function[0]).toEqual('failure_count');
  917. expect(queryFields.at(2).props().fieldValue.function[0]).toEqual('count_unique');
  918. await tick();
  919. wrapper.unmount();
  920. });
  921. it('uses displayType if given a displayType', async function () {
  922. const wrapper = mountModal({
  923. initialData,
  924. onAddWidget: () => undefined,
  925. onUpdateWidget: () => undefined,
  926. source: types.DashboardWidgetSource.DISCOVERV2,
  927. displayType: types.DisplayType.BAR,
  928. });
  929. expect(wrapper.find('SelectPicker').at(1).props().value.value).toEqual('bar');
  930. await tick();
  931. wrapper.unmount();
  932. });
  933. it('correctly defaults fields and orderby when in Top N display', async function () {
  934. const wrapper = mountModal({
  935. initialData,
  936. onAddWidget: () => undefined,
  937. onUpdateWidget: () => undefined,
  938. source: types.DashboardWidgetSource.DISCOVERV2,
  939. displayType: types.DisplayType.TOP_N,
  940. defaultWidgetQuery: {
  941. fields: ['title', 'count()', 'count_unique(user)'],
  942. aggregates: ['count()', 'count_unique(user)'],
  943. columns: ['title'],
  944. orderby: '-count_unique_user',
  945. },
  946. defaultTableColumns: ['title', 'count()'],
  947. });
  948. expect(wrapper.find('SelectPicker').at(1).props().value.value).toEqual('top_n');
  949. expect(wrapper.find('WidgetQueriesForm').props().queries[0].fields).toEqual([
  950. 'title',
  951. 'count()',
  952. 'count_unique(user)',
  953. ]);
  954. expect(wrapper.find('WidgetQueriesForm').props().queries[0].orderby).toEqual(
  955. '-count_unique_user'
  956. );
  957. await tick();
  958. wrapper.unmount();
  959. });
  960. it('submits custom widget correctly', async function () {
  961. const onAddLibraryWidgetMock = jest.fn();
  962. const wrapper = mountModal({
  963. initialData,
  964. dashboard,
  965. onAddLibraryWidget: onAddLibraryWidgetMock,
  966. source: types.DashboardWidgetSource.LIBRARY,
  967. });
  968. const input = wrapper.find('Input[name="title"] input');
  969. input.simulate('change', {target: {value: 'All Events'}});
  970. await clickSubmit(wrapper);
  971. expect(onAddLibraryWidgetMock).toHaveBeenCalledTimes(1);
  972. wrapper.unmount();
  973. });
  974. it('renders the tab button bar from widget library', function () {
  975. const onAddLibraryWidgetMock = jest.fn();
  976. const wrapper = mountModal({
  977. initialData,
  978. dashboard,
  979. onAddLibraryWidget: onAddLibraryWidgetMock,
  980. source: types.DashboardWidgetSource.LIBRARY,
  981. });
  982. expect(wrapper.find('LibraryButton')).toHaveLength(1);
  983. expect(wrapper.find('CustomButton')).toHaveLength(1);
  984. wrapper.find('LibraryButton button').simulate('click');
  985. expect(openDashboardWidgetLibraryModal).toHaveBeenCalledTimes(1);
  986. wrapper.unmount();
  987. });
  988. it('sets widgetType to discover', async function () {
  989. const onAdd = jest.fn();
  990. const wrapper = mountModal({
  991. initialData,
  992. onAddWidget: onAdd,
  993. onUpdateWidget: () => undefined,
  994. });
  995. await clickSubmit(wrapper);
  996. expect(onAdd).toHaveBeenCalledWith(expect.objectContaining({widgetType: 'discover'}));
  997. wrapper.unmount();
  998. });
  999. it('limits TopN display to one query when switching from another visualization', async () => {
  1000. render(
  1001. <AddDashboardWidgetModal
  1002. Header={stubEl}
  1003. Body={stubEl}
  1004. Footer={stubEl}
  1005. CloseButton={stubEl}
  1006. organization={initialData.organization}
  1007. onAddWidget={() => undefined}
  1008. onUpdateWidget={() => undefined}
  1009. widget={initialData.widget}
  1010. closeModal={() => void 0}
  1011. source={types.DashboardWidgetSource.DASHBOARDS}
  1012. />
  1013. );
  1014. userEvent.click(screen.getByText('Table'));
  1015. userEvent.click(await screen.findByText('Bar Chart'));
  1016. userEvent.click(screen.getByText('Add Query'));
  1017. userEvent.click(screen.getByText('Add Query'));
  1018. expect(
  1019. screen.getAllByPlaceholderText('Search for events, users, tags, and more').length
  1020. ).toEqual(3);
  1021. userEvent.click(screen.getByText('Bar Chart'));
  1022. userEvent.click(await screen.findByText('Top 5 Events'));
  1023. expect(
  1024. screen.getAllByPlaceholderText('Search for events, users, tags, and more').length
  1025. ).toEqual(1);
  1026. });
  1027. describe('Issue Widgets', function () {
  1028. it('sets widgetType to issues', async function () {
  1029. initialData.organization.features = ['performance-view', 'discover-query'];
  1030. const onAdd = jest.fn(() => {});
  1031. const wrapper = mountModalWithRtl({
  1032. initialData,
  1033. onAddWidget: onAdd,
  1034. onUpdateWidget: () => undefined,
  1035. });
  1036. userEvent.click(screen.getByText('Issues (States, Assignment, Time, etc.)'));
  1037. userEvent.click(screen.getByTestId('add-widget'));
  1038. await tick();
  1039. expect(onAdd).toHaveBeenCalledWith(
  1040. expect.objectContaining({
  1041. displayType: 'table',
  1042. interval: '5m',
  1043. queries: [
  1044. {
  1045. conditions: '',
  1046. fields: ['issue', 'assignee', 'title'],
  1047. name: '',
  1048. orderby: '',
  1049. aggregates: [],
  1050. columns: ['issue', 'assignee', 'title'],
  1051. },
  1052. ],
  1053. title: '',
  1054. widgetType: 'issue',
  1055. })
  1056. );
  1057. wrapper.unmount();
  1058. });
  1059. it('does not render the dataset selector', async function () {
  1060. initialData.organization.features = ['performance-view', 'discover-query'];
  1061. const wrapper = mountModalWithRtl({
  1062. initialData,
  1063. onAddWidget: () => undefined,
  1064. onUpdateWidget: () => undefined,
  1065. source: types.DashboardWidgetSource.DISCOVERV2,
  1066. });
  1067. await tick();
  1068. userEvent.click(screen.getByText('Table'));
  1069. userEvent.click(screen.getByText('Line Chart'));
  1070. expect(screen.queryByText('Dataset')).not.toBeInTheDocument();
  1071. wrapper.unmount();
  1072. });
  1073. it('renders the dataset selector', function () {
  1074. initialData.organization.features = ['performance-view', 'discover-query'];
  1075. const wrapper = mountModalWithRtl({
  1076. initialData,
  1077. onAddWidget: () => undefined,
  1078. onUpdateWidget: () => undefined,
  1079. source: types.DashboardWidgetSource.DASHBOARDS,
  1080. });
  1081. expect(metricsDataMock).not.toHaveBeenCalled();
  1082. expect(screen.getByText('Dataset')).toBeInTheDocument();
  1083. expect(
  1084. screen.getByText('All Events (Errors and Transactions)')
  1085. ).toBeInTheDocument();
  1086. expect(
  1087. screen.getByText('Issues (States, Assignment, Time, etc.)')
  1088. ).toBeInTheDocument();
  1089. // Hide without the dashboards-releases feature flag
  1090. expect(screen.queryByText(/release/i)).not.toBeInTheDocument();
  1091. wrapper.unmount();
  1092. });
  1093. it('disables moving and deleting issue column', async function () {
  1094. initialData.organization.features = ['performance-view', 'discover-query'];
  1095. const wrapper = mountModalWithRtl({
  1096. initialData,
  1097. onAddWidget: () => undefined,
  1098. onUpdateWidget: () => undefined,
  1099. source: types.DashboardWidgetSource.DASHBOARDS,
  1100. });
  1101. userEvent.click(screen.getByText('Issues (States, Assignment, Time, etc.)'));
  1102. await tick();
  1103. expect(screen.getByText('issue')).toBeInTheDocument();
  1104. expect(screen.getByText('assignee')).toBeInTheDocument();
  1105. expect(screen.getByText('title')).toBeInTheDocument();
  1106. expect(screen.getAllByRole('button', {name: 'Remove column'}).length).toEqual(2);
  1107. expect(screen.getAllByRole('button', {name: 'Drag to reorder'}).length).toEqual(3);
  1108. userEvent.click(screen.getAllByRole('button', {name: 'Remove column'})[1]);
  1109. userEvent.click(screen.getAllByRole('button', {name: 'Remove column'})[0]);
  1110. await tick();
  1111. expect(screen.getByText('issue')).toBeInTheDocument();
  1112. expect(screen.queryByText('assignee')).not.toBeInTheDocument();
  1113. expect(screen.queryByText('title')).not.toBeInTheDocument();
  1114. expect(screen.queryAllByRole('button', {name: 'Remove column'}).length).toEqual(0);
  1115. expect(screen.queryAllByRole('button', {name: 'Drag to reorder'}).length).toEqual(
  1116. 0
  1117. );
  1118. wrapper.unmount();
  1119. });
  1120. });
  1121. // Disabling since the modal is going to be removed soon
  1122. // eslint-disable-next-line jest/no-disabled-tests
  1123. describe.skip('Metrics Widgets', function () {
  1124. it('renders the dataset selector', async function () {
  1125. initialData.organization.features = [
  1126. 'performance-view',
  1127. 'discover-query',
  1128. 'dashboards-releases',
  1129. ];
  1130. mountModalWithRtl({
  1131. initialData,
  1132. onAddWidget: () => undefined,
  1133. onUpdateWidget: () => undefined,
  1134. source: types.DashboardWidgetSource.DASHBOARDS,
  1135. });
  1136. expect(await screen.findByText('Dataset')).toBeInTheDocument();
  1137. expect(
  1138. screen.getByText('All Events (Errors and Transactions)')
  1139. ).toBeInTheDocument();
  1140. expect(
  1141. screen.getByText('Issues (States, Assignment, Time, etc.)')
  1142. ).toBeInTheDocument();
  1143. expect(screen.getByText('Health (Releases, sessions)')).toBeInTheDocument();
  1144. });
  1145. it('maintains the selected dataset when display type is changed', async function () {
  1146. initialData.organization.features = [
  1147. 'performance-view',
  1148. 'discover-query',
  1149. 'dashboards-releases',
  1150. ];
  1151. mountModalWithRtl({
  1152. initialData,
  1153. onAddWidget: () => undefined,
  1154. onUpdateWidget: () => undefined,
  1155. source: types.DashboardWidgetSource.DASHBOARDS,
  1156. });
  1157. expect(
  1158. await screen.findByRole('heading', {name: 'Add Widget'})
  1159. ).toBeInTheDocument();
  1160. expect(screen.getByLabelText(/release/i)).not.toBeChecked();
  1161. userEvent.click(screen.getByLabelText(/release/i));
  1162. await waitFor(() => expect(screen.getByLabelText(/release/i)).toBeChecked());
  1163. userEvent.click(screen.getByText('Table'));
  1164. userEvent.click(screen.getByText('Line Chart'));
  1165. await waitFor(() => expect(screen.getByLabelText(/release/i)).toBeChecked());
  1166. });
  1167. it('displays metrics tags', async function () {
  1168. initialData.organization.features = [
  1169. 'performance-view',
  1170. 'discover-query',
  1171. 'dashboards-releases',
  1172. ];
  1173. mountModalWithRtl({
  1174. initialData,
  1175. onAddWidget: () => undefined,
  1176. onUpdateWidget: () => undefined,
  1177. source: types.DashboardWidgetSource.DASHBOARDS,
  1178. });
  1179. expect(
  1180. await screen.findByRole('heading', {name: 'Add Widget'})
  1181. ).toBeInTheDocument();
  1182. userEvent.click(screen.getByLabelText(/release/i));
  1183. expect(await screen.findByText('sum(…)')).toBeInTheDocument();
  1184. expect(screen.getByText('sentry.sessions.session')).toBeInTheDocument();
  1185. userEvent.click(screen.getByText('sum(…)'));
  1186. expect(await screen.findByText('count_unique(…)')).toBeInTheDocument();
  1187. expect(screen.getByText('release')).toBeInTheDocument();
  1188. expect(screen.getByText('environment')).toBeInTheDocument();
  1189. expect(screen.getByText('session.status')).toBeInTheDocument();
  1190. userEvent.click(screen.getByText('count_unique(…)'));
  1191. expect(await screen.findByText('sentry.sessions.user')).toBeInTheDocument();
  1192. });
  1193. it('displays the correct options for area chart', async function () {
  1194. initialData.organization.features = [
  1195. 'performance-view',
  1196. 'discover-query',
  1197. 'dashboards-releases',
  1198. ];
  1199. mountModalWithRtl({
  1200. initialData,
  1201. onAddWidget: () => undefined,
  1202. onUpdateWidget: () => undefined,
  1203. source: types.DashboardWidgetSource.DASHBOARDS,
  1204. });
  1205. expect(
  1206. await screen.findByRole('heading', {name: 'Add Widget'})
  1207. ).toBeInTheDocument();
  1208. userEvent.click(screen.getByLabelText(/release/i));
  1209. userEvent.click(screen.getByText('Table'));
  1210. userEvent.click(screen.getByText('Line Chart'));
  1211. expect(await screen.findByText('sum(…)')).toBeInTheDocument();
  1212. expect(screen.getByText('sentry.sessions.session')).toBeInTheDocument();
  1213. userEvent.click(screen.getByText('sum(…)'));
  1214. expect(await screen.findByText('count_unique(…)')).toBeInTheDocument();
  1215. userEvent.click(screen.getByText('count_unique(…)'));
  1216. expect(await screen.findByText('sentry.sessions.user')).toBeInTheDocument();
  1217. });
  1218. it('makes the appropriate metrics call', async function () {
  1219. initialData.organization.features = [
  1220. 'performance-view',
  1221. 'discover-query',
  1222. 'dashboards-releases',
  1223. ];
  1224. mountModalWithRtl({
  1225. initialData,
  1226. onAddWidget: () => undefined,
  1227. onUpdateWidget: () => undefined,
  1228. source: types.DashboardWidgetSource.DASHBOARDS,
  1229. });
  1230. expect(
  1231. await screen.findByRole('heading', {name: 'Add Widget'})
  1232. ).toBeInTheDocument();
  1233. userEvent.click(screen.getByLabelText(/release/i));
  1234. userEvent.click(screen.getByText('Table'));
  1235. userEvent.click(screen.getByText('Line Chart'));
  1236. await waitFor(() =>
  1237. expect(metricsDataMock).toHaveBeenLastCalledWith(
  1238. `/organizations/org-slug/metrics/data/`,
  1239. expect.objectContaining({
  1240. query: {
  1241. environment: [],
  1242. field: ['sum(sentry.sessions.session)'],
  1243. groupBy: [],
  1244. interval: '30m',
  1245. project: [],
  1246. statsPeriod: '14d',
  1247. per_page: 20,
  1248. orderBy: 'sum(sentry.sessions.session)',
  1249. },
  1250. })
  1251. )
  1252. );
  1253. });
  1254. it('can select derived metric fields', async function () {
  1255. initialData.organization.features = [
  1256. 'performance-view',
  1257. 'discover-query',
  1258. 'dashboards-releases',
  1259. ];
  1260. const addWidgetMock = jest.fn();
  1261. mountModalWithRtl({
  1262. initialData,
  1263. onAddWidget: addWidgetMock,
  1264. onUpdateWidget: addWidgetMock,
  1265. source: types.DashboardWidgetSource.DASHBOARDS,
  1266. });
  1267. expect(
  1268. await screen.findByRole('heading', {name: 'Add Widget'})
  1269. ).toBeInTheDocument();
  1270. userEvent.click(screen.getByLabelText(/release/i));
  1271. await selectEvent.select(screen.getByText('sum(…)'), /session.crash_free_rate/);
  1272. expect(screen.getByText('session.crash_free_rate()')).toBeInTheDocument();
  1273. expect(screen.getByText('f(x)')).toBeInTheDocument();
  1274. expect(metricsDataMock).toHaveBeenLastCalledWith(
  1275. `/organizations/org-slug/metrics/data/`,
  1276. expect.objectContaining({
  1277. query: {
  1278. environment: [],
  1279. field: ['session.crash_free_rate'],
  1280. groupBy: [],
  1281. interval: '30m',
  1282. orderBy: 'session.crash_free_rate',
  1283. per_page: 5,
  1284. project: [],
  1285. statsPeriod: '14d',
  1286. },
  1287. })
  1288. );
  1289. userEvent.click(screen.getByRole('button', {name: 'Add Widget'}));
  1290. await waitFor(() =>
  1291. expect(addWidgetMock).toHaveBeenCalledWith(
  1292. expect.objectContaining({
  1293. displayType: 'table',
  1294. interval: '5m',
  1295. queries: [
  1296. {
  1297. aggregates: ['calculated|session.crash_free_rate'],
  1298. columns: [],
  1299. conditions: '',
  1300. fields: ['calculated|session.crash_free_rate'],
  1301. name: '',
  1302. orderby: '',
  1303. },
  1304. ],
  1305. title: '',
  1306. widgetType: 'metrics',
  1307. })
  1308. )
  1309. );
  1310. });
  1311. it('displays no metrics message', async function () {
  1312. // ensure that we have no metrics fields
  1313. MockApiClient.addMockResponse({
  1314. url: `/organizations/org-slug/metrics/meta/`,
  1315. body: [],
  1316. });
  1317. initialData.organization.features = [
  1318. 'performance-view',
  1319. 'discover-query',
  1320. 'dashboards-releases',
  1321. ];
  1322. mountModalWithRtl({
  1323. initialData,
  1324. onAddWidget: () => undefined,
  1325. onUpdateWidget: () => undefined,
  1326. source: types.DashboardWidgetSource.DASHBOARDS,
  1327. });
  1328. expect(
  1329. await screen.findByRole('heading', {name: 'Add Widget'})
  1330. ).toBeInTheDocument();
  1331. // change dataset to metrics
  1332. userEvent.click(screen.getByLabelText(/release/i));
  1333. // open visualization select
  1334. userEvent.click(screen.getByText('Table'));
  1335. // choose line chart
  1336. userEvent.click(screen.getByText('Line Chart'));
  1337. // open fields select
  1338. userEvent.click(screen.getByText(/required/i));
  1339. // there's correct empty message
  1340. expect(await screen.findByText(/no metrics/i)).toBeInTheDocument();
  1341. });
  1342. it('metrics do not have equation', async function () {
  1343. mountModalWithRtl({
  1344. initialData,
  1345. widget: {
  1346. displayType: 'table',
  1347. widgetType: 'metrics',
  1348. queries: [
  1349. {
  1350. id: '9',
  1351. name: 'errors',
  1352. conditions: 'event.type:error',
  1353. fields: ['sdk.name', 'count()'],
  1354. columns: ['sdk.name'],
  1355. aggregates: ['count()'],
  1356. orderby: '',
  1357. },
  1358. ],
  1359. },
  1360. });
  1361. // Select line chart display
  1362. userEvent.click(await screen.findByText('Table'));
  1363. userEvent.click(screen.getByText('Line Chart'));
  1364. await waitFor(() =>
  1365. expect(screen.queryByLabelText('Add an Equation')).not.toBeInTheDocument()
  1366. );
  1367. });
  1368. });
  1369. });