addDashboardWidgetModal.spec.jsx 50 KB

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