widgetBuilder.spec.tsx 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679
  1. import {urlEncode} from '@sentry/utils';
  2. import {DashboardFixture} from 'sentry-fixture/dashboard';
  3. import {LocationFixture} from 'sentry-fixture/locationFixture';
  4. import {MetricsFieldFixture} from 'sentry-fixture/metrics';
  5. import {ReleaseFixture} from 'sentry-fixture/release';
  6. import {SessionsFieldFixture} from 'sentry-fixture/sessions';
  7. import {TagsFixture} from 'sentry-fixture/tags';
  8. import {WidgetFixture} from 'sentry-fixture/widget';
  9. import {initializeOrg} from 'sentry-test/initializeOrg';
  10. import {
  11. act,
  12. render,
  13. renderGlobalModal,
  14. screen,
  15. userEvent,
  16. waitFor,
  17. } from 'sentry-test/reactTestingLibrary';
  18. import selectEvent from 'sentry-test/selectEvent';
  19. import * as modals from 'sentry/actionCreators/modal';
  20. import ProjectsStore from 'sentry/stores/projectsStore';
  21. import TagStore from 'sentry/stores/tagStore';
  22. import {DatasetSource, TOP_N} from 'sentry/utils/discover/types';
  23. import type {DashboardDetails, Widget} from 'sentry/views/dashboards/types';
  24. import {
  25. DashboardWidgetSource,
  26. DisplayType,
  27. WidgetType,
  28. } from 'sentry/views/dashboards/types';
  29. import type {WidgetBuilderProps} from 'sentry/views/dashboards/widgetBuilder';
  30. import WidgetBuilder from 'sentry/views/dashboards/widgetBuilder';
  31. import WidgetLegendSelectionState from '../widgetLegendSelectionState';
  32. const defaultOrgFeatures = [
  33. 'performance-view',
  34. 'dashboards-edit',
  35. 'global-views',
  36. 'dashboards-mep',
  37. ];
  38. function mockDashboard(dashboard: Partial<DashboardDetails>): DashboardDetails {
  39. return {
  40. id: '1',
  41. title: 'Dashboard',
  42. createdBy: undefined,
  43. dateCreated: '2020-01-01T00:00:00.000Z',
  44. widgets: [],
  45. projects: [],
  46. filters: {},
  47. ...dashboard,
  48. };
  49. }
  50. function renderTestComponent({
  51. dashboard,
  52. query,
  53. orgFeatures,
  54. onSave,
  55. params,
  56. updateDashboardSplitDecision,
  57. }: {
  58. dashboard?: WidgetBuilderProps['dashboard'];
  59. onSave?: WidgetBuilderProps['onSave'];
  60. orgFeatures?: string[];
  61. params?: Partial<WidgetBuilderProps['params']>;
  62. query?: Record<string, any>;
  63. updateDashboardSplitDecision?: WidgetBuilderProps['updateDashboardSplitDecision'];
  64. } = {}) {
  65. const {organization, projects, router} = initializeOrg({
  66. organization: {
  67. features: orgFeatures ?? defaultOrgFeatures,
  68. },
  69. router: {
  70. location: {
  71. query: {
  72. source: DashboardWidgetSource.DASHBOARDS,
  73. ...query,
  74. },
  75. },
  76. },
  77. });
  78. ProjectsStore.loadInitialData(projects);
  79. const widgetLegendState = new WidgetLegendSelectionState({
  80. location: LocationFixture(),
  81. dashboard: DashboardFixture([], {id: 'new', title: 'Dashboard', ...dashboard}),
  82. organization,
  83. router,
  84. });
  85. render(
  86. <WidgetBuilder
  87. route={{}}
  88. router={router}
  89. routes={router.routes}
  90. routeParams={router.params}
  91. location={router.location}
  92. dashboard={{
  93. id: 'new',
  94. title: 'Dashboard',
  95. createdBy: undefined,
  96. dateCreated: '2020-01-01T00:00:00.000Z',
  97. widgets: [],
  98. projects: [],
  99. filters: {},
  100. ...dashboard,
  101. }}
  102. onSave={onSave ?? jest.fn()}
  103. params={{
  104. orgId: organization.slug,
  105. dashboardId: dashboard?.id ?? 'new',
  106. ...params,
  107. }}
  108. updateDashboardSplitDecision={updateDashboardSplitDecision}
  109. widgetLegendState={widgetLegendState}
  110. />,
  111. {
  112. router,
  113. organization,
  114. }
  115. );
  116. return {router};
  117. }
  118. /**
  119. * This test suite contains tests that test the generic interactions
  120. * between most components in the WidgetBuilder. Tests for the
  121. * SortBy step can be found in (and should be added to)
  122. * ./widgetBuilderSortBy.spec.tsx and tests for specific dataset
  123. * behaviour can be found (and should be added to) ./widgetBuilderDataset.spec.tsx.
  124. * The test files are broken up to allow better parallelization
  125. * in CI (we currently parallelize files).
  126. */
  127. describe('WidgetBuilder', function () {
  128. const untitledDashboard: DashboardDetails = {
  129. id: '1',
  130. title: 'Untitled Dashboard',
  131. createdBy: undefined,
  132. dateCreated: '2020-01-01T00:00:00.000Z',
  133. widgets: [],
  134. projects: [],
  135. filters: {},
  136. };
  137. const testDashboard: DashboardDetails = {
  138. id: '2',
  139. title: 'Test Dashboard',
  140. createdBy: undefined,
  141. dateCreated: '2020-01-01T00:00:00.000Z',
  142. widgets: [],
  143. projects: [],
  144. filters: {},
  145. };
  146. let eventsStatsMock: jest.Mock | undefined;
  147. let eventsMock: jest.Mock | undefined;
  148. let tagsMock: jest.Mock | undefined;
  149. beforeEach(function () {
  150. MockApiClient.addMockResponse({
  151. url: '/organizations/org-slug/dashboards/',
  152. body: [
  153. {...untitledDashboard, widgetDisplay: [DisplayType.TABLE]},
  154. {...testDashboard, widgetDisplay: [DisplayType.AREA]},
  155. ],
  156. });
  157. MockApiClient.addMockResponse({
  158. url: '/organizations/org-slug/dashboards/widgets/',
  159. method: 'POST',
  160. statusCode: 200,
  161. body: [],
  162. });
  163. eventsMock = MockApiClient.addMockResponse({
  164. url: '/organizations/org-slug/events/',
  165. method: 'GET',
  166. statusCode: 200,
  167. body: {
  168. meta: {fields: {}},
  169. data: [],
  170. },
  171. });
  172. MockApiClient.addMockResponse({
  173. url: '/organizations/org-slug/projects/',
  174. method: 'GET',
  175. body: [],
  176. });
  177. MockApiClient.addMockResponse({
  178. url: '/organizations/org-slug/recent-searches/',
  179. method: 'GET',
  180. body: [],
  181. });
  182. MockApiClient.addMockResponse({
  183. url: '/organizations/org-slug/recent-searches/',
  184. method: 'POST',
  185. body: [],
  186. });
  187. MockApiClient.addMockResponse({
  188. url: '/organizations/org-slug/issues/',
  189. method: 'GET',
  190. body: [],
  191. });
  192. eventsStatsMock = MockApiClient.addMockResponse({
  193. url: '/organizations/org-slug/events-stats/',
  194. body: [],
  195. });
  196. MockApiClient.addMockResponse({
  197. url: '/organizations/org-slug/tags/event.type/values/',
  198. body: [{count: 2, name: 'Nvidia 1080ti'}],
  199. });
  200. MockApiClient.addMockResponse({
  201. url: '/organizations/org-slug/users/',
  202. body: [],
  203. });
  204. MockApiClient.addMockResponse({
  205. method: 'GET',
  206. url: '/organizations/org-slug/sessions/',
  207. body: SessionsFieldFixture(`sum(session)`),
  208. });
  209. MockApiClient.addMockResponse({
  210. method: 'GET',
  211. url: '/organizations/org-slug/metrics/data/',
  212. body: MetricsFieldFixture('session.all'),
  213. });
  214. tagsMock = MockApiClient.addMockResponse({
  215. url: '/organizations/org-slug/tags/',
  216. method: 'GET',
  217. body: TagsFixture(),
  218. });
  219. MockApiClient.addMockResponse({
  220. url: '/organizations/org-slug/measurements-meta/',
  221. method: 'GET',
  222. body: {},
  223. });
  224. MockApiClient.addMockResponse({
  225. url: '/organizations/org-slug/tags/is/values/',
  226. method: 'GET',
  227. body: [],
  228. });
  229. MockApiClient.addMockResponse({
  230. url: '/organizations/org-slug/tags/transaction.duration/values/',
  231. method: 'GET',
  232. body: [],
  233. });
  234. MockApiClient.addMockResponse({
  235. url: '/organizations/org-slug/releases/',
  236. body: [],
  237. });
  238. MockApiClient.addMockResponse({
  239. url: `/organizations/org-slug/spans/fields/`,
  240. body: [],
  241. });
  242. TagStore.reset();
  243. });
  244. afterEach(function () {
  245. MockApiClient.clearMockResponses();
  246. jest.clearAllMocks();
  247. });
  248. it('no feature access', function () {
  249. renderTestComponent({orgFeatures: []});
  250. expect(screen.getByText("You don't have access to this feature")).toBeInTheDocument();
  251. });
  252. it('widget not found', async function () {
  253. const widget: Widget = {
  254. displayType: DisplayType.AREA,
  255. interval: '1d',
  256. queries: [
  257. {
  258. name: 'Known Users',
  259. fields: [],
  260. columns: [],
  261. aggregates: [],
  262. conditions: '',
  263. orderby: '-time',
  264. },
  265. {
  266. name: 'Anonymous Users',
  267. fields: [],
  268. columns: [],
  269. aggregates: [],
  270. conditions: '',
  271. orderby: '-time',
  272. },
  273. ],
  274. title: 'Transactions',
  275. id: '1',
  276. };
  277. const dashboard = mockDashboard({widgets: [widget]});
  278. renderTestComponent({
  279. dashboard,
  280. orgFeatures: ['dashboards-edit'],
  281. params: {
  282. widgetIndex: '2', // Out of bounds, only one widget
  283. },
  284. });
  285. expect(
  286. await screen.findByText('The widget you want to edit was not found.')
  287. ).toBeInTheDocument();
  288. });
  289. it('renders a widget not found message if the widget index url is not an integer', async function () {
  290. const widget: Widget = {
  291. displayType: DisplayType.AREA,
  292. interval: '1d',
  293. queries: [
  294. {
  295. name: 'Known Users',
  296. fields: [],
  297. columns: [],
  298. aggregates: [],
  299. conditions: '',
  300. orderby: '-time',
  301. },
  302. ],
  303. title: 'Transactions',
  304. id: '1',
  305. };
  306. const dashboard = mockDashboard({widgets: [widget]});
  307. renderTestComponent({
  308. dashboard,
  309. orgFeatures: ['dashboards-edit'],
  310. params: {
  311. widgetIndex: '0.5', // Invalid index
  312. },
  313. });
  314. expect(
  315. await screen.findByText('The widget you want to edit was not found.')
  316. ).toBeInTheDocument();
  317. });
  318. it('renders', async function () {
  319. renderTestComponent();
  320. // Header - Breadcrumbs
  321. expect(await screen.findByRole('link', {name: 'Dashboards'})).toHaveAttribute(
  322. 'href',
  323. '/organizations/org-slug/dashboards/'
  324. );
  325. expect(screen.getByRole('link', {name: 'Dashboard'})).toHaveAttribute(
  326. 'href',
  327. '/organizations/org-slug/dashboards/new/'
  328. );
  329. expect(screen.getByText('Widget Builder')).toBeInTheDocument();
  330. // Header - Widget Title
  331. expect(screen.getByText('Custom Widget')).toBeInTheDocument();
  332. // Footer - Actions
  333. expect(screen.getByLabelText('Cancel')).toBeInTheDocument();
  334. expect(screen.getByLabelText('Add Widget')).toBeInTheDocument();
  335. // Content - Step 1
  336. expect(
  337. screen.getByRole('heading', {name: 'Choose your dataset'})
  338. ).toBeInTheDocument();
  339. expect(screen.getByLabelText('Errors and Transactions')).toBeChecked();
  340. // Content - Step 2
  341. expect(
  342. screen.getByRole('heading', {name: 'Choose your visualization'})
  343. ).toBeInTheDocument();
  344. // Content - Step 3
  345. expect(
  346. screen.getByRole('heading', {name: 'Choose your columns'})
  347. ).toBeInTheDocument();
  348. // Content - Step 4
  349. expect(
  350. screen.getByRole('heading', {name: 'Filter your results'})
  351. ).toBeInTheDocument();
  352. // Content - Step 5
  353. expect(screen.getByRole('heading', {name: 'Sort by a column'})).toBeInTheDocument();
  354. });
  355. it('has links back to the new dashboard if creating', async function () {
  356. // Dashboard has undefined dashboardId when creating from a new dashboard
  357. // because of route setup
  358. renderTestComponent({params: {dashboardId: undefined}});
  359. expect(await screen.findByLabelText('Cancel')).toHaveAttribute(
  360. 'href',
  361. '/organizations/org-slug/dashboards/new/'
  362. );
  363. });
  364. it('renders new design', async function () {
  365. renderTestComponent({
  366. orgFeatures: [...defaultOrgFeatures],
  367. });
  368. // Switch to line chart for time series
  369. await userEvent.click(screen.getByText('Table'));
  370. await userEvent.click(screen.getByText('Line Chart'));
  371. // Header - Breadcrumbs
  372. expect(await screen.findByRole('link', {name: 'Dashboards'})).toHaveAttribute(
  373. 'href',
  374. '/organizations/org-slug/dashboards/'
  375. );
  376. expect(screen.getByRole('link', {name: 'Dashboard'})).toHaveAttribute(
  377. 'href',
  378. '/organizations/org-slug/dashboards/new/'
  379. );
  380. expect(screen.getByText('Widget Builder')).toBeInTheDocument();
  381. // Header - Widget Title
  382. expect(screen.getByText('Custom Widget')).toBeInTheDocument();
  383. // Footer - Actions
  384. expect(screen.getByLabelText('Cancel')).toBeInTheDocument();
  385. expect(screen.getByLabelText('Add Widget')).toBeInTheDocument();
  386. // Content - Step 1
  387. expect(
  388. screen.getByRole('heading', {name: 'Choose your dataset'})
  389. ).toBeInTheDocument();
  390. expect(screen.getByLabelText('Errors and Transactions')).toBeChecked();
  391. // Content - Step 2
  392. expect(
  393. screen.getByRole('heading', {name: 'Choose your visualization'})
  394. ).toBeInTheDocument();
  395. // Content - Step 3
  396. expect(
  397. screen.getByRole('heading', {name: 'Choose what to plot in the y-axis'})
  398. ).toBeInTheDocument();
  399. // Content - Step 4
  400. expect(
  401. screen.getByRole('heading', {name: 'Filter your results'})
  402. ).toBeInTheDocument();
  403. // Content - Step 5
  404. expect(screen.getByRole('heading', {name: 'Group your results'})).toBeInTheDocument();
  405. });
  406. it('can update the title', async function () {
  407. renderTestComponent({
  408. query: {source: DashboardWidgetSource.DISCOVERV2},
  409. });
  410. const customWidgetLabels = await screen.findByText('Custom Widget');
  411. // EditableText and chart title
  412. expect(customWidgetLabels).toBeInTheDocument();
  413. await userEvent.clear(screen.getByRole('textbox', {name: 'Widget title'}));
  414. await userEvent.click(screen.getByRole('textbox', {name: 'Widget title'}));
  415. await userEvent.paste('Unique Users');
  416. await userEvent.keyboard('{enter}');
  417. expect(screen.queryByText('Custom Widget')).not.toBeInTheDocument();
  418. expect(screen.getByText('Unique Users')).toBeInTheDocument();
  419. });
  420. it('can add query conditions', async function () {
  421. const {router} = renderTestComponent({
  422. query: {source: DashboardWidgetSource.DISCOVERV2},
  423. dashboard: testDashboard,
  424. });
  425. await userEvent.click(
  426. await screen.findByRole('combobox', {name: 'Add a search term'})
  427. );
  428. await userEvent.paste('color:blue');
  429. await userEvent.keyboard('{enter}');
  430. await userEvent.click(screen.getByText('Add Widget'));
  431. await waitFor(() => {
  432. expect(router.push).toHaveBeenCalledWith(
  433. expect.objectContaining({
  434. pathname: '/organizations/org-slug/dashboard/2/',
  435. query: {
  436. displayType: 'table',
  437. interval: '5m',
  438. title: 'Custom Widget',
  439. queryNames: [''],
  440. queryConditions: ['color:blue'],
  441. queryFields: ['count()'],
  442. queryOrderby: '-count()',
  443. start: null,
  444. end: null,
  445. statsPeriod: '24h',
  446. utc: null,
  447. project: [],
  448. environment: [],
  449. widgetType: 'discover',
  450. },
  451. })
  452. );
  453. });
  454. });
  455. it('can choose a field', async function () {
  456. const {router} = renderTestComponent({
  457. query: {source: DashboardWidgetSource.DISCOVERV2},
  458. dashboard: testDashboard,
  459. });
  460. expect(await screen.findByText('Custom Widget')).toBeInTheDocument();
  461. // No delete button as there is only one query.
  462. expect(screen.queryByLabelText('Remove query')).not.toBeInTheDocument();
  463. // 1 in the table header, 1 in the column selector, 1 in the sort field
  464. const countFields = screen.getAllByText('count()');
  465. expect(countFields).toHaveLength(3);
  466. await selectEvent.select(countFields[1], ['last_seen()']);
  467. await userEvent.click(screen.getByText('Add Widget'));
  468. await waitFor(() => {
  469. expect(router.push).toHaveBeenCalledWith(
  470. expect.objectContaining({
  471. pathname: '/organizations/org-slug/dashboard/2/',
  472. query: {
  473. displayType: 'table',
  474. interval: '5m',
  475. title: 'Custom Widget',
  476. queryNames: [''],
  477. queryConditions: [''],
  478. queryFields: ['last_seen()'],
  479. queryOrderby: '-last_seen()',
  480. start: null,
  481. end: null,
  482. statsPeriod: '24h',
  483. utc: null,
  484. project: [],
  485. environment: [],
  486. widgetType: 'discover',
  487. },
  488. })
  489. );
  490. });
  491. });
  492. it('can add additional fields', async function () {
  493. const handleSave = jest.fn();
  494. renderTestComponent({onSave: handleSave});
  495. await userEvent.click(await screen.findByText('Table'));
  496. // Select line chart display
  497. await userEvent.click(screen.getByText('Line Chart'));
  498. // Click the add overlay button
  499. await userEvent.click(screen.getByLabelText('Add Overlay'));
  500. await selectEvent.select(screen.getByText('(Required)'), ['count_unique(…)']);
  501. await userEvent.click(screen.getByLabelText('Add Widget'));
  502. await waitFor(() => {
  503. expect(handleSave).toHaveBeenCalledWith([
  504. expect.objectContaining({
  505. title: 'Custom Widget',
  506. displayType: DisplayType.LINE,
  507. interval: '5m',
  508. widgetType: WidgetType.DISCOVER,
  509. queries: [
  510. {
  511. conditions: '',
  512. fields: ['count()', 'count_unique(user)'],
  513. aggregates: ['count()', 'count_unique(user)'],
  514. fieldAliases: [],
  515. columns: [],
  516. orderby: '',
  517. name: '',
  518. },
  519. ],
  520. }),
  521. ]);
  522. });
  523. expect(handleSave).toHaveBeenCalledTimes(1);
  524. });
  525. it('can add additional fields and equation for Big Number with selection', async function () {
  526. renderTestComponent({
  527. query: {
  528. displayType: DisplayType.BIG_NUMBER,
  529. },
  530. orgFeatures: [...defaultOrgFeatures, 'dashboards-bignumber-equations'],
  531. });
  532. // Add new field
  533. await userEvent.click(screen.getByLabelText('Add Field'));
  534. expect(screen.getByText('(Required)')).toBeInTheDocument();
  535. await selectEvent.select(screen.getByText('(Required)'), ['count_unique(…)']);
  536. expect(screen.getByRole('radio', {name: 'field1'})).toBeChecked();
  537. // Add another new field
  538. await userEvent.click(screen.getByLabelText('Add Field'));
  539. expect(screen.getByText('(Required)')).toBeInTheDocument();
  540. await selectEvent.select(screen.getByText('(Required)'), ['eps()']);
  541. expect(screen.getByRole('radio', {name: 'field2'})).toBeChecked();
  542. // Add an equation
  543. await userEvent.click(screen.getByLabelText('Add an Equation'));
  544. expect(screen.getByPlaceholderText('Equation')).toBeInTheDocument();
  545. expect(screen.getByRole('radio', {name: 'field3'})).toBeChecked();
  546. await userEvent.click(screen.getByPlaceholderText('Equation'));
  547. await userEvent.paste('eps() + 100');
  548. // Check if right value is displayed from equation
  549. await userEvent.click(screen.getByPlaceholderText('Equation'));
  550. await userEvent.paste('2 * 100');
  551. expect(screen.getByText('200')).toBeInTheDocument();
  552. });
  553. it('can add equation fields', async function () {
  554. const handleSave = jest.fn();
  555. renderTestComponent({onSave: handleSave});
  556. await userEvent.click(await screen.findByText('Table'));
  557. // Select line chart display
  558. await userEvent.click(screen.getByText('Line Chart'));
  559. // Click the add an equation button
  560. await userEvent.click(screen.getByLabelText('Add an Equation'));
  561. expect(screen.getByPlaceholderText('Equation')).toBeInTheDocument();
  562. await userEvent.click(screen.getByPlaceholderText('Equation'));
  563. await userEvent.paste('count() + 100');
  564. await userEvent.click(screen.getByLabelText('Add Widget'));
  565. await waitFor(() => {
  566. expect(handleSave).toHaveBeenCalledWith([
  567. expect.objectContaining({
  568. title: 'Custom Widget',
  569. displayType: DisplayType.LINE,
  570. interval: '5m',
  571. widgetType: WidgetType.DISCOVER,
  572. queries: [
  573. {
  574. name: '',
  575. fields: ['count()', 'equation|count() + 100'],
  576. aggregates: ['count()', 'equation|count() + 100'],
  577. columns: [],
  578. fieldAliases: [],
  579. conditions: '',
  580. orderby: '',
  581. },
  582. ],
  583. }),
  584. ]);
  585. });
  586. expect(handleSave).toHaveBeenCalledTimes(1);
  587. });
  588. it('can respond to validation feedback', async function () {
  589. MockApiClient.addMockResponse({
  590. url: '/organizations/org-slug/dashboards/widgets/',
  591. method: 'POST',
  592. statusCode: 400,
  593. body: {
  594. title: ['This field may not be blank.'],
  595. },
  596. });
  597. renderTestComponent();
  598. await userEvent.click(await screen.findByText('Table'));
  599. const customWidgetLabels = await screen.findByText('Custom Widget');
  600. // EditableText and chart title
  601. expect(customWidgetLabels).toBeInTheDocument();
  602. await userEvent.clear(screen.getByRole('textbox', {name: 'Widget title'}));
  603. await userEvent.click(screen.getByText('Add Widget'));
  604. await screen.findByText('This field may not be blank.');
  605. });
  606. it('sets up widget data in edit correctly', async function () {
  607. const widget: Widget = {
  608. id: '1',
  609. title: 'Errors over time',
  610. interval: '5m',
  611. displayType: DisplayType.LINE,
  612. queries: [
  613. {
  614. name: 'errors',
  615. conditions: 'event.type:error',
  616. fields: ['count()', 'count_unique(id)'],
  617. aggregates: ['count()', 'count_unique(id)'],
  618. columns: [],
  619. orderby: '',
  620. },
  621. {
  622. name: 'csp',
  623. conditions: 'event.type:csp',
  624. fields: ['count()', 'count_unique(id)'],
  625. aggregates: ['count()', 'count_unique(id)'],
  626. columns: [],
  627. orderby: '',
  628. },
  629. ],
  630. };
  631. const dashboard = mockDashboard({widgets: [widget]});
  632. renderTestComponent({dashboard, params: {widgetIndex: '0'}});
  633. await screen.findByText('Line Chart');
  634. // Should be in edit 'mode'
  635. expect(await screen.findByText('Update Widget')).toBeInTheDocument();
  636. // Should set widget data up.
  637. expect(screen.getByText('Update Widget')).toBeInTheDocument();
  638. // Filters
  639. expect(
  640. await screen.findAllByRole('grid', {name: 'Create a search query'})
  641. ).toHaveLength(2);
  642. expect(screen.getByRole('row', {name: 'event.type:csp'})).toBeInTheDocument();
  643. expect(screen.getByRole('row', {name: 'event.type:error'})).toBeInTheDocument();
  644. // Y-axis
  645. expect(screen.getAllByRole('button', {name: 'Remove query'})).toHaveLength(2);
  646. expect(screen.getByText('count()')).toBeInTheDocument();
  647. expect(screen.getByText('count_unique(…)')).toBeInTheDocument();
  648. expect(screen.getByText('id')).toBeInTheDocument();
  649. // Expect events-stats endpoint to be called for each search conditions with
  650. // the same y-axis parameters
  651. expect(eventsStatsMock).toHaveBeenNthCalledWith(
  652. 1,
  653. '/organizations/org-slug/events-stats/',
  654. expect.objectContaining({
  655. query: expect.objectContaining({
  656. query: 'event.type:error',
  657. yAxis: ['count()', 'count_unique(id)'],
  658. }),
  659. })
  660. );
  661. expect(eventsStatsMock).toHaveBeenNthCalledWith(
  662. 2,
  663. '/organizations/org-slug/events-stats/',
  664. expect.objectContaining({
  665. query: expect.objectContaining({
  666. query: 'event.type:csp',
  667. yAxis: ['count()', 'count_unique(id)'],
  668. }),
  669. })
  670. );
  671. });
  672. it('can edit a widget', async function () {
  673. const widget: Widget = {
  674. id: '1',
  675. title: 'Errors over time',
  676. interval: '5m',
  677. displayType: DisplayType.LINE,
  678. queries: [
  679. {
  680. name: 'errors',
  681. conditions: 'event.type:error',
  682. fields: ['count()', 'count_unique(id)'],
  683. aggregates: ['count()', 'count_unique(id)'],
  684. columns: [],
  685. orderby: '',
  686. },
  687. {
  688. name: 'csp',
  689. conditions: 'event.type:csp',
  690. fields: ['count()', 'count_unique(id)'],
  691. aggregates: ['count()', 'count_unique(id)'],
  692. columns: [],
  693. orderby: '',
  694. },
  695. ],
  696. };
  697. const dashboard = mockDashboard({widgets: [widget]});
  698. const handleSave = jest.fn();
  699. renderTestComponent({onSave: handleSave, dashboard, params: {widgetIndex: '0'}});
  700. await screen.findByText('Line Chart');
  701. // Should be in edit 'mode'
  702. expect(screen.getByText('Update Widget')).toBeInTheDocument();
  703. const customWidgetLabels = screen.getByText(widget.title);
  704. // EditableText and chart title
  705. expect(customWidgetLabels).toBeInTheDocument();
  706. await userEvent.clear(screen.getByRole('textbox', {name: 'Widget title'}));
  707. await userEvent.click(screen.getByRole('textbox', {name: 'Widget title'}));
  708. await userEvent.paste('New Title');
  709. await userEvent.click(screen.getByRole('button', {name: 'Update Widget'}));
  710. await waitFor(() => {
  711. expect(handleSave).toHaveBeenCalledWith([
  712. expect.objectContaining({
  713. ...widget,
  714. title: 'New Title',
  715. }),
  716. ]);
  717. });
  718. expect(handleSave).toHaveBeenCalledTimes(1);
  719. });
  720. it('renders column inputs for table widgets', async function () {
  721. const widget: Widget = {
  722. id: '0',
  723. title: 'sdk usage',
  724. interval: '5m',
  725. displayType: DisplayType.TABLE,
  726. queries: [
  727. {
  728. name: 'errors',
  729. conditions: 'event.type:error',
  730. fields: ['sdk.name', 'count()'],
  731. columns: ['sdk.name'],
  732. aggregates: ['count()'],
  733. orderby: '',
  734. },
  735. ],
  736. };
  737. const dashboard = mockDashboard({widgets: [widget]});
  738. renderTestComponent({dashboard, params: {widgetIndex: '0'}});
  739. // Should be in edit 'mode'
  740. expect(await screen.findByText('Update Widget')).toBeInTheDocument();
  741. // Should set widget data up.
  742. expect(screen.getByText(widget.title)).toBeInTheDocument();
  743. expect(screen.getByText('Table')).toBeInTheDocument();
  744. await screen.findByRole('grid', {name: 'Create a search query'});
  745. // Should have an orderby select
  746. expect(screen.getByText('Sort by a column')).toBeInTheDocument();
  747. // Add a column, and choose a value,
  748. expect(screen.getByLabelText('Add a Column')).toBeInTheDocument();
  749. });
  750. it('can save table widgets', async function () {
  751. const widget: Widget = {
  752. id: '0',
  753. title: 'sdk usage',
  754. interval: '5m',
  755. displayType: DisplayType.TABLE,
  756. queries: [
  757. {
  758. name: 'errors',
  759. conditions: 'event.type:error',
  760. fields: ['sdk.name', 'count()'],
  761. columns: ['sdk.name'],
  762. aggregates: ['count()'],
  763. orderby: '-count()',
  764. },
  765. ],
  766. };
  767. const dashboard = mockDashboard({widgets: [widget]});
  768. const handleSave = jest.fn();
  769. renderTestComponent({dashboard, onSave: handleSave, params: {widgetIndex: '0'}});
  770. // Should be in edit 'mode'
  771. expect(await screen.findByText('Update Widget')).toBeInTheDocument();
  772. // Add a column, and choose a value,
  773. await userEvent.click(screen.getByLabelText('Add a Column'));
  774. await selectEvent.select(screen.getByText('(Required)'), 'trace');
  775. // Save widget
  776. await userEvent.click(screen.getByLabelText('Update Widget'));
  777. await waitFor(() => {
  778. expect(handleSave).toHaveBeenCalledWith([
  779. expect.objectContaining({
  780. id: '0',
  781. title: 'sdk usage',
  782. displayType: DisplayType.TABLE,
  783. interval: '5m',
  784. queries: [
  785. {
  786. name: 'errors',
  787. conditions: 'event.type:error',
  788. fields: ['sdk.name', 'count()', 'trace'],
  789. aggregates: ['count()'],
  790. columns: ['sdk.name', 'trace'],
  791. orderby: '-count()',
  792. fieldAliases: ['', '', ''],
  793. },
  794. ],
  795. widgetType: WidgetType.DISCOVER,
  796. }),
  797. ]);
  798. });
  799. expect(handleSave).toHaveBeenCalledTimes(1);
  800. });
  801. it('should properly query for table fields', async function () {
  802. const defaultWidgetQuery = {
  803. name: '',
  804. fields: ['title', 'count()'],
  805. columns: ['title'],
  806. aggregates: ['count()'],
  807. conditions: '',
  808. orderby: '',
  809. };
  810. const defaultTableColumns = ['title', 'count()', 'count_unique(user)', 'epm()'];
  811. renderTestComponent({
  812. query: {
  813. source: DashboardWidgetSource.DISCOVERV2,
  814. defaultWidgetQuery: urlEncode(defaultWidgetQuery),
  815. displayType: DisplayType.LINE,
  816. defaultTableColumns,
  817. },
  818. });
  819. expect(await screen.findByText('Line Chart')).toBeInTheDocument();
  820. await userEvent.click(screen.getByText('Line Chart'));
  821. await userEvent.click(screen.getByText('Table'));
  822. await waitFor(() => {
  823. expect(eventsMock).toHaveBeenLastCalledWith(
  824. '/organizations/org-slug/events/',
  825. expect.objectContaining({
  826. query: expect.objectContaining({
  827. field: defaultTableColumns,
  828. }),
  829. })
  830. );
  831. });
  832. });
  833. it('should use defaultWidgetQuery Y-Axis and Conditions if given a defaultWidgetQuery', async function () {
  834. const defaultWidgetQuery = {
  835. name: '',
  836. fields: ['count()', 'failure_count()', 'count_unique(user)'],
  837. columns: [],
  838. aggregates: ['count()', 'failure_count()', 'count_unique(user)'],
  839. conditions: 'tag:value',
  840. orderby: '',
  841. };
  842. renderTestComponent({
  843. query: {
  844. source: DashboardWidgetSource.DISCOVERV2,
  845. defaultWidgetQuery: urlEncode(defaultWidgetQuery),
  846. },
  847. });
  848. expect(await screen.findByRole('row', {name: 'tag:value'})).toBeInTheDocument();
  849. // Table display, column, and sort field
  850. await waitFor(() => {
  851. expect(screen.getAllByText('count()')).toHaveLength(3);
  852. });
  853. // Table display and column
  854. expect(screen.getAllByText('failure_count()')).toHaveLength(2);
  855. // Table display
  856. expect(screen.getByText('count_unique(user)')).toBeInTheDocument();
  857. // Column
  858. expect(screen.getByText('count_unique(…)')).toBeInTheDocument();
  859. // Column
  860. expect(screen.getByText('user')).toBeInTheDocument();
  861. });
  862. it('uses displayType if given a displayType', async function () {
  863. renderTestComponent({
  864. query: {
  865. displayType: DisplayType.BAR,
  866. },
  867. });
  868. expect(await screen.findByText('Bar Chart')).toBeInTheDocument();
  869. });
  870. it('deletes the widget when the modal is confirmed', async () => {
  871. const handleSave = jest.fn();
  872. const widget: Widget = {
  873. id: '1',
  874. title: 'Errors over time',
  875. interval: '5m',
  876. displayType: DisplayType.LINE,
  877. queries: [
  878. {
  879. name: 'errors',
  880. conditions: 'event.type:error',
  881. fields: ['count()', 'count_unique(id)'],
  882. aggregates: ['count()', 'count_unique(id)'],
  883. columns: [],
  884. orderby: '',
  885. },
  886. {
  887. name: 'csp',
  888. conditions: 'event.type:csp',
  889. fields: ['count()', 'count_unique(id)'],
  890. aggregates: ['count()', 'count_unique(id)'],
  891. columns: [],
  892. orderby: '',
  893. },
  894. ],
  895. };
  896. const dashboard = mockDashboard({widgets: [widget]});
  897. renderTestComponent({onSave: handleSave, dashboard, params: {widgetIndex: '0'}});
  898. await userEvent.click(await screen.findByText('Delete'));
  899. renderGlobalModal();
  900. await userEvent.click(await screen.findByText('Confirm'));
  901. await waitFor(() => {
  902. // The only widget was deleted
  903. expect(handleSave).toHaveBeenCalledWith([]);
  904. });
  905. expect(handleSave).toHaveBeenCalledTimes(1);
  906. });
  907. it('persists the page filter period when updating a widget', async () => {
  908. const widget: Widget = {
  909. id: '1',
  910. title: 'Errors over time',
  911. interval: '5m',
  912. displayType: DisplayType.LINE,
  913. queries: [
  914. {
  915. name: 'errors',
  916. conditions: 'event.type:error',
  917. fields: ['count()', 'count_unique(id)'],
  918. aggregates: ['count()', 'count_unique(id)'],
  919. columns: [],
  920. orderby: '',
  921. },
  922. ],
  923. };
  924. const dashboard = mockDashboard({widgets: [widget]});
  925. const {router} = renderTestComponent({
  926. dashboard,
  927. params: {orgId: 'org-slug', widgetIndex: '0'},
  928. query: {statsPeriod: '90d'},
  929. });
  930. await userEvent.click(screen.getByText('Update Widget'));
  931. await waitFor(() => {
  932. expect(router.push).toHaveBeenLastCalledWith(
  933. expect.objectContaining({
  934. pathname: '/organizations/org-slug/dashboard/1/',
  935. query: expect.objectContaining({
  936. statsPeriod: '90d',
  937. }),
  938. })
  939. );
  940. });
  941. });
  942. it('renders page filters in the filter step', async () => {
  943. const mockReleases = MockApiClient.addMockResponse({
  944. url: '/organizations/org-slug/releases/',
  945. body: [ReleaseFixture()],
  946. });
  947. renderTestComponent({
  948. params: {orgId: 'org-slug'},
  949. query: {statsPeriod: '90d'},
  950. orgFeatures: defaultOrgFeatures,
  951. });
  952. expect(await screen.findByTestId('page-filter-timerange-selector')).toBeDisabled();
  953. expect(screen.getByTestId('page-filter-environment-selector')).toBeDisabled();
  954. expect(screen.getByTestId('page-filter-project-selector')).toBeDisabled();
  955. expect(mockReleases).toHaveBeenCalled();
  956. expect(screen.getByRole('button', {name: /all releases/i})).toBeDisabled();
  957. });
  958. it('appends dashboard filters to widget builder fetch data request', async () => {
  959. MockApiClient.addMockResponse({
  960. url: '/organizations/org-slug/releases/',
  961. body: [ReleaseFixture()],
  962. });
  963. const mock = MockApiClient.addMockResponse({
  964. url: '/organizations/org-slug/events/',
  965. body: [],
  966. });
  967. renderTestComponent({
  968. dashboard: {
  969. id: 'new',
  970. title: 'Dashboard',
  971. createdBy: undefined,
  972. dateCreated: '2020-01-01T00:00:00.000Z',
  973. widgets: [],
  974. projects: [],
  975. filters: {release: ['abc@1.2.0']},
  976. },
  977. params: {orgId: 'org-slug'},
  978. query: {statsPeriod: '90d'},
  979. orgFeatures: defaultOrgFeatures,
  980. });
  981. await waitFor(() => {
  982. expect(mock).toHaveBeenCalledWith(
  983. '/organizations/org-slug/events/',
  984. expect.objectContaining({
  985. query: expect.objectContaining({
  986. query: ' release:"abc@1.2.0" ',
  987. }),
  988. })
  989. );
  990. });
  991. });
  992. it('does not error when query conditions field is blurred', async function () {
  993. const widget: Widget = {
  994. id: '0',
  995. title: 'sdk usage',
  996. interval: '5m',
  997. displayType: DisplayType.BAR,
  998. queries: [
  999. {
  1000. name: 'filled in',
  1001. conditions: 'event.type:error',
  1002. fields: ['count()', 'count_unique(id)'],
  1003. aggregates: ['count()', 'count_unique(id)'],
  1004. columns: [],
  1005. orderby: '-count()',
  1006. },
  1007. ],
  1008. };
  1009. const dashboard = mockDashboard({widgets: [widget]});
  1010. const handleSave = jest.fn();
  1011. renderTestComponent({dashboard, onSave: handleSave, params: {widgetIndex: '0'}});
  1012. await userEvent.click(await screen.findByLabelText('Add Query'), {delay: null});
  1013. // Triggering the onBlur of the new field should not error
  1014. await userEvent.click(
  1015. screen.getAllByPlaceholderText('Search for events, users, tags, and more')[1],
  1016. {delay: null}
  1017. );
  1018. await userEvent.keyboard('{Escape}', {delay: null});
  1019. // Run all timers because the handleBlur contains a setTimeout
  1020. await act(tick);
  1021. });
  1022. it('does not wipe column changes when filters are modified', async function () {
  1023. // widgetIndex: undefined means creating a new widget
  1024. renderTestComponent({params: {widgetIndex: undefined}});
  1025. await userEvent.click(await screen.findByLabelText('Add a Column'), {delay: null});
  1026. await selectEvent.select(screen.getByText('(Required)'), /project/);
  1027. // Triggering the onBlur of the filter should not error
  1028. await userEvent.click(
  1029. screen.getByPlaceholderText('Search for events, users, tags, and more'),
  1030. {delay: null}
  1031. );
  1032. await userEvent.keyboard('{enter}', {delay: null});
  1033. expect(await screen.findAllByText('project')).toHaveLength(2);
  1034. });
  1035. it('renders fields with commas properly', async () => {
  1036. const defaultWidgetQuery = {
  1037. conditions: '',
  1038. fields: ['equation|count_if(transaction.duration,equals,300)*2'],
  1039. aggregates: ['equation|count_if(transaction.duration,equals,300)*2'],
  1040. columns: [],
  1041. orderby: '',
  1042. name: '',
  1043. };
  1044. const defaultTableColumns = [
  1045. 'count_if(transaction.duration,equals,300)',
  1046. 'equation|count_if(transaction.duration,equals,300)*2',
  1047. ];
  1048. renderTestComponent({
  1049. query: {
  1050. source: DashboardWidgetSource.DISCOVERV2,
  1051. defaultWidgetQuery: urlEncode(defaultWidgetQuery),
  1052. defaultTableColumns,
  1053. yAxis: ['equation|count_if(transaction.duration,equals,300)*2'],
  1054. },
  1055. });
  1056. expect(
  1057. await screen.findByText('count_if(transaction.duration,equals,300)*2')
  1058. ).toBeInTheDocument();
  1059. });
  1060. it('sets the correct fields for a top n widget', async () => {
  1061. renderTestComponent({
  1062. orgFeatures: [...defaultOrgFeatures, 'performance-view'],
  1063. query: {
  1064. displayType: DisplayType.TOP_N,
  1065. },
  1066. });
  1067. // Top N now opens as Area Chart
  1068. expect(await screen.findByText('Area Chart')).toBeInTheDocument();
  1069. // Add a group by
  1070. await userEvent.click(screen.getByText('Add Overlay'));
  1071. await selectEvent.select(screen.getByText('Select group'), /project/);
  1072. // Change the y-axis
  1073. await selectEvent.select(screen.getAllByText('count()')[0], 'eps()');
  1074. await waitFor(() => {
  1075. expect(eventsStatsMock).toHaveBeenLastCalledWith(
  1076. '/organizations/org-slug/events-stats/',
  1077. expect.objectContaining({
  1078. query: expect.objectContaining({
  1079. query: '',
  1080. yAxis: ['eps()'],
  1081. field: ['project', 'eps()'],
  1082. topEvents: TOP_N,
  1083. orderby: '-eps()',
  1084. }),
  1085. })
  1086. );
  1087. });
  1088. });
  1089. it('fetches tags when tag store is empty', async function () {
  1090. renderTestComponent();
  1091. await waitFor(() => {
  1092. expect(tagsMock).toHaveBeenCalled();
  1093. });
  1094. });
  1095. it('does not fetch tags when tag store is not empty', async function () {
  1096. await act(async () => {
  1097. TagStore.loadTagsSuccess(TagsFixture());
  1098. renderTestComponent();
  1099. await tick();
  1100. });
  1101. expect(tagsMock).not.toHaveBeenCalled();
  1102. });
  1103. it('excludes the Other series when grouping and using multiple y-axes', async function () {
  1104. renderTestComponent({
  1105. orgFeatures: [...defaultOrgFeatures],
  1106. query: {
  1107. displayType: DisplayType.LINE,
  1108. },
  1109. });
  1110. await selectEvent.select(await screen.findByText('Select group'), 'project');
  1111. await userEvent.click(screen.getByText('Add Overlay'));
  1112. await selectEvent.select(screen.getByText('(Required)'), /count_unique/);
  1113. await waitFor(() => {
  1114. expect(eventsStatsMock).toHaveBeenCalledWith(
  1115. '/organizations/org-slug/events-stats/',
  1116. expect.objectContaining({
  1117. query: expect.objectContaining({excludeOther: '1'}),
  1118. })
  1119. );
  1120. });
  1121. });
  1122. it('excludes the Other series when grouping and using multiple queries', async function () {
  1123. renderTestComponent({
  1124. orgFeatures: [...defaultOrgFeatures],
  1125. query: {
  1126. displayType: DisplayType.LINE,
  1127. },
  1128. });
  1129. await selectEvent.select(await screen.findByText('Select group'), 'project');
  1130. await userEvent.click(screen.getByText('Add Query'));
  1131. await waitFor(() => {
  1132. expect(eventsStatsMock).toHaveBeenCalledWith(
  1133. '/organizations/org-slug/events-stats/',
  1134. expect.objectContaining({
  1135. query: expect.objectContaining({excludeOther: '1'}),
  1136. })
  1137. );
  1138. });
  1139. });
  1140. it('includes Other series when there is only one query and one y-axis', async function () {
  1141. renderTestComponent({
  1142. orgFeatures: [...defaultOrgFeatures],
  1143. query: {
  1144. displayType: DisplayType.LINE,
  1145. },
  1146. });
  1147. await selectEvent.select(await screen.findByText('Select group'), 'project');
  1148. await waitFor(() => {
  1149. expect(eventsStatsMock).toHaveBeenCalledWith(
  1150. '/organizations/org-slug/events-stats/',
  1151. expect.objectContaining({
  1152. query: expect.not.objectContaining({excludeOther: '1'}),
  1153. })
  1154. );
  1155. });
  1156. });
  1157. it('decreases the limit when more y-axes and queries are added', async function () {
  1158. renderTestComponent({
  1159. orgFeatures: [...defaultOrgFeatures],
  1160. query: {
  1161. displayType: DisplayType.LINE,
  1162. },
  1163. });
  1164. await selectEvent.select(await screen.findByText('Select group'), 'project');
  1165. screen.getByText('Limit to 5 results');
  1166. await userEvent.click(screen.getByText('Add Query'));
  1167. await userEvent.click(screen.getByText('Add Overlay'));
  1168. expect(screen.getByText('Limit to 2 results')).toBeInTheDocument();
  1169. });
  1170. it('alerts the user if there are unsaved title changes', async function () {
  1171. renderTestComponent();
  1172. window.confirm = jest.fn();
  1173. const customWidgetLabels = await screen.findByText('Custom Widget');
  1174. // EditableText and chart title
  1175. expect(customWidgetLabels).toBeInTheDocument();
  1176. // Change title text
  1177. await userEvent.clear(screen.getByRole('textbox', {name: 'Widget title'}));
  1178. await userEvent.click(screen.getByRole('textbox', {name: 'Widget title'}));
  1179. await userEvent.paste('Unique Users');
  1180. await userEvent.keyboard('{Enter}');
  1181. // Click Cancel
  1182. await userEvent.click(screen.getByText('Cancel'));
  1183. // Assert an alert was triggered
  1184. expect(window.confirm).toHaveBeenCalled();
  1185. });
  1186. it('alerts the user if there are unsaved description changes', async function () {
  1187. renderTestComponent();
  1188. window.confirm = jest.fn();
  1189. const descriptionTextArea = await screen.findByRole('textbox', {
  1190. name: 'Widget Description',
  1191. });
  1192. expect(descriptionTextArea).toBeInTheDocument();
  1193. expect(descriptionTextArea).toHaveAttribute(
  1194. 'placeholder',
  1195. 'Enter description (Optional)'
  1196. );
  1197. // Change description text
  1198. await userEvent.clear(descriptionTextArea);
  1199. await userEvent.click(descriptionTextArea);
  1200. await userEvent.paste('This is a description');
  1201. await userEvent.keyboard('{Enter}');
  1202. // Click Cancel
  1203. await userEvent.click(screen.getByText('Cancel'));
  1204. // Assert an alert was triggered
  1205. expect(window.confirm).toHaveBeenCalled();
  1206. });
  1207. it('does not trigger alert dialog if no changes', async function () {
  1208. renderTestComponent();
  1209. const alertMock = jest.spyOn(window, 'confirm');
  1210. await userEvent.click(await screen.findByText('Cancel'));
  1211. expect(alertMock).not.toHaveBeenCalled();
  1212. });
  1213. describe('Widget creation coming from other verticals', function () {
  1214. it('redirects correctly when creating a new dashboard', async function () {
  1215. const {router} = renderTestComponent({
  1216. query: {source: DashboardWidgetSource.DISCOVERV2},
  1217. });
  1218. await userEvent.click(await screen.findByText('Add Widget'));
  1219. await waitFor(() => {
  1220. expect(router.push).toHaveBeenCalledWith(
  1221. expect.objectContaining({
  1222. pathname: '/organizations/org-slug/dashboards/new/',
  1223. query: {
  1224. displayType: 'table',
  1225. interval: '5m',
  1226. title: 'Custom Widget',
  1227. queryNames: [''],
  1228. queryConditions: [''],
  1229. queryFields: ['count()'],
  1230. queryOrderby: '-count()',
  1231. start: null,
  1232. end: null,
  1233. statsPeriod: '24h',
  1234. utc: null,
  1235. project: [],
  1236. environment: [],
  1237. widgetType: 'discover',
  1238. },
  1239. })
  1240. );
  1241. });
  1242. });
  1243. it('redirects correctly when choosing an existing dashboard', async function () {
  1244. const {router} = renderTestComponent({
  1245. query: {source: DashboardWidgetSource.DISCOVERV2},
  1246. dashboard: testDashboard,
  1247. });
  1248. await userEvent.click(await screen.findByText('Add Widget'));
  1249. await waitFor(() => {
  1250. expect(router.push).toHaveBeenCalledWith(
  1251. expect.objectContaining({
  1252. pathname: '/organizations/org-slug/dashboard/2/',
  1253. query: {
  1254. displayType: 'table',
  1255. interval: '5m',
  1256. title: 'Custom Widget',
  1257. queryNames: [''],
  1258. queryConditions: [''],
  1259. queryFields: ['count()'],
  1260. queryOrderby: '-count()',
  1261. start: null,
  1262. end: null,
  1263. statsPeriod: '24h',
  1264. utc: null,
  1265. project: [],
  1266. environment: [],
  1267. widgetType: 'discover',
  1268. },
  1269. })
  1270. );
  1271. });
  1272. });
  1273. it('shows the correct orderby when switching from a line chart to table', async function () {
  1274. const defaultWidgetQuery = {
  1275. name: '',
  1276. fields: ['count_unique(user)'],
  1277. columns: [],
  1278. aggregates: ['count_unique(user)'],
  1279. conditions: '',
  1280. orderby: 'count_unique(user)',
  1281. };
  1282. const defaultTableColumns = ['title', 'count_unique(user)'];
  1283. renderTestComponent({
  1284. orgFeatures: [...defaultOrgFeatures],
  1285. query: {
  1286. source: DashboardWidgetSource.DISCOVERV2,
  1287. defaultWidgetQuery: urlEncode(defaultWidgetQuery),
  1288. displayType: DisplayType.LINE,
  1289. defaultTableColumns,
  1290. },
  1291. });
  1292. await userEvent.click(await screen.findByText('Line Chart'));
  1293. await userEvent.click(screen.getByText('Table'));
  1294. expect(screen.getAllByText('count_unique(user)')[0]).toBeInTheDocument();
  1295. await waitFor(() => {
  1296. expect(eventsMock).toHaveBeenLastCalledWith(
  1297. '/organizations/org-slug/events/',
  1298. expect.objectContaining({
  1299. query: expect.objectContaining({
  1300. field: defaultTableColumns,
  1301. sort: ['count_unique(user)'],
  1302. }),
  1303. })
  1304. );
  1305. });
  1306. });
  1307. it('does not send request with orderby if a timeseries chart without grouping', async function () {
  1308. const defaultWidgetQuery = {
  1309. name: '',
  1310. fields: ['count_unique(user)'],
  1311. columns: [],
  1312. aggregates: ['count_unique(user)'],
  1313. conditions: '',
  1314. orderby: 'count_unique(user)',
  1315. };
  1316. const defaultTableColumns = ['title', 'count_unique(user)'];
  1317. renderTestComponent({
  1318. orgFeatures: [...defaultOrgFeatures],
  1319. query: {
  1320. source: DashboardWidgetSource.DISCOVERV2,
  1321. defaultWidgetQuery: urlEncode(defaultWidgetQuery),
  1322. displayType: DisplayType.LINE,
  1323. defaultTableColumns,
  1324. },
  1325. });
  1326. await waitFor(() => {
  1327. expect(eventsStatsMock).toHaveBeenLastCalledWith(
  1328. '/organizations/org-slug/events-stats/',
  1329. expect.objectContaining({
  1330. query: expect.objectContaining({
  1331. orderby: '',
  1332. }),
  1333. })
  1334. );
  1335. });
  1336. });
  1337. });
  1338. it('opens top-N widgets as area display', async function () {
  1339. const widget: Widget = {
  1340. id: '1',
  1341. title: 'Errors over time',
  1342. interval: '5m',
  1343. displayType: DisplayType.TOP_N,
  1344. queries: [
  1345. {
  1346. name: '',
  1347. conditions: '',
  1348. fields: ['count()', 'count_unique(id)'],
  1349. aggregates: ['count()', 'count_unique(id)'],
  1350. columns: [],
  1351. orderby: '-count()',
  1352. },
  1353. ],
  1354. };
  1355. const dashboard = mockDashboard({widgets: [widget]});
  1356. renderTestComponent({
  1357. orgFeatures: [...defaultOrgFeatures],
  1358. dashboard,
  1359. params: {
  1360. widgetIndex: '0',
  1361. },
  1362. });
  1363. expect(await screen.findByText('Area Chart')).toBeInTheDocument();
  1364. });
  1365. it('Update table header values (field alias)', async function () {
  1366. const handleSave = jest.fn();
  1367. renderTestComponent({
  1368. onSave: handleSave,
  1369. orgFeatures: [...defaultOrgFeatures],
  1370. });
  1371. await userEvent.click(screen.getByPlaceholderText('Alias'));
  1372. await userEvent.paste('First Alias');
  1373. await userEvent.click(screen.getByLabelText('Add a Column'));
  1374. await userEvent.click(screen.getAllByPlaceholderText('Alias')[1]);
  1375. await userEvent.paste('Second Alias');
  1376. await userEvent.click(screen.getByText('Add Widget'));
  1377. await waitFor(() => {
  1378. expect(handleSave).toHaveBeenCalledWith([
  1379. expect.objectContaining({
  1380. queries: [
  1381. expect.objectContaining({fieldAliases: ['First Alias', 'Second Alias']}),
  1382. ],
  1383. }),
  1384. ]);
  1385. });
  1386. });
  1387. it('does not wipe equation aliases when a column alias is updated', async function () {
  1388. renderTestComponent({
  1389. orgFeatures: [...defaultOrgFeatures],
  1390. });
  1391. await userEvent.click(screen.getByText('Add an Equation'));
  1392. await userEvent.click(screen.getAllByPlaceholderText('Alias')[1]);
  1393. await userEvent.paste('This should persist');
  1394. await userEvent.type(screen.getAllByPlaceholderText('Alias')[0], 'A');
  1395. expect(await screen.findByText('This should persist')).toBeInTheDocument();
  1396. });
  1397. it('does not wipe equation aliases when a column selection is made', async function () {
  1398. renderTestComponent({
  1399. orgFeatures: [...defaultOrgFeatures],
  1400. });
  1401. await userEvent.click(screen.getByText('Add an Equation'));
  1402. await userEvent.click(screen.getAllByPlaceholderText('Alias')[1]);
  1403. await userEvent.paste('This should persist');
  1404. // 1 for the table, 1 for the column selector, 1 for the sort
  1405. await waitFor(() => expect(screen.getAllByText('count()')).toHaveLength(3));
  1406. await selectEvent.select(screen.getAllByText('count()')[1], /count_unique/);
  1407. expect(screen.getByText('This should persist')).toBeInTheDocument();
  1408. });
  1409. it('copies over the orderby from the previous query if adding another', async function () {
  1410. renderTestComponent({
  1411. orgFeatures: [...defaultOrgFeatures],
  1412. });
  1413. await userEvent.click(await screen.findByText('Table'));
  1414. await userEvent.click(screen.getByText('Line Chart'));
  1415. await selectEvent.select(screen.getByText('Select group'), 'project');
  1416. await selectEvent.select(screen.getAllByText('count()')[1], 'count_unique(…)');
  1417. MockApiClient.clearMockResponses();
  1418. eventsStatsMock = MockApiClient.addMockResponse({
  1419. url: '/organizations/org-slug/events-stats/',
  1420. body: [],
  1421. });
  1422. await userEvent.click(screen.getByText('Add Query'));
  1423. // Assert on two calls, one for each query
  1424. const expectedArgs = expect.objectContaining({
  1425. query: expect.objectContaining({
  1426. orderby: '-count_unique(user)',
  1427. }),
  1428. });
  1429. expect(eventsStatsMock).toHaveBeenNthCalledWith(
  1430. 1,
  1431. '/organizations/org-slug/events-stats/',
  1432. expectedArgs
  1433. );
  1434. expect(eventsStatsMock).toHaveBeenNthCalledWith(
  1435. 2,
  1436. '/organizations/org-slug/events-stats/',
  1437. expectedArgs
  1438. );
  1439. });
  1440. it('disables add widget button and prevents widget previewing from firing widget query if widget query condition is invalid', async function () {
  1441. renderTestComponent({
  1442. orgFeatures: [...defaultOrgFeatures],
  1443. });
  1444. await userEvent.click(await screen.findByText('Table'));
  1445. await userEvent.click(screen.getByText('Line Chart'));
  1446. expect(eventsStatsMock).toHaveBeenCalledTimes(1);
  1447. await userEvent.click(
  1448. await screen.findByRole('combobox', {name: 'Add a search term'})
  1449. );
  1450. await userEvent.paste('transaction.duration:123a');
  1451. // Unfocus input
  1452. await userEvent.click(screen.getByText('Filter your results'));
  1453. expect(screen.getByText('Add Widget').closest('button')).toBeDisabled();
  1454. expect(screen.getByText('Widget query condition is invalid.')).toBeInTheDocument();
  1455. expect(eventsStatsMock).toHaveBeenCalledTimes(1);
  1456. });
  1457. describe('discover dataset split', function () {
  1458. let widget, dashboard;
  1459. describe('events', function () {
  1460. beforeEach(function () {
  1461. widget = {
  1462. displayType: DisplayType.TABLE,
  1463. interval: '1d',
  1464. queries: [
  1465. {
  1466. name: 'Test Widget',
  1467. fields: ['count()', 'count_unique(user)', 'epm()', 'project'],
  1468. columns: ['project'],
  1469. aggregates: ['count()', 'count_unique(user)', 'epm()'],
  1470. conditions: '',
  1471. orderby: '',
  1472. },
  1473. ],
  1474. title: 'Transactions',
  1475. id: '1',
  1476. };
  1477. dashboard = mockDashboard({widgets: [widget]});
  1478. });
  1479. it('selects the error discover split type as the dataset when the events request completes', async function () {
  1480. eventsMock = MockApiClient.addMockResponse({
  1481. url: '/organizations/org-slug/events/',
  1482. method: 'GET',
  1483. statusCode: 200,
  1484. body: {
  1485. meta: {discoverSplitDecision: WidgetType.ERRORS},
  1486. data: [],
  1487. },
  1488. });
  1489. const mockUpdateDashboardSplitDecision = jest.fn();
  1490. renderTestComponent({
  1491. orgFeatures: [...defaultOrgFeatures, 'performance-discover-dataset-selector'],
  1492. dashboard,
  1493. params: {
  1494. widgetIndex: '0',
  1495. },
  1496. updateDashboardSplitDecision: mockUpdateDashboardSplitDecision,
  1497. });
  1498. await waitFor(() => {
  1499. expect(eventsMock).toHaveBeenCalled();
  1500. });
  1501. expect(screen.getByRole('radio', {name: /errors/i})).toBeChecked();
  1502. expect(mockUpdateDashboardSplitDecision).toHaveBeenCalledWith(
  1503. '1',
  1504. WidgetType.ERRORS
  1505. );
  1506. expect(
  1507. await screen.findByText(
  1508. "We're splitting our datasets up to make it a bit easier to digest. We defaulted this widget to Errors. Edit as you see fit."
  1509. )
  1510. ).toBeInTheDocument();
  1511. });
  1512. it('selects the transaction discover split type as the dataset when the events request completes', async function () {
  1513. eventsMock = MockApiClient.addMockResponse({
  1514. url: '/organizations/org-slug/events/',
  1515. method: 'GET',
  1516. statusCode: 200,
  1517. body: {
  1518. meta: {discoverSplitDecision: WidgetType.TRANSACTIONS},
  1519. data: [],
  1520. },
  1521. });
  1522. const mockUpdateDashboardSplitDecision = jest.fn();
  1523. renderTestComponent({
  1524. orgFeatures: [...defaultOrgFeatures, 'performance-discover-dataset-selector'],
  1525. dashboard,
  1526. params: {
  1527. widgetIndex: '0',
  1528. },
  1529. updateDashboardSplitDecision: mockUpdateDashboardSplitDecision,
  1530. });
  1531. await waitFor(() => {
  1532. expect(eventsMock).toHaveBeenCalled();
  1533. });
  1534. expect(screen.getByRole('radio', {name: /transactions/i})).toBeChecked();
  1535. expect(mockUpdateDashboardSplitDecision).toHaveBeenCalledWith(
  1536. '1',
  1537. WidgetType.TRANSACTIONS
  1538. );
  1539. expect(
  1540. await screen.findByText(
  1541. "We're splitting our datasets up to make it a bit easier to digest. We defaulted this widget to Transactions. Edit as you see fit."
  1542. )
  1543. ).toBeInTheDocument();
  1544. });
  1545. it('persists the query state for tables when switching between errors and transactions', async function () {
  1546. dashboard = mockDashboard({
  1547. widgets: [
  1548. WidgetFixture({
  1549. displayType: DisplayType.TABLE,
  1550. widgetType: WidgetType.TRANSACTIONS,
  1551. queries: [
  1552. {
  1553. name: 'Test Widget',
  1554. fields: ['p99(transaction.duration)'],
  1555. columns: [],
  1556. aggregates: ['p99(transaction.duration)'],
  1557. conditions: 'testFilter:value',
  1558. orderby: '',
  1559. },
  1560. ],
  1561. }),
  1562. ],
  1563. });
  1564. renderTestComponent({
  1565. orgFeatures: [...defaultOrgFeatures, 'performance-discover-dataset-selector'],
  1566. dashboard,
  1567. params: {
  1568. widgetIndex: '0',
  1569. },
  1570. });
  1571. expect(await screen.findByText(/p99\(…\)/i)).toBeInTheDocument();
  1572. expect(screen.getByText('transaction.duration')).toBeInTheDocument();
  1573. expect(screen.getByRole('row', {name: 'testFilter:value'})).toBeInTheDocument();
  1574. expect(screen.getByRole('radio', {name: /transactions/i})).toBeChecked();
  1575. // Switch to errors
  1576. await userEvent.click(screen.getByRole('radio', {name: /errors/i}));
  1577. expect(screen.getByRole('radio', {name: /transactions/i})).not.toBeChecked();
  1578. expect(screen.getByRole('radio', {name: /errors/i})).toBeChecked();
  1579. // The state is still the same
  1580. expect(await screen.findByText(/p99\(…\)/i)).toBeInTheDocument();
  1581. expect(screen.getByText('transaction.duration')).toBeInTheDocument();
  1582. expect(screen.getByRole('row', {name: 'testFilter:value'})).toBeInTheDocument();
  1583. });
  1584. it('sets the correct default count_if parameters for the errors dataset', async function () {
  1585. dashboard = mockDashboard({
  1586. widgets: [
  1587. WidgetFixture({
  1588. displayType: DisplayType.TABLE,
  1589. widgetType: WidgetType.ERRORS,
  1590. queries: [
  1591. {
  1592. name: 'Test Widget',
  1593. fields: ['count()'],
  1594. columns: [],
  1595. aggregates: ['count()'],
  1596. conditions: '',
  1597. orderby: '',
  1598. },
  1599. ],
  1600. }),
  1601. ],
  1602. });
  1603. renderTestComponent({
  1604. orgFeatures: [...defaultOrgFeatures, 'performance-discover-dataset-selector'],
  1605. dashboard,
  1606. params: {
  1607. widgetIndex: '0',
  1608. },
  1609. });
  1610. await userEvent.click(await screen.findByTestId('label'));
  1611. await userEvent.click(screen.getByText(/count_if/));
  1612. const fieldLabels = screen.getAllByTestId('label');
  1613. expect(fieldLabels[0]).toHaveTextContent(/count_if/);
  1614. expect(fieldLabels[1]).toHaveTextContent('event.type');
  1615. expect(screen.getByDisplayValue('error')).toBeInTheDocument();
  1616. });
  1617. it('sets the correct default count_if parameters for the transactions dataset', async function () {
  1618. dashboard = mockDashboard({
  1619. widgets: [
  1620. WidgetFixture({
  1621. displayType: DisplayType.TABLE,
  1622. widgetType: WidgetType.TRANSACTIONS,
  1623. queries: [
  1624. {
  1625. name: 'Test Widget',
  1626. fields: ['count()'],
  1627. columns: [],
  1628. aggregates: ['count()'],
  1629. conditions: '',
  1630. orderby: '',
  1631. },
  1632. ],
  1633. }),
  1634. ],
  1635. });
  1636. renderTestComponent({
  1637. orgFeatures: [...defaultOrgFeatures, 'performance-discover-dataset-selector'],
  1638. dashboard,
  1639. params: {
  1640. widgetIndex: '0',
  1641. },
  1642. });
  1643. await userEvent.click(await screen.findByTestId('label'));
  1644. await userEvent.click(screen.getByText(/count_if/));
  1645. const fieldLabels = screen.getAllByTestId('label');
  1646. expect(fieldLabels[0]).toHaveTextContent(/count_if/);
  1647. expect(fieldLabels[1]).toHaveTextContent('transaction.duration');
  1648. expect(screen.getByDisplayValue('300')).toBeInTheDocument();
  1649. });
  1650. });
  1651. describe('events-stats', function () {
  1652. beforeEach(function () {
  1653. widget = {
  1654. displayType: DisplayType.LINE,
  1655. interval: '1d',
  1656. queries: [
  1657. {
  1658. name: 'Test Widget',
  1659. fields: ['count()', 'count_unique(user)', 'epm()', 'project'],
  1660. columns: ['project'],
  1661. aggregates: ['count()', 'count_unique(user)', 'epm()'],
  1662. conditions: '',
  1663. orderby: '',
  1664. },
  1665. ],
  1666. title: 'Transactions',
  1667. id: '1',
  1668. };
  1669. dashboard = mockDashboard({widgets: [widget]});
  1670. });
  1671. it('selects the error discover split type as the dataset when the request completes', async function () {
  1672. eventsStatsMock = MockApiClient.addMockResponse({
  1673. url: '/organizations/org-slug/events-stats/',
  1674. method: 'GET',
  1675. statusCode: 200,
  1676. body: {
  1677. meta: {discoverSplitDecision: WidgetType.ERRORS},
  1678. data: [],
  1679. },
  1680. });
  1681. const mockUpdateDashboardSplitDecision = jest.fn();
  1682. renderTestComponent({
  1683. orgFeatures: [...defaultOrgFeatures, 'performance-discover-dataset-selector'],
  1684. dashboard,
  1685. params: {
  1686. widgetIndex: '0',
  1687. },
  1688. updateDashboardSplitDecision: mockUpdateDashboardSplitDecision,
  1689. });
  1690. await waitFor(() => {
  1691. expect(eventsStatsMock).toHaveBeenCalled();
  1692. });
  1693. expect(screen.getByRole('radio', {name: /errors/i})).toBeChecked();
  1694. expect(mockUpdateDashboardSplitDecision).toHaveBeenCalledWith(
  1695. '1',
  1696. WidgetType.ERRORS
  1697. );
  1698. expect(
  1699. await screen.findByText(
  1700. "We're splitting our datasets up to make it a bit easier to digest. We defaulted this widget to Errors. Edit as you see fit."
  1701. )
  1702. ).toBeInTheDocument();
  1703. });
  1704. it('selects the transaction discover split type as the dataset when the request completes', async function () {
  1705. eventsStatsMock = MockApiClient.addMockResponse({
  1706. url: '/organizations/org-slug/events-stats/',
  1707. method: 'GET',
  1708. statusCode: 200,
  1709. body: {
  1710. meta: {discoverSplitDecision: WidgetType.TRANSACTIONS},
  1711. data: [],
  1712. },
  1713. });
  1714. const mockUpdateDashboardSplitDecision = jest.fn();
  1715. renderTestComponent({
  1716. orgFeatures: [...defaultOrgFeatures, 'performance-discover-dataset-selector'],
  1717. dashboard,
  1718. params: {
  1719. widgetIndex: '0',
  1720. },
  1721. updateDashboardSplitDecision: mockUpdateDashboardSplitDecision,
  1722. });
  1723. await waitFor(() => {
  1724. expect(eventsStatsMock).toHaveBeenCalled();
  1725. });
  1726. expect(screen.getByRole('radio', {name: /transactions/i})).toBeChecked();
  1727. expect(mockUpdateDashboardSplitDecision).toHaveBeenCalledWith(
  1728. '1',
  1729. WidgetType.TRANSACTIONS
  1730. );
  1731. expect(
  1732. await screen.findByText(
  1733. "We're splitting our datasets up to make it a bit easier to digest. We defaulted this widget to Transactions. Edit as you see fit."
  1734. )
  1735. ).toBeInTheDocument();
  1736. });
  1737. it('persists the query state for timeseries when switching between errors and transactions', async function () {
  1738. dashboard = mockDashboard({
  1739. widgets: [
  1740. WidgetFixture({
  1741. displayType: DisplayType.LINE,
  1742. widgetType: WidgetType.TRANSACTIONS,
  1743. queries: [
  1744. {
  1745. name: 'Test Widget',
  1746. fields: ['p99(transaction.duration)'],
  1747. columns: [],
  1748. aggregates: ['p99(transaction.duration)'],
  1749. conditions: 'testFilter:value',
  1750. orderby: '',
  1751. },
  1752. ],
  1753. }),
  1754. ],
  1755. });
  1756. renderTestComponent({
  1757. orgFeatures: [...defaultOrgFeatures, 'performance-discover-dataset-selector'],
  1758. dashboard,
  1759. params: {
  1760. widgetIndex: '0',
  1761. },
  1762. });
  1763. expect(await screen.findByText(/p99\(…\)/i)).toBeInTheDocument();
  1764. expect(screen.getByText('transaction.duration')).toBeInTheDocument();
  1765. expect(screen.getByRole('row', {name: 'testFilter:value'})).toBeInTheDocument(); // Check for query builder token
  1766. expect(screen.getByRole('radio', {name: /transactions/i})).toBeChecked();
  1767. // Switch to errors
  1768. await userEvent.click(screen.getByRole('radio', {name: /errors/i}));
  1769. expect(screen.getByRole('radio', {name: /transactions/i})).not.toBeChecked();
  1770. expect(screen.getByRole('radio', {name: /errors/i})).toBeChecked();
  1771. // The state is still the same
  1772. expect(await screen.findByText(/p99\(…\)/i)).toBeInTheDocument();
  1773. expect(screen.getByText('transaction.duration')).toBeInTheDocument();
  1774. expect(screen.getByRole('row', {name: 'testFilter:value'})).toBeInTheDocument();
  1775. });
  1776. it('sets the correct default count_if parameters for the errors dataset', async function () {
  1777. dashboard = mockDashboard({
  1778. widgets: [
  1779. WidgetFixture({
  1780. displayType: DisplayType.LINE,
  1781. widgetType: WidgetType.ERRORS,
  1782. queries: [
  1783. {
  1784. name: 'Test Widget',
  1785. fields: ['count()'],
  1786. columns: [],
  1787. aggregates: ['count()'],
  1788. conditions: '',
  1789. orderby: '',
  1790. },
  1791. ],
  1792. }),
  1793. ],
  1794. });
  1795. renderTestComponent({
  1796. orgFeatures: [...defaultOrgFeatures, 'performance-discover-dataset-selector'],
  1797. dashboard,
  1798. params: {
  1799. widgetIndex: '0',
  1800. },
  1801. });
  1802. await userEvent.click(await screen.findByTestId('label'));
  1803. await userEvent.click(screen.getByText(/count_if/));
  1804. const fieldLabels = screen.getAllByTestId('label');
  1805. expect(fieldLabels[0]).toHaveTextContent(/count_if/);
  1806. expect(fieldLabels[1]).toHaveTextContent('event.type');
  1807. expect(screen.getByDisplayValue('error')).toBeInTheDocument();
  1808. });
  1809. it('sets the correct default count_if parameters for the transactions dataset', async function () {
  1810. dashboard = mockDashboard({
  1811. widgets: [
  1812. WidgetFixture({
  1813. displayType: DisplayType.LINE,
  1814. widgetType: WidgetType.TRANSACTIONS,
  1815. queries: [
  1816. {
  1817. name: 'Test Widget',
  1818. fields: ['count()'],
  1819. columns: [],
  1820. aggregates: ['count()'],
  1821. conditions: '',
  1822. orderby: '',
  1823. },
  1824. ],
  1825. }),
  1826. ],
  1827. });
  1828. renderTestComponent({
  1829. orgFeatures: [...defaultOrgFeatures, 'performance-discover-dataset-selector'],
  1830. dashboard,
  1831. params: {
  1832. widgetIndex: '0',
  1833. },
  1834. });
  1835. await userEvent.click(await screen.findByTestId('label'));
  1836. await userEvent.click(screen.getByText(/count_if/));
  1837. const fieldLabels = screen.getAllByTestId('label');
  1838. expect(fieldLabels[0]).toHaveTextContent(/count_if/);
  1839. expect(fieldLabels[1]).toHaveTextContent('transaction.duration');
  1840. expect(screen.getByDisplayValue('300')).toBeInTheDocument();
  1841. });
  1842. });
  1843. describe('discover split warning', function () {
  1844. it('does not show the alert if the widget type is already split', async function () {
  1845. dashboard = mockDashboard({
  1846. widgets: [WidgetFixture({widgetType: WidgetType.TRANSACTIONS})],
  1847. });
  1848. eventsStatsMock = MockApiClient.addMockResponse({
  1849. url: '/organizations/org-slug/events-stats/',
  1850. method: 'GET',
  1851. statusCode: 200,
  1852. body: {
  1853. meta: {},
  1854. data: [],
  1855. },
  1856. });
  1857. renderTestComponent({
  1858. orgFeatures: [...defaultOrgFeatures, 'performance-discover-dataset-selector'],
  1859. dashboard,
  1860. params: {
  1861. widgetIndex: '0',
  1862. },
  1863. });
  1864. await waitFor(() => {
  1865. expect(screen.getByRole('radio', {name: /transactions/i})).toBeChecked();
  1866. });
  1867. expect(
  1868. screen.queryByText(/we're splitting our datasets/i)
  1869. ).not.toBeInTheDocument();
  1870. });
  1871. it('shows the alert if the widget is split but the decision is forced', async function () {
  1872. dashboard = mockDashboard({
  1873. widgets: [
  1874. WidgetFixture({
  1875. widgetType: WidgetType.ERRORS,
  1876. datasetSource: DatasetSource.FORCED,
  1877. }),
  1878. ],
  1879. });
  1880. eventsStatsMock = MockApiClient.addMockResponse({
  1881. url: '/organizations/org-slug/events-stats/',
  1882. method: 'GET',
  1883. statusCode: 200,
  1884. body: {
  1885. meta: {},
  1886. data: [],
  1887. },
  1888. });
  1889. renderTestComponent({
  1890. orgFeatures: [...defaultOrgFeatures, 'performance-discover-dataset-selector'],
  1891. dashboard,
  1892. params: {
  1893. widgetIndex: '0',
  1894. },
  1895. });
  1896. await waitFor(() => {
  1897. expect(screen.getByRole('radio', {name: /errors/i})).toBeChecked();
  1898. });
  1899. expect(
  1900. await screen.findByText(
  1901. "We're splitting our datasets up to make it a bit easier to digest. We defaulted this widget to Errors. Edit as you see fit."
  1902. )
  1903. ).toBeInTheDocument();
  1904. });
  1905. it('is dismissable', async function () {
  1906. dashboard = mockDashboard({
  1907. widgets: [
  1908. WidgetFixture({
  1909. widgetType: WidgetType.ERRORS,
  1910. datasetSource: DatasetSource.FORCED,
  1911. }),
  1912. ],
  1913. });
  1914. eventsStatsMock = MockApiClient.addMockResponse({
  1915. url: '/organizations/org-slug/events-stats/',
  1916. method: 'GET',
  1917. statusCode: 200,
  1918. body: {
  1919. meta: {},
  1920. data: [],
  1921. },
  1922. });
  1923. renderTestComponent({
  1924. orgFeatures: [...defaultOrgFeatures, 'performance-discover-dataset-selector'],
  1925. dashboard,
  1926. params: {
  1927. widgetIndex: '0',
  1928. },
  1929. });
  1930. expect(
  1931. await screen.findByText(
  1932. "We're splitting our datasets up to make it a bit easier to digest. We defaulted this widget to Errors. Edit as you see fit."
  1933. )
  1934. ).toBeInTheDocument();
  1935. await userEvent.click(screen.getByRole('button', {name: 'Close'}));
  1936. expect(
  1937. screen.queryByText(/we're splitting our datasets/i)
  1938. ).not.toBeInTheDocument();
  1939. });
  1940. });
  1941. });
  1942. describe('Widget Library', function () {
  1943. it('only opens the modal when the query data is changed', async function () {
  1944. const mockModal = jest.spyOn(modals, 'openWidgetBuilderOverwriteModal');
  1945. renderTestComponent();
  1946. await screen.findByText('Widget Library');
  1947. await userEvent.click(screen.getByText('Duration Distribution'));
  1948. // Widget Library, Builder title, and Chart title
  1949. expect(screen.getAllByText('Duration Distribution')).toHaveLength(2);
  1950. // Confirm modal doesn't open because no changes were made
  1951. expect(mockModal).not.toHaveBeenCalled();
  1952. await userEvent.click(screen.getAllByLabelText('Remove this Y-Axis')[0]);
  1953. await userEvent.click(screen.getByText('High Throughput Transactions'));
  1954. // Should not have overwritten widget data, and confirm modal should open
  1955. expect(screen.getAllByText('Duration Distribution')).toHaveLength(2);
  1956. expect(mockModal).toHaveBeenCalled();
  1957. });
  1958. });
  1959. describe('group by field', function () {
  1960. it('does not contain functions as options', async function () {
  1961. renderTestComponent({
  1962. query: {displayType: 'line'},
  1963. orgFeatures: [...defaultOrgFeatures],
  1964. });
  1965. expect(await screen.findByText('Select group')).toBeInTheDocument();
  1966. await userEvent.click(screen.getByText('Select group'));
  1967. // Only one f(x) field set in the y-axis selector
  1968. expect(screen.getByText('f(x)')).toBeInTheDocument();
  1969. });
  1970. it('adds more fields when Add Group is clicked', async function () {
  1971. renderTestComponent({
  1972. query: {displayType: 'line'},
  1973. orgFeatures: [...defaultOrgFeatures],
  1974. });
  1975. await userEvent.click(await screen.findByText('Add Group'));
  1976. expect(await screen.findAllByText('Select group')).toHaveLength(2);
  1977. });
  1978. it("doesn't reset group by when changing y-axis", async function () {
  1979. renderTestComponent({
  1980. query: {displayType: 'line'},
  1981. orgFeatures: [...defaultOrgFeatures],
  1982. });
  1983. await selectEvent.select(await screen.findByText('Select group'), 'project');
  1984. await userEvent.click(screen.getAllByText('count()')[0], {
  1985. skipHover: true,
  1986. });
  1987. await userEvent.click(screen.getByText(/count_unique/), {
  1988. skipHover: true,
  1989. });
  1990. expect(await screen.findByText('project')).toBeInTheDocument();
  1991. });
  1992. it("doesn't erase the selection when switching to another time series", async function () {
  1993. renderTestComponent({
  1994. query: {displayType: 'line'},
  1995. orgFeatures: [...defaultOrgFeatures],
  1996. });
  1997. await selectEvent.select(await screen.findByText('Select group'), 'project');
  1998. await userEvent.click(screen.getByText('Line Chart'));
  1999. await userEvent.click(screen.getByText('Area Chart'));
  2000. expect(await screen.findByText('project')).toBeInTheDocument();
  2001. });
  2002. it('sends a top N request when a grouping is selected', async function () {
  2003. renderTestComponent({
  2004. query: {displayType: 'line'},
  2005. orgFeatures: [...defaultOrgFeatures],
  2006. });
  2007. await userEvent.click(await screen.findByText('Group your results'));
  2008. await userEvent.type(screen.getByText('Select group'), 'project{enter}');
  2009. await waitFor(() =>
  2010. expect(eventsStatsMock).toHaveBeenNthCalledWith(
  2011. 2,
  2012. '/organizations/org-slug/events-stats/',
  2013. expect.objectContaining({
  2014. query: expect.objectContaining({
  2015. query: '',
  2016. yAxis: ['count()'],
  2017. field: ['project', 'count()'],
  2018. topEvents: TOP_N,
  2019. orderby: '-count()',
  2020. }),
  2021. })
  2022. )
  2023. );
  2024. });
  2025. it('allows deleting groups until there is one left', async function () {
  2026. renderTestComponent({
  2027. query: {displayType: 'line'},
  2028. orgFeatures: [...defaultOrgFeatures],
  2029. });
  2030. await userEvent.click(await screen.findByText('Add Group'));
  2031. expect(screen.getAllByLabelText('Remove group')).toHaveLength(2);
  2032. await userEvent.click(screen.getAllByLabelText('Remove group')[1]);
  2033. await waitFor(() =>
  2034. expect(screen.queryByLabelText('Remove group')).not.toBeInTheDocument()
  2035. );
  2036. });
  2037. it("display 'remove' and 'drag to reorder' buttons", async function () {
  2038. renderTestComponent({
  2039. query: {displayType: 'line'},
  2040. orgFeatures: [...defaultOrgFeatures],
  2041. });
  2042. expect(screen.queryByLabelText('Remove group')).not.toBeInTheDocument();
  2043. await selectEvent.select(screen.getByText('Select group'), 'project');
  2044. expect(screen.getByLabelText('Remove group')).toBeInTheDocument();
  2045. expect(screen.queryByLabelText('Drag to reorder')).not.toBeInTheDocument();
  2046. await userEvent.click(screen.getByText('Add Group'));
  2047. expect(screen.getAllByLabelText('Remove group')).toHaveLength(2);
  2048. expect(screen.getAllByLabelText('Drag to reorder')).toHaveLength(2);
  2049. });
  2050. it.todo(
  2051. 'Since simulate drag and drop with RTL is not recommended because of browser layout, remember to create acceptance test for this'
  2052. );
  2053. });
  2054. describe('limit field', function () {
  2055. it('renders if groupBy value is present', async function () {
  2056. const handleSave = jest.fn();
  2057. renderTestComponent({
  2058. query: {displayType: 'line'},
  2059. orgFeatures: [...defaultOrgFeatures],
  2060. onSave: handleSave,
  2061. });
  2062. await selectEvent.select(await screen.findByText('Select group'), 'project');
  2063. expect(screen.getByText('Limit to 5 results')).toBeInTheDocument();
  2064. await userEvent.click(screen.getByText('Add Widget'));
  2065. await waitFor(() =>
  2066. expect(handleSave).toHaveBeenCalledWith([
  2067. expect.objectContaining({
  2068. limit: 5,
  2069. }),
  2070. ])
  2071. );
  2072. });
  2073. it('update value', async function () {
  2074. renderTestComponent({
  2075. query: {displayType: 'line'},
  2076. orgFeatures: [...defaultOrgFeatures],
  2077. });
  2078. await selectEvent.select(await screen.findByText('Select group'), 'project');
  2079. await userEvent.click(screen.getByText('Limit to 5 results'));
  2080. await userEvent.click(screen.getByText('Limit to 2 results'));
  2081. await waitFor(() =>
  2082. expect(eventsStatsMock).toHaveBeenCalledWith(
  2083. '/organizations/org-slug/events-stats/',
  2084. expect.objectContaining({
  2085. query: expect.objectContaining({
  2086. query: '',
  2087. yAxis: ['count()'],
  2088. field: ['project', 'count()'],
  2089. topEvents: 2,
  2090. orderby: '-count()',
  2091. }),
  2092. })
  2093. )
  2094. );
  2095. });
  2096. it('gets removed if no groupBy value', async function () {
  2097. renderTestComponent({
  2098. query: {displayType: 'line'},
  2099. orgFeatures: [...defaultOrgFeatures],
  2100. });
  2101. await selectEvent.select(await screen.findByText('Select group'), 'project');
  2102. expect(screen.getByText('Limit to 5 results')).toBeInTheDocument();
  2103. await userEvent.click(screen.getByLabelText('Remove group'));
  2104. await waitFor(() =>
  2105. expect(screen.queryByText('Limit to 5 results')).not.toBeInTheDocument()
  2106. );
  2107. });
  2108. it('applies a limit when switching from a table to timeseries chart with grouping', async function () {
  2109. const widget: Widget = {
  2110. displayType: DisplayType.TABLE,
  2111. interval: '1d',
  2112. queries: [
  2113. {
  2114. name: 'Test Widget',
  2115. fields: ['count()', 'count_unique(user)', 'epm()', 'project'],
  2116. columns: ['project'],
  2117. aggregates: ['count()', 'count_unique(user)', 'epm()'],
  2118. conditions: '',
  2119. orderby: '',
  2120. },
  2121. ],
  2122. title: 'Transactions',
  2123. id: '1',
  2124. };
  2125. const dashboard = mockDashboard({widgets: [widget]});
  2126. renderTestComponent({
  2127. dashboard,
  2128. orgFeatures: [...defaultOrgFeatures],
  2129. params: {
  2130. widgetIndex: '0',
  2131. },
  2132. });
  2133. await userEvent.click(await screen.findByText('Table'));
  2134. await userEvent.click(screen.getByText('Line Chart'));
  2135. expect(screen.getByText('Limit to 3 results')).toBeInTheDocument();
  2136. expect(eventsStatsMock).toHaveBeenCalledWith(
  2137. '/organizations/org-slug/events-stats/',
  2138. expect.objectContaining({
  2139. query: expect.objectContaining({
  2140. topEvents: 3,
  2141. }),
  2142. })
  2143. );
  2144. });
  2145. it('persists the limit when switching between timeseries charts', async function () {
  2146. const widget: Widget = {
  2147. displayType: DisplayType.AREA,
  2148. interval: '1d',
  2149. queries: [
  2150. {
  2151. name: 'Test Widget',
  2152. fields: ['count()', 'count_unique(user)', 'epm()', 'project'],
  2153. columns: ['project'],
  2154. aggregates: ['count()', 'count_unique(user)', 'epm()'],
  2155. conditions: '',
  2156. orderby: '',
  2157. },
  2158. ],
  2159. title: 'Transactions',
  2160. id: '1',
  2161. limit: 1,
  2162. };
  2163. const dashboard = mockDashboard({widgets: [widget]});
  2164. renderTestComponent({
  2165. dashboard,
  2166. orgFeatures: [...defaultOrgFeatures],
  2167. params: {
  2168. widgetIndex: '0',
  2169. },
  2170. });
  2171. await userEvent.click(await screen.findByText('Area Chart'));
  2172. await userEvent.click(screen.getByText('Line Chart'));
  2173. expect(screen.getByText('Limit to 1 result')).toBeInTheDocument();
  2174. expect(eventsStatsMock).toHaveBeenCalledWith(
  2175. '/organizations/org-slug/events-stats/',
  2176. expect.objectContaining({
  2177. query: expect.objectContaining({
  2178. topEvents: 1,
  2179. }),
  2180. })
  2181. );
  2182. });
  2183. it('unsets the limit when going from timeseries to table', async function () {
  2184. const widget: Widget = {
  2185. displayType: DisplayType.AREA,
  2186. interval: '1d',
  2187. queries: [
  2188. {
  2189. name: 'Test Widget',
  2190. fields: ['count()', 'count_unique(user)', 'epm()', 'project'],
  2191. columns: ['project'],
  2192. aggregates: ['count()', 'count_unique(user)', 'epm()'],
  2193. conditions: '',
  2194. orderby: '',
  2195. },
  2196. ],
  2197. title: 'Transactions',
  2198. id: '1',
  2199. limit: 1,
  2200. };
  2201. const dashboard = mockDashboard({widgets: [widget]});
  2202. renderTestComponent({
  2203. dashboard,
  2204. orgFeatures: [...defaultOrgFeatures],
  2205. params: {
  2206. widgetIndex: '0',
  2207. },
  2208. });
  2209. await userEvent.click(await screen.findByText('Area Chart'));
  2210. await userEvent.click(screen.getByText('Table'));
  2211. expect(screen.queryByText('Limit to 1 result')).not.toBeInTheDocument();
  2212. expect(eventsMock).toHaveBeenCalledWith(
  2213. '/organizations/org-slug/events/',
  2214. expect.objectContaining({
  2215. query: expect.objectContaining({
  2216. topEvents: undefined,
  2217. }),
  2218. })
  2219. );
  2220. });
  2221. });
  2222. describe('Spans Dataset', () => {
  2223. it('queries for span tags and returns the correct data', async () => {
  2224. MockApiClient.addMockResponse({
  2225. url: `/organizations/org-slug/spans/fields/`,
  2226. body: [
  2227. {
  2228. key: 'plan',
  2229. name: 'Plan',
  2230. },
  2231. ],
  2232. match: [
  2233. function (_url: string, options: Record<string, any>) {
  2234. return options.query.type === 'string';
  2235. },
  2236. ],
  2237. });
  2238. MockApiClient.addMockResponse({
  2239. url: `/organizations/org-slug/spans/fields/`,
  2240. body: [
  2241. {
  2242. key: 'lcp.size',
  2243. name: 'Lcp.Size',
  2244. },
  2245. {
  2246. key: 'something.else',
  2247. name: 'Something.Else',
  2248. },
  2249. ],
  2250. match: [
  2251. function (_url: string, options: Record<string, any>) {
  2252. return options.query.type === 'number';
  2253. },
  2254. ],
  2255. });
  2256. const dashboard = mockDashboard({
  2257. widgets: [
  2258. WidgetFixture({
  2259. widgetType: WidgetType.SPANS,
  2260. displayType: DisplayType.TABLE,
  2261. queries: [
  2262. {
  2263. name: 'Test Widget',
  2264. fields: ['count(tags[lcp.size,number])'],
  2265. columns: [],
  2266. aggregates: ['count(tags[lcp.size,number])'],
  2267. conditions: '',
  2268. orderby: '',
  2269. },
  2270. ],
  2271. }),
  2272. ],
  2273. });
  2274. renderTestComponent({
  2275. dashboard,
  2276. orgFeatures: [...defaultOrgFeatures],
  2277. params: {
  2278. widgetIndex: '0',
  2279. },
  2280. });
  2281. // Click the argument to the count() function
  2282. expect(await screen.findByText('lcp.size')).toBeInTheDocument();
  2283. await userEvent.click(screen.getByText('lcp.size'));
  2284. // The option now appears in the aggregate property dropdown
  2285. expect(screen.queryAllByText('lcp.size')).toHaveLength(2);
  2286. expect(screen.getByText('something.else')).toBeInTheDocument();
  2287. // Click count() to verify the string tag is in the dropdown
  2288. expect(screen.queryByText('plan')).not.toBeInTheDocument();
  2289. await userEvent.click(screen.getByText(`count(…)`));
  2290. expect(screen.getByText('plan')).toBeInTheDocument();
  2291. });
  2292. });
  2293. });