detail.spec.tsx 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129
  1. import {DashboardFixture} from 'sentry-fixture/dashboard';
  2. import {LocationFixture} from 'sentry-fixture/locationFixture';
  3. import {OrganizationFixture} from 'sentry-fixture/organization';
  4. import {ProjectFixture} from 'sentry-fixture/project';
  5. import {ReleaseFixture} from 'sentry-fixture/release';
  6. import {RouteComponentPropsFixture} from 'sentry-fixture/routeComponentPropsFixture';
  7. import {TeamFixture} from 'sentry-fixture/team';
  8. import {UserFixture} from 'sentry-fixture/user';
  9. import {WidgetFixture} from 'sentry-fixture/widget';
  10. import {initializeOrg} from 'sentry-test/initializeOrg';
  11. import {
  12. act,
  13. render,
  14. screen,
  15. userEvent,
  16. waitFor,
  17. within,
  18. } from 'sentry-test/reactTestingLibrary';
  19. import * as modals from 'sentry/actionCreators/modal';
  20. import ConfigStore from 'sentry/stores/configStore';
  21. import PageFiltersStore from 'sentry/stores/pageFiltersStore';
  22. import ProjectsStore from 'sentry/stores/projectsStore';
  23. import TeamStore from 'sentry/stores/teamStore';
  24. import {browserHistory} from 'sentry/utils/browserHistory';
  25. import CreateDashboard from 'sentry/views/dashboards/create';
  26. import DashboardDetail, {
  27. handleUpdateDashboardSplit,
  28. } from 'sentry/views/dashboards/detail';
  29. import EditAccessSelector from 'sentry/views/dashboards/editAccessSelector';
  30. import * as types from 'sentry/views/dashboards/types';
  31. import {DashboardState} from 'sentry/views/dashboards/types';
  32. import ViewEditDashboard from 'sentry/views/dashboards/view';
  33. import {OrganizationContext} from 'sentry/views/organizationContext';
  34. describe('Dashboards > Detail', function () {
  35. const organization = OrganizationFixture({
  36. features: ['global-views', 'dashboards-basic', 'dashboards-edit', 'discover-query'],
  37. });
  38. const projects = [ProjectFixture()];
  39. describe('prebuilt dashboards', function () {
  40. let initialData;
  41. beforeEach(function () {
  42. act(() => ProjectsStore.loadInitialData(projects));
  43. initialData = initializeOrg({organization});
  44. MockApiClient.addMockResponse({
  45. url: '/organizations/org-slug/tags/',
  46. body: [],
  47. });
  48. MockApiClient.addMockResponse({
  49. url: '/organizations/org-slug/projects/',
  50. body: [ProjectFixture()],
  51. });
  52. MockApiClient.addMockResponse({
  53. url: '/organizations/org-slug/dashboards/',
  54. body: [
  55. DashboardFixture([], {id: 'default-overview', title: 'Default'}),
  56. DashboardFixture([], {id: '1', title: 'Custom Errors'}),
  57. ],
  58. });
  59. MockApiClient.addMockResponse({
  60. url: '/organizations/org-slug/dashboards/default-overview/',
  61. body: DashboardFixture([], {id: 'default-overview', title: 'Default'}),
  62. });
  63. MockApiClient.addMockResponse({
  64. url: '/organizations/org-slug/dashboards/1/visit/',
  65. method: 'POST',
  66. body: [],
  67. statusCode: 200,
  68. });
  69. MockApiClient.addMockResponse({
  70. url: '/organizations/org-slug/users/',
  71. method: 'GET',
  72. body: [],
  73. });
  74. MockApiClient.addMockResponse({
  75. url: '/organizations/org-slug/sdk-updates/',
  76. body: [],
  77. });
  78. MockApiClient.addMockResponse({
  79. url: '/organizations/org-slug/prompts-activity/',
  80. body: {},
  81. });
  82. MockApiClient.addMockResponse({
  83. url: '/organizations/org-slug/events/',
  84. method: 'GET',
  85. body: [],
  86. });
  87. MockApiClient.addMockResponse({
  88. url: '/organizations/org-slug/events-stats/',
  89. body: {data: []},
  90. });
  91. MockApiClient.addMockResponse({
  92. method: 'GET',
  93. url: '/organizations/org-slug/issues/',
  94. body: [],
  95. });
  96. MockApiClient.addMockResponse({
  97. url: '/organizations/org-slug/releases/',
  98. body: [],
  99. });
  100. MockApiClient.addMockResponse({
  101. url: '/organizations/org-slug/releases/stats/',
  102. body: [],
  103. });
  104. MockApiClient.addMockResponse({
  105. url: '/organizations/org-slug/metrics/meta/',
  106. body: [],
  107. });
  108. });
  109. afterEach(function () {
  110. MockApiClient.clearMockResponses();
  111. });
  112. it('assigns unique IDs to all widgets so grid keys are unique', async function () {
  113. MockApiClient.addMockResponse({
  114. url: '/organizations/org-slug/events-stats/',
  115. body: {data: []},
  116. });
  117. MockApiClient.addMockResponse({
  118. url: '/organizations/org-slug/dashboards/default-overview/',
  119. body: DashboardFixture(
  120. [
  121. WidgetFixture({
  122. queries: [
  123. {
  124. name: '',
  125. conditions: 'event.type:error',
  126. fields: ['count()'],
  127. aggregates: ['count()'],
  128. columns: [],
  129. orderby: '-count()',
  130. },
  131. ],
  132. title: 'Default Widget 1',
  133. interval: '1d',
  134. }),
  135. WidgetFixture({
  136. queries: [
  137. {
  138. name: '',
  139. conditions: 'event.type:transaction',
  140. fields: ['count()'],
  141. aggregates: ['count()'],
  142. columns: [],
  143. orderby: '-count()',
  144. },
  145. ],
  146. title: 'Default Widget 2',
  147. interval: '1d',
  148. }),
  149. ],
  150. {id: 'default-overview', title: 'Default'}
  151. ),
  152. });
  153. initialData = initializeOrg({
  154. organization: OrganizationFixture({
  155. features: ['global-views', 'dashboards-basic', 'discover-query'],
  156. }),
  157. });
  158. render(
  159. <OrganizationContext.Provider value={initialData.organization}>
  160. <ViewEditDashboard
  161. {...RouteComponentPropsFixture()}
  162. organization={initialData.organization}
  163. params={{orgId: 'org-slug', dashboardId: 'default-overview'}}
  164. router={initialData.router}
  165. location={initialData.router.location}
  166. >
  167. {null}
  168. </ViewEditDashboard>
  169. </OrganizationContext.Provider>,
  170. {router: initialData.router}
  171. );
  172. expect(await screen.findByText('Default Widget 1')).toBeInTheDocument();
  173. expect(screen.getByText('Default Widget 2')).toBeInTheDocument();
  174. });
  175. it('opens the widget viewer modal in a prebuilt dashboard using the widget id specified in the url', async () => {
  176. const openWidgetViewerModal = jest.spyOn(modals, 'openWidgetViewerModal');
  177. render(
  178. <CreateDashboard
  179. {...RouteComponentPropsFixture()}
  180. organization={initialData.organization}
  181. params={{templateId: 'default-template', widgetId: '2'}}
  182. router={initialData.router}
  183. location={{...initialData.router.location, pathname: '/widget/2/'}}
  184. >
  185. {null}
  186. </CreateDashboard>,
  187. {router: initialData.router, organization: initialData.organization}
  188. );
  189. await waitFor(() => {
  190. expect(openWidgetViewerModal).toHaveBeenCalledWith(
  191. expect.objectContaining({
  192. organization: initialData.organization,
  193. widget: expect.objectContaining({
  194. displayType: 'line',
  195. interval: '5m',
  196. queries: [
  197. {
  198. aggregates: ['count()'],
  199. columns: [],
  200. conditions: '!event.type:transaction',
  201. fields: ['count()'],
  202. name: 'Events',
  203. orderby: 'count()',
  204. },
  205. ],
  206. title: 'Events',
  207. widgetType: types.WidgetType.DISCOVER,
  208. }),
  209. onClose: expect.anything(),
  210. })
  211. );
  212. });
  213. });
  214. });
  215. describe('custom dashboards', function () {
  216. let initialData, widgets, mockVisit, mockPut;
  217. beforeEach(function () {
  218. window.confirm = jest.fn();
  219. initialData = initializeOrg({
  220. organization,
  221. router: {
  222. location: LocationFixture(),
  223. },
  224. });
  225. PageFiltersStore.init();
  226. PageFiltersStore.onInitializeUrlState(
  227. {
  228. projects: [],
  229. environments: [],
  230. datetime: {start: null, end: null, period: '14d', utc: null},
  231. },
  232. new Set()
  233. );
  234. widgets = [
  235. WidgetFixture({
  236. queries: [
  237. {
  238. name: '',
  239. conditions: 'event.type:error',
  240. fields: ['count()'],
  241. columns: [],
  242. aggregates: ['count()'],
  243. orderby: '-count()',
  244. },
  245. ],
  246. title: 'Errors',
  247. interval: '1d',
  248. widgetType: types.WidgetType.DISCOVER,
  249. id: '1',
  250. }),
  251. WidgetFixture({
  252. queries: [
  253. {
  254. name: '',
  255. conditions: 'event.type:transaction',
  256. fields: ['count()'],
  257. columns: [],
  258. aggregates: ['count()'],
  259. orderby: '-count()',
  260. },
  261. ],
  262. title: 'Transactions',
  263. interval: '1d',
  264. widgetType: types.WidgetType.DISCOVER,
  265. id: '2',
  266. }),
  267. WidgetFixture({
  268. queries: [
  269. {
  270. name: '',
  271. conditions: 'event.type:transaction transaction:/api/cats',
  272. fields: ['p50()'],
  273. columns: [],
  274. aggregates: ['p50()'],
  275. orderby: '-p50()',
  276. },
  277. ],
  278. title: 'p50 of /api/cats',
  279. interval: '1d',
  280. id: '3',
  281. }),
  282. ];
  283. mockVisit = MockApiClient.addMockResponse({
  284. url: '/organizations/org-slug/dashboards/1/visit/',
  285. method: 'POST',
  286. body: [],
  287. statusCode: 200,
  288. });
  289. MockApiClient.addMockResponse({
  290. url: '/organizations/org-slug/tags/',
  291. body: [],
  292. });
  293. MockApiClient.addMockResponse({
  294. url: '/organizations/org-slug/projects/',
  295. body: [ProjectFixture()],
  296. });
  297. MockApiClient.addMockResponse({
  298. url: '/organizations/org-slug/dashboards/',
  299. body: [
  300. {
  301. ...DashboardFixture([], {
  302. id: 'default-overview',
  303. title: 'Default',
  304. }),
  305. widgetDisplay: ['area'],
  306. },
  307. {
  308. ...DashboardFixture([], {
  309. id: '1',
  310. title: 'Custom Errors',
  311. }),
  312. widgetDisplay: ['area'],
  313. },
  314. ],
  315. });
  316. MockApiClient.addMockResponse({
  317. url: '/organizations/org-slug/dashboards/1/',
  318. body: DashboardFixture(widgets, {
  319. id: '1',
  320. title: 'Custom Errors',
  321. filters: {},
  322. createdBy: UserFixture({id: '1'}),
  323. }),
  324. });
  325. mockPut = MockApiClient.addMockResponse({
  326. url: '/organizations/org-slug/dashboards/1/',
  327. method: 'PUT',
  328. body: DashboardFixture(widgets, {id: '1', title: 'Custom Errors'}),
  329. });
  330. MockApiClient.addMockResponse({
  331. url: '/organizations/org-slug/events-stats/',
  332. body: {data: []},
  333. });
  334. MockApiClient.addMockResponse({
  335. method: 'POST',
  336. url: '/organizations/org-slug/dashboards/widgets/',
  337. body: [],
  338. });
  339. MockApiClient.addMockResponse({
  340. method: 'GET',
  341. url: '/organizations/org-slug/recent-searches/',
  342. body: [],
  343. });
  344. MockApiClient.addMockResponse({
  345. method: 'GET',
  346. url: '/organizations/org-slug/issues/',
  347. body: [],
  348. });
  349. MockApiClient.addMockResponse({
  350. url: '/organizations/org-slug/events/',
  351. method: 'GET',
  352. body: [],
  353. });
  354. MockApiClient.addMockResponse({
  355. url: '/organizations/org-slug/users/',
  356. method: 'GET',
  357. body: [],
  358. });
  359. MockApiClient.addMockResponse({
  360. url: '/organizations/org-slug/releases/',
  361. body: [],
  362. });
  363. MockApiClient.addMockResponse({
  364. url: '/organizations/org-slug/releases/stats/',
  365. body: [],
  366. });
  367. MockApiClient.addMockResponse({
  368. url: '/organizations/org-slug/sdk-updates/',
  369. body: [],
  370. });
  371. MockApiClient.addMockResponse({
  372. url: '/organizations/org-slug/prompts-activity/',
  373. body: {},
  374. });
  375. MockApiClient.addMockResponse({
  376. url: '/organizations/org-slug/metrics/meta/',
  377. body: [],
  378. });
  379. });
  380. afterEach(function () {
  381. MockApiClient.clearMockResponses();
  382. jest.clearAllMocks();
  383. });
  384. it('can remove widgets', async function () {
  385. const updateMock = MockApiClient.addMockResponse({
  386. url: '/organizations/org-slug/dashboards/1/',
  387. method: 'PUT',
  388. body: DashboardFixture([widgets[0]], {id: '1', title: 'Custom Errors'}),
  389. });
  390. render(
  391. <OrganizationContext.Provider value={initialData.organization}>
  392. <ViewEditDashboard
  393. {...RouteComponentPropsFixture()}
  394. organization={initialData.organization}
  395. params={{orgId: 'org-slug', dashboardId: '1'}}
  396. router={initialData.router}
  397. location={initialData.router.location}
  398. >
  399. {null}
  400. </ViewEditDashboard>
  401. </OrganizationContext.Provider>,
  402. {router: initialData.router}
  403. );
  404. await waitFor(() => expect(mockVisit).toHaveBeenCalledTimes(1));
  405. // Enter edit mode.
  406. await userEvent.click(screen.getByRole('button', {name: 'Edit Dashboard'}));
  407. // Remove the second and third widgets
  408. await userEvent.click(
  409. (await screen.findAllByRole('button', {name: 'Delete Widget'}))[1]
  410. );
  411. await userEvent.click(
  412. (await screen.findAllByRole('button', {name: 'Delete Widget'}))[1]
  413. );
  414. // Save changes
  415. await userEvent.click(screen.getByRole('button', {name: 'Save and Finish'}));
  416. expect(updateMock).toHaveBeenCalled();
  417. expect(updateMock).toHaveBeenCalledWith(
  418. '/organizations/org-slug/dashboards/1/',
  419. expect.objectContaining({
  420. data: expect.objectContaining({
  421. title: 'Custom Errors',
  422. widgets: [expect.objectContaining(widgets[0])],
  423. }),
  424. })
  425. );
  426. // Visit should not be called again on dashboard update
  427. expect(mockVisit).toHaveBeenCalledTimes(1);
  428. });
  429. it('appends dashboard-level filters to series request', async function () {
  430. MockApiClient.addMockResponse({
  431. url: '/organizations/org-slug/dashboards/1/',
  432. body: DashboardFixture(widgets, {
  433. id: '1',
  434. title: 'Custom Errors',
  435. filters: {release: ['abc@1.2.0']},
  436. }),
  437. });
  438. const mock = MockApiClient.addMockResponse({
  439. url: '/organizations/org-slug/events-stats/',
  440. body: [],
  441. });
  442. render(
  443. <OrganizationContext.Provider value={initialData.organization}>
  444. <ViewEditDashboard
  445. {...RouteComponentPropsFixture()}
  446. organization={initialData.organization}
  447. params={{orgId: 'org-slug', dashboardId: '1'}}
  448. router={initialData.router}
  449. location={initialData.router.location}
  450. >
  451. {null}
  452. </ViewEditDashboard>
  453. </OrganizationContext.Provider>,
  454. {router: initialData.router}
  455. );
  456. await waitFor(() =>
  457. expect(mock).toHaveBeenLastCalledWith(
  458. '/organizations/org-slug/events-stats/',
  459. expect.objectContaining({
  460. query: expect.objectContaining({
  461. query:
  462. '(event.type:transaction transaction:/api/cats) release:"abc@1.2.0" ',
  463. }),
  464. })
  465. )
  466. );
  467. });
  468. it('shows add widget option', async function () {
  469. render(
  470. <OrganizationContext.Provider value={initialData.organization}>
  471. <ViewEditDashboard
  472. {...RouteComponentPropsFixture()}
  473. organization={initialData.organization}
  474. params={{orgId: 'org-slug', dashboardId: '1'}}
  475. router={initialData.router}
  476. location={initialData.router.location}
  477. >
  478. {null}
  479. </ViewEditDashboard>
  480. </OrganizationContext.Provider>,
  481. {router: initialData.router}
  482. );
  483. // Enter edit mode.
  484. await userEvent.click(screen.getByRole('button', {name: 'Edit Dashboard'}));
  485. expect(await screen.findByRole('button', {name: 'Add widget'})).toBeInTheDocument();
  486. });
  487. it('shows add widget option with dataset selector flag', async function () {
  488. initialData = initializeOrg({
  489. organization: OrganizationFixture({
  490. features: [
  491. 'global-views',
  492. 'dashboards-basic',
  493. 'dashboards-edit',
  494. 'discover-query',
  495. 'custom-metrics',
  496. 'performance-discover-dataset-selector',
  497. ],
  498. }),
  499. });
  500. render(
  501. <OrganizationContext.Provider value={initialData.organization}>
  502. <ViewEditDashboard
  503. {...RouteComponentPropsFixture()}
  504. organization={initialData.organization}
  505. params={{orgId: 'org-slug', dashboardId: '1'}}
  506. router={initialData.router}
  507. location={initialData.router.location}
  508. >
  509. {null}
  510. </ViewEditDashboard>
  511. </OrganizationContext.Provider>,
  512. {router: initialData.router}
  513. );
  514. await userEvent.click(screen.getAllByText('Add Widget')[0]);
  515. const menuOptions = await screen.findAllByTestId('menu-list-item-label');
  516. expect(menuOptions.map(e => e.textContent)).toEqual([
  517. 'Errors',
  518. 'Transactions',
  519. 'Issues',
  520. 'Releases',
  521. 'Metrics',
  522. ]);
  523. });
  524. it('shows add widget option without dataset selector flag', async function () {
  525. initialData = initializeOrg({
  526. organization: OrganizationFixture({
  527. features: [
  528. 'global-views',
  529. 'dashboards-basic',
  530. 'dashboards-edit',
  531. 'discover-query',
  532. 'custom-metrics',
  533. ],
  534. }),
  535. });
  536. render(
  537. <OrganizationContext.Provider value={initialData.organization}>
  538. <ViewEditDashboard
  539. {...RouteComponentPropsFixture()}
  540. organization={initialData.organization}
  541. params={{orgId: 'org-slug', dashboardId: '1'}}
  542. router={initialData.router}
  543. location={initialData.router.location}
  544. >
  545. {null}
  546. </ViewEditDashboard>
  547. </OrganizationContext.Provider>,
  548. {router: initialData.router}
  549. );
  550. await userEvent.click(screen.getAllByText('Add Widget')[0]);
  551. const menuOptions = await screen.findAllByTestId('menu-list-item-label');
  552. expect(menuOptions.map(e => e.textContent)).toEqual([
  553. 'Errors and Transactions',
  554. 'Issues',
  555. 'Releases',
  556. 'Metrics',
  557. ]);
  558. });
  559. it('shows top level release filter', async function () {
  560. const mockReleases = MockApiClient.addMockResponse({
  561. url: '/organizations/org-slug/releases/',
  562. body: [ReleaseFixture()],
  563. });
  564. initialData = initializeOrg({
  565. organization: OrganizationFixture({
  566. features: [
  567. 'global-views',
  568. 'dashboards-basic',
  569. 'dashboards-edit',
  570. 'discover-query',
  571. ],
  572. }),
  573. });
  574. render(
  575. <OrganizationContext.Provider value={initialData.organization}>
  576. <ViewEditDashboard
  577. {...RouteComponentPropsFixture()}
  578. organization={initialData.organization}
  579. params={{orgId: 'org-slug', dashboardId: '1'}}
  580. router={initialData.router}
  581. location={initialData.router.location}
  582. >
  583. {null}
  584. </ViewEditDashboard>
  585. </OrganizationContext.Provider>,
  586. {router: initialData.router}
  587. );
  588. expect(await screen.findByText('All Releases')).toBeInTheDocument();
  589. expect(mockReleases).toHaveBeenCalledTimes(2); // Called once when PageFiltersStore is initialized
  590. });
  591. it('hides add widget option', async function () {
  592. // @ts-expect-error this is assigning to readonly property...
  593. types.MAX_WIDGETS = 1;
  594. render(
  595. <OrganizationContext.Provider value={initialData.organization}>
  596. <ViewEditDashboard
  597. {...RouteComponentPropsFixture()}
  598. organization={initialData.organization}
  599. params={{orgId: 'org-slug', dashboardId: '1'}}
  600. router={initialData.router}
  601. location={initialData.router.location}
  602. >
  603. {null}
  604. </ViewEditDashboard>
  605. </OrganizationContext.Provider>,
  606. {router: initialData.router}
  607. );
  608. // Enter edit mode.
  609. await userEvent.click(await screen.findByRole('button', {name: 'Edit Dashboard'}));
  610. expect(screen.queryByRole('button', {name: 'Add widget'})).not.toBeInTheDocument();
  611. });
  612. it('renders successfully if more widgets than stored layouts', async function () {
  613. // A case where someone has async added widgets to a dashboard
  614. MockApiClient.addMockResponse({
  615. url: '/organizations/org-slug/dashboards/1/',
  616. body: DashboardFixture(
  617. [
  618. WidgetFixture({
  619. queries: [
  620. {
  621. name: '',
  622. conditions: 'event.type:error',
  623. fields: ['count()'],
  624. aggregates: ['count()'],
  625. columns: [],
  626. orderby: '-count()',
  627. },
  628. ],
  629. title: 'First Widget',
  630. interval: '1d',
  631. id: '1',
  632. layout: {x: 0, y: 0, w: 2, h: 6, minH: 0},
  633. }),
  634. WidgetFixture({
  635. queries: [
  636. {
  637. name: '',
  638. conditions: 'event.type:error',
  639. fields: ['count()'],
  640. aggregates: ['count()'],
  641. columns: [],
  642. orderby: '-count()',
  643. },
  644. ],
  645. title: 'Second Widget',
  646. interval: '1d',
  647. id: '2',
  648. }),
  649. ],
  650. {id: '1', title: 'Custom Errors'}
  651. ),
  652. });
  653. render(
  654. <ViewEditDashboard
  655. {...RouteComponentPropsFixture()}
  656. organization={initialData.organization}
  657. params={{orgId: 'org-slug', dashboardId: '1'}}
  658. router={initialData.router}
  659. location={initialData.router.location}
  660. >
  661. {null}
  662. </ViewEditDashboard>,
  663. {router: initialData.router, organization: initialData.organization}
  664. );
  665. expect(await screen.findByText('First Widget')).toBeInTheDocument();
  666. expect(await screen.findByText('Second Widget')).toBeInTheDocument();
  667. });
  668. it('does not trigger request if layout not updated', async () => {
  669. MockApiClient.addMockResponse({
  670. url: '/organizations/org-slug/dashboards/1/',
  671. body: DashboardFixture(
  672. [
  673. WidgetFixture({
  674. queries: [
  675. {
  676. name: '',
  677. conditions: 'event.type:error',
  678. fields: ['count()'],
  679. aggregates: ['count()'],
  680. columns: [],
  681. orderby: '-count()',
  682. },
  683. ],
  684. title: 'First Widget',
  685. interval: '1d',
  686. id: '1',
  687. layout: {x: 0, y: 0, w: 2, h: 6, minH: 0},
  688. }),
  689. ],
  690. {id: '1', title: 'Custom Errors'}
  691. ),
  692. });
  693. render(
  694. <ViewEditDashboard
  695. {...RouteComponentPropsFixture()}
  696. organization={initialData.organization}
  697. params={{orgId: 'org-slug', dashboardId: '1'}}
  698. router={initialData.router}
  699. location={initialData.router.location}
  700. >
  701. {null}
  702. </ViewEditDashboard>,
  703. {router: initialData.router, organization: initialData.organization}
  704. );
  705. await userEvent.click(await screen.findByText('Edit Dashboard'));
  706. await userEvent.click(await screen.findByText('Save and Finish'));
  707. expect(screen.getByText('Edit Dashboard')).toBeInTheDocument();
  708. expect(mockPut).not.toHaveBeenCalled();
  709. });
  710. it('renders the custom resize handler for a widget', async () => {
  711. MockApiClient.addMockResponse({
  712. url: '/organizations/org-slug/dashboards/1/',
  713. body: DashboardFixture(
  714. [
  715. WidgetFixture({
  716. queries: [
  717. {
  718. name: '',
  719. conditions: 'event.type:error',
  720. fields: ['count()'],
  721. aggregates: ['count()'],
  722. columns: [],
  723. orderby: '-count()',
  724. },
  725. ],
  726. title: 'First Widget',
  727. interval: '1d',
  728. id: '1',
  729. layout: {x: 0, y: 0, w: 2, h: 6, minH: 0},
  730. }),
  731. ],
  732. {id: '1', title: 'Custom Errors'}
  733. ),
  734. });
  735. render(
  736. <ViewEditDashboard
  737. {...RouteComponentPropsFixture()}
  738. organization={initialData.organization}
  739. params={{orgId: 'org-slug', dashboardId: '1'}}
  740. router={initialData.router}
  741. location={initialData.router.location}
  742. >
  743. {null}
  744. </ViewEditDashboard>,
  745. {router: initialData.router, organization: initialData.organization}
  746. );
  747. await userEvent.click(await screen.findByText('Edit Dashboard'));
  748. const widget = (await screen.findByText('First Widget')).closest(
  749. '.react-grid-item'
  750. ) as HTMLElement;
  751. const resizeHandle = within(widget).getByTestId('custom-resize-handle');
  752. expect(resizeHandle).toBeVisible();
  753. });
  754. it('does not trigger an alert when the widgets have no layout and user cancels without changes', async () => {
  755. MockApiClient.addMockResponse({
  756. url: '/organizations/org-slug/dashboards/1/',
  757. body: DashboardFixture(
  758. [
  759. WidgetFixture({
  760. queries: [
  761. {
  762. name: '',
  763. conditions: 'event.type:error',
  764. fields: ['count()'],
  765. aggregates: ['count()'],
  766. columns: [],
  767. orderby: '-count()',
  768. },
  769. ],
  770. title: 'First Widget',
  771. interval: '1d',
  772. id: '1',
  773. layout: null,
  774. }),
  775. ],
  776. {id: '1', title: 'Custom Errors'}
  777. ),
  778. });
  779. render(
  780. <ViewEditDashboard
  781. {...RouteComponentPropsFixture()}
  782. organization={initialData.organization}
  783. params={{orgId: 'org-slug', dashboardId: '1'}}
  784. router={initialData.router}
  785. location={initialData.router.location}
  786. >
  787. {null}
  788. </ViewEditDashboard>,
  789. {router: initialData.router, organization: initialData.organization}
  790. );
  791. await userEvent.click(await screen.findByText('Edit Dashboard'));
  792. await userEvent.click(await screen.findByText('Cancel'));
  793. expect(window.confirm).not.toHaveBeenCalled();
  794. });
  795. it('opens the widget viewer modal using the widget id specified in the url', async () => {
  796. const openWidgetViewerModal = jest.spyOn(modals, 'openWidgetViewerModal');
  797. const widget = WidgetFixture({
  798. queries: [
  799. {
  800. name: '',
  801. conditions: 'event.type:error',
  802. fields: ['count()'],
  803. aggregates: ['count()'],
  804. columns: [],
  805. orderby: '',
  806. },
  807. ],
  808. title: 'First Widget',
  809. interval: '1d',
  810. id: '1',
  811. layout: null,
  812. });
  813. MockApiClient.addMockResponse({
  814. url: '/organizations/org-slug/dashboards/1/',
  815. body: DashboardFixture([widget], {id: '1', title: 'Custom Errors'}),
  816. });
  817. render(
  818. <ViewEditDashboard
  819. {...RouteComponentPropsFixture()}
  820. organization={initialData.organization}
  821. params={{orgId: 'org-slug', dashboardId: '1', widgetId: 1}}
  822. router={initialData.router}
  823. location={{...initialData.router.location, pathname: '/widget/123/'}}
  824. >
  825. {null}
  826. </ViewEditDashboard>,
  827. {router: initialData.router, organization: initialData.organization}
  828. );
  829. await waitFor(() => {
  830. expect(openWidgetViewerModal).toHaveBeenCalledWith(
  831. expect.objectContaining({
  832. organization: initialData.organization,
  833. widget,
  834. onClose: expect.anything(),
  835. })
  836. );
  837. });
  838. });
  839. it('redirects user to dashboard url if widget is not found', async () => {
  840. const openWidgetViewerModal = jest.spyOn(modals, 'openWidgetViewerModal');
  841. MockApiClient.addMockResponse({
  842. url: '/organizations/org-slug/dashboards/1/',
  843. body: DashboardFixture([], {id: '1', title: 'Custom Errors'}),
  844. });
  845. render(
  846. <ViewEditDashboard
  847. {...RouteComponentPropsFixture()}
  848. organization={initialData.organization}
  849. params={{orgId: 'org-slug', dashboardId: '1', widgetId: 123}}
  850. router={initialData.router}
  851. location={{...initialData.router.location, pathname: '/widget/123/'}}
  852. >
  853. {null}
  854. </ViewEditDashboard>,
  855. {router: initialData.router, organization: initialData.organization}
  856. );
  857. expect(await screen.findByText('All Releases')).toBeInTheDocument();
  858. expect(openWidgetViewerModal).not.toHaveBeenCalled();
  859. expect(initialData.router.replace).toHaveBeenCalledWith(
  860. expect.objectContaining({
  861. pathname: '/organizations/org-slug/dashboard/1/',
  862. query: {},
  863. })
  864. );
  865. });
  866. it('saves a new dashboard with the page filters', async () => {
  867. const mockPOST = MockApiClient.addMockResponse({
  868. url: '/organizations/org-slug/dashboards/',
  869. method: 'POST',
  870. body: [],
  871. });
  872. render(
  873. <CreateDashboard
  874. {...RouteComponentPropsFixture()}
  875. organization={initialData.organization}
  876. params={{templateId: undefined}}
  877. router={initialData.router}
  878. location={{
  879. ...initialData.router.location,
  880. query: {
  881. ...initialData.router.location.query,
  882. statsPeriod: '7d',
  883. project: [2],
  884. environment: ['alpha', 'beta'],
  885. },
  886. }}
  887. >
  888. {null}
  889. </CreateDashboard>,
  890. {
  891. router: initialData.router,
  892. organization: initialData.organization,
  893. }
  894. );
  895. await userEvent.click(await screen.findByText('Save and Finish'));
  896. expect(mockPOST).toHaveBeenCalledWith(
  897. '/organizations/org-slug/dashboards/',
  898. expect.objectContaining({
  899. data: expect.objectContaining({
  900. projects: [2],
  901. environment: ['alpha', 'beta'],
  902. period: '7d',
  903. }),
  904. })
  905. );
  906. });
  907. it('saves a template with the page filters', async () => {
  908. const mockPOST = MockApiClient.addMockResponse({
  909. url: '/organizations/org-slug/dashboards/',
  910. method: 'POST',
  911. body: [],
  912. });
  913. render(
  914. <CreateDashboard
  915. {...RouteComponentPropsFixture()}
  916. organization={initialData.organization}
  917. params={{templateId: 'default-template'}}
  918. router={initialData.router}
  919. location={{
  920. ...initialData.router.location,
  921. query: {
  922. ...initialData.router.location.query,
  923. statsPeriod: '7d',
  924. project: [2],
  925. environment: ['alpha', 'beta'],
  926. },
  927. }}
  928. >
  929. {null}
  930. </CreateDashboard>,
  931. {
  932. router: initialData.router,
  933. organization: initialData.organization,
  934. }
  935. );
  936. await userEvent.click(await screen.findByText('Add Dashboard'));
  937. expect(mockPOST).toHaveBeenCalledWith(
  938. '/organizations/org-slug/dashboards/',
  939. expect.objectContaining({
  940. data: expect.objectContaining({
  941. projects: [2],
  942. environment: ['alpha', 'beta'],
  943. period: '7d',
  944. }),
  945. })
  946. );
  947. });
  948. it('does not render save and cancel buttons on templates', async () => {
  949. MockApiClient.addMockResponse({
  950. url: '/organizations/org-slug/releases/',
  951. body: [
  952. ReleaseFixture({
  953. shortVersion: 'sentry-android-shop@1.2.0',
  954. version: 'sentry-android-shop@1.2.0',
  955. }),
  956. ],
  957. });
  958. render(
  959. <CreateDashboard
  960. {...RouteComponentPropsFixture()}
  961. organization={initialData.organization}
  962. params={{templateId: 'default-template'}}
  963. router={initialData.router}
  964. location={initialData.router.location}
  965. >
  966. {null}
  967. </CreateDashboard>,
  968. {
  969. router: initialData.router,
  970. organization: initialData.organization,
  971. }
  972. );
  973. await userEvent.click(await screen.findByText('24H'));
  974. await userEvent.click(screen.getByText('Last 7 days'));
  975. await screen.findByText('7D');
  976. expect(screen.queryByText('Cancel')).not.toBeInTheDocument();
  977. expect(screen.queryByText('Save')).not.toBeInTheDocument();
  978. });
  979. it('opens the widget viewer with saved dashboard filters', async () => {
  980. const openWidgetViewerModal = jest.spyOn(modals, 'openWidgetViewerModal');
  981. MockApiClient.addMockResponse({
  982. url: '/organizations/org-slug/dashboards/1/',
  983. body: DashboardFixture(widgets, {
  984. id: '1',
  985. filters: {release: ['sentry-android-shop@1.2.0']},
  986. }),
  987. });
  988. render(
  989. <ViewEditDashboard
  990. {...RouteComponentPropsFixture()}
  991. organization={initialData.organization}
  992. params={{orgId: 'org-slug', dashboardId: '1', widgetId: 1}}
  993. router={initialData.router}
  994. location={{...initialData.router.location, pathname: '/widget/1/'}}
  995. >
  996. {null}
  997. </ViewEditDashboard>,
  998. {router: initialData.router, organization: initialData.organization}
  999. );
  1000. await waitFor(() => {
  1001. expect(openWidgetViewerModal).toHaveBeenCalledWith(
  1002. expect.objectContaining({
  1003. dashboardFilters: {release: ['sentry-android-shop@1.2.0']},
  1004. })
  1005. );
  1006. });
  1007. });
  1008. it('opens the widget viewer with unsaved dashboard filters', async () => {
  1009. const openWidgetViewerModal = jest.spyOn(modals, 'openWidgetViewerModal');
  1010. MockApiClient.addMockResponse({
  1011. url: '/organizations/org-slug/dashboards/1/',
  1012. body: DashboardFixture(widgets, {
  1013. id: '1',
  1014. filters: {release: ['sentry-android-shop@1.2.0']},
  1015. }),
  1016. });
  1017. render(
  1018. <ViewEditDashboard
  1019. {...RouteComponentPropsFixture()}
  1020. organization={initialData.organization}
  1021. params={{orgId: 'org-slug', dashboardId: '1', widgetId: 1}}
  1022. router={initialData.router}
  1023. location={{
  1024. ...initialData.router.location,
  1025. pathname: '/widget/1/',
  1026. query: {release: ['unsaved-release-filter@1.2.0']},
  1027. }}
  1028. >
  1029. {null}
  1030. </ViewEditDashboard>,
  1031. {router: initialData.router, organization: initialData.organization}
  1032. );
  1033. await waitFor(() => {
  1034. expect(openWidgetViewerModal).toHaveBeenCalledWith(
  1035. expect.objectContaining({
  1036. dashboardFilters: {release: ['unsaved-release-filter@1.2.0']},
  1037. })
  1038. );
  1039. });
  1040. });
  1041. it('can save dashboard filters in existing dashboard', async () => {
  1042. MockApiClient.addMockResponse({
  1043. url: '/organizations/org-slug/releases/',
  1044. body: [
  1045. ReleaseFixture({
  1046. shortVersion: 'sentry-android-shop@1.2.0',
  1047. version: 'sentry-android-shop@1.2.0',
  1048. }),
  1049. ],
  1050. });
  1051. const testData = initializeOrg({
  1052. organization: OrganizationFixture({
  1053. features: [
  1054. 'global-views',
  1055. 'dashboards-basic',
  1056. 'dashboards-edit',
  1057. 'discover-query',
  1058. ],
  1059. }),
  1060. router: {
  1061. location: {
  1062. ...LocationFixture(),
  1063. query: {
  1064. statsPeriod: '7d',
  1065. release: ['sentry-android-shop@1.2.0'],
  1066. },
  1067. },
  1068. },
  1069. });
  1070. render(
  1071. <ViewEditDashboard
  1072. {...RouteComponentPropsFixture()}
  1073. organization={testData.organization}
  1074. params={{orgId: 'org-slug', dashboardId: '1'}}
  1075. router={testData.router}
  1076. location={testData.router.location}
  1077. >
  1078. {null}
  1079. </ViewEditDashboard>,
  1080. {router: testData.router, organization: testData.organization}
  1081. );
  1082. await userEvent.click(await screen.findByText('Save'));
  1083. expect(mockPut).toHaveBeenCalledWith(
  1084. '/organizations/org-slug/dashboards/1/',
  1085. expect.objectContaining({
  1086. data: expect.objectContaining({
  1087. period: '7d',
  1088. filters: {release: ['sentry-android-shop@1.2.0']},
  1089. }),
  1090. })
  1091. );
  1092. });
  1093. it('can clear dashboard filters in compact select', async () => {
  1094. MockApiClient.addMockResponse({
  1095. url: '/organizations/org-slug/dashboards/1/',
  1096. body: DashboardFixture(widgets, {
  1097. id: '1',
  1098. title: 'Custom Errors',
  1099. filters: {release: ['sentry-android-shop@1.2.0']},
  1100. }),
  1101. });
  1102. MockApiClient.addMockResponse({
  1103. url: '/organizations/org-slug/releases/',
  1104. body: [
  1105. ReleaseFixture({
  1106. shortVersion: 'sentry-android-shop@1.2.0',
  1107. version: 'sentry-android-shop@1.2.0',
  1108. }),
  1109. ],
  1110. });
  1111. const testData = initializeOrg({
  1112. organization: OrganizationFixture({
  1113. features: [
  1114. 'global-views',
  1115. 'dashboards-basic',
  1116. 'dashboards-edit',
  1117. 'discover-query',
  1118. ],
  1119. }),
  1120. router: {
  1121. location: {
  1122. ...LocationFixture(),
  1123. query: {
  1124. statsPeriod: '7d',
  1125. },
  1126. },
  1127. },
  1128. });
  1129. render(
  1130. <ViewEditDashboard
  1131. {...RouteComponentPropsFixture()}
  1132. organization={testData.organization}
  1133. params={{orgId: 'org-slug', dashboardId: '1'}}
  1134. router={testData.router}
  1135. location={testData.router.location}
  1136. >
  1137. {null}
  1138. </ViewEditDashboard>,
  1139. {router: testData.router, organization: testData.organization}
  1140. );
  1141. await screen.findByText('7D');
  1142. await userEvent.click(await screen.findByText('sentry-android-shop@1.2.0'));
  1143. await userEvent.click(screen.getAllByText('Clear')[0]);
  1144. screen.getByText('All Releases');
  1145. await userEvent.click(document.body);
  1146. await waitFor(() => {
  1147. expect(browserHistory.push).toHaveBeenCalledWith(
  1148. expect.objectContaining({
  1149. query: expect.objectContaining({
  1150. release: '',
  1151. }),
  1152. })
  1153. );
  1154. });
  1155. });
  1156. it('can save absolute time range in existing dashboard', async () => {
  1157. const testData = initializeOrg({
  1158. organization: OrganizationFixture({
  1159. features: [
  1160. 'global-views',
  1161. 'dashboards-basic',
  1162. 'dashboards-edit',
  1163. 'discover-query',
  1164. ],
  1165. }),
  1166. router: {
  1167. location: {
  1168. ...LocationFixture(),
  1169. query: {
  1170. start: '2022-07-14T07:00:00',
  1171. end: '2022-07-19T23:59:59',
  1172. utc: 'true',
  1173. },
  1174. },
  1175. },
  1176. });
  1177. render(
  1178. <ViewEditDashboard
  1179. {...RouteComponentPropsFixture()}
  1180. organization={testData.organization}
  1181. params={{orgId: 'org-slug', dashboardId: '1'}}
  1182. router={testData.router}
  1183. location={testData.router.location}
  1184. >
  1185. {null}
  1186. </ViewEditDashboard>,
  1187. {router: testData.router, organization: testData.organization}
  1188. );
  1189. await userEvent.click(await screen.findByText('Save'));
  1190. expect(mockPut).toHaveBeenCalledWith(
  1191. '/organizations/org-slug/dashboards/1/',
  1192. expect.objectContaining({
  1193. data: expect.objectContaining({
  1194. start: '2022-07-14T07:00:00.000',
  1195. end: '2022-07-19T23:59:59.000',
  1196. utc: true,
  1197. }),
  1198. })
  1199. );
  1200. });
  1201. it('can clear dashboard filters in existing dashboard', async () => {
  1202. MockApiClient.addMockResponse({
  1203. url: '/organizations/org-slug/releases/',
  1204. body: [
  1205. ReleaseFixture({
  1206. shortVersion: 'sentry-android-shop@1.2.0',
  1207. version: 'sentry-android-shop@1.2.0',
  1208. }),
  1209. ],
  1210. });
  1211. const testData = initializeOrg({
  1212. organization: OrganizationFixture({
  1213. features: [
  1214. 'global-views',
  1215. 'dashboards-basic',
  1216. 'dashboards-edit',
  1217. 'discover-query',
  1218. ],
  1219. }),
  1220. router: {
  1221. location: {
  1222. ...LocationFixture(),
  1223. query: {
  1224. statsPeriod: '7d',
  1225. environment: ['alpha', 'beta'],
  1226. },
  1227. },
  1228. },
  1229. });
  1230. render(
  1231. <ViewEditDashboard
  1232. {...RouteComponentPropsFixture()}
  1233. organization={testData.organization}
  1234. params={{orgId: 'org-slug', dashboardId: '1'}}
  1235. router={testData.router}
  1236. location={testData.router.location}
  1237. >
  1238. {null}
  1239. </ViewEditDashboard>,
  1240. {router: testData.router, organization: testData.organization}
  1241. );
  1242. await screen.findByText('7D');
  1243. await userEvent.click(await screen.findByText('All Releases'));
  1244. await userEvent.click(screen.getByText('sentry-android-shop@1.2.0'));
  1245. await userEvent.keyboard('{Escape}');
  1246. await userEvent.click(screen.getByText('Cancel'));
  1247. screen.getByText('All Releases');
  1248. expect(browserHistory.replace).toHaveBeenCalledWith(
  1249. expect.objectContaining({
  1250. query: expect.objectContaining({
  1251. project: undefined,
  1252. statsPeriod: undefined,
  1253. environment: undefined,
  1254. }),
  1255. })
  1256. );
  1257. });
  1258. it('disables the Edit Dashboard button when there are unsaved filters', async () => {
  1259. MockApiClient.addMockResponse({
  1260. url: '/organizations/org-slug/releases/',
  1261. body: [
  1262. ReleaseFixture({
  1263. shortVersion: 'sentry-android-shop@1.2.0',
  1264. version: 'sentry-android-shop@1.2.0',
  1265. }),
  1266. ],
  1267. });
  1268. const testData = initializeOrg({
  1269. organization: OrganizationFixture({
  1270. features: [
  1271. 'global-views',
  1272. 'dashboards-basic',
  1273. 'dashboards-edit',
  1274. 'discover-basic',
  1275. 'discover-query',
  1276. ],
  1277. }),
  1278. router: {
  1279. location: {
  1280. ...LocationFixture(),
  1281. query: {
  1282. statsPeriod: '7d',
  1283. environment: ['alpha', 'beta'],
  1284. },
  1285. },
  1286. },
  1287. });
  1288. render(
  1289. <ViewEditDashboard
  1290. {...RouteComponentPropsFixture()}
  1291. organization={testData.organization}
  1292. params={{orgId: 'org-slug', dashboardId: '1'}}
  1293. router={testData.router}
  1294. location={testData.router.location}
  1295. >
  1296. {null}
  1297. </ViewEditDashboard>,
  1298. {router: testData.router, organization: testData.organization}
  1299. );
  1300. expect(await screen.findByText('Save')).toBeInTheDocument();
  1301. expect(screen.getByText('Cancel')).toBeInTheDocument();
  1302. expect(screen.getByRole('button', {name: 'Edit Dashboard'})).toBeDisabled();
  1303. });
  1304. it('ignores the order of selection of page filters to render unsaved filters', async () => {
  1305. const testProjects = [
  1306. ProjectFixture({id: '1', name: 'first', environments: ['alpha', 'beta']}),
  1307. ProjectFixture({id: '2', name: 'second', environments: ['alpha', 'beta']}),
  1308. ];
  1309. act(() => ProjectsStore.loadInitialData(testProjects));
  1310. MockApiClient.addMockResponse({
  1311. url: '/organizations/org-slug/projects/',
  1312. body: testProjects,
  1313. });
  1314. MockApiClient.addMockResponse({
  1315. url: '/organizations/org-slug/dashboards/1/',
  1316. body: DashboardFixture(widgets, {
  1317. id: '1',
  1318. title: 'Custom Errors',
  1319. filters: {},
  1320. environment: ['alpha', 'beta'],
  1321. }),
  1322. });
  1323. const testData = initializeOrg({
  1324. organization: OrganizationFixture({
  1325. features: [
  1326. 'global-views',
  1327. 'dashboards-basic',
  1328. 'dashboards-edit',
  1329. 'discover-query',
  1330. ],
  1331. }),
  1332. router: {
  1333. location: {
  1334. ...LocationFixture(),
  1335. query: {
  1336. environment: ['beta', 'alpha'], // Reversed order from saved dashboard
  1337. },
  1338. },
  1339. },
  1340. });
  1341. render(
  1342. <ViewEditDashboard
  1343. {...RouteComponentPropsFixture()}
  1344. organization={testData.organization}
  1345. params={{orgId: 'org-slug', dashboardId: '1'}}
  1346. router={testData.router}
  1347. location={testData.router.location}
  1348. >
  1349. {null}
  1350. </ViewEditDashboard>,
  1351. {router: testData.router, organization: testData.organization}
  1352. );
  1353. await waitFor(() => expect(screen.queryAllByText('Loading\u2026')).toEqual([]));
  1354. await userEvent.click(screen.getByRole('button', {name: 'All Envs'}));
  1355. expect(screen.getByRole('row', {name: 'alpha'})).toHaveAttribute(
  1356. 'aria-selected',
  1357. 'true'
  1358. );
  1359. expect(screen.getByRole('row', {name: 'beta'})).toHaveAttribute(
  1360. 'aria-selected',
  1361. 'true'
  1362. );
  1363. // Save and Cancel should not appear because alpha, beta is the same as beta, alpha
  1364. expect(screen.queryByText('Save')).not.toBeInTheDocument();
  1365. expect(screen.queryByText('Cancel')).not.toBeInTheDocument();
  1366. });
  1367. it('uses releases from the URL query params', async function () {
  1368. const testData = initializeOrg({
  1369. organization: OrganizationFixture({
  1370. features: [
  1371. 'global-views',
  1372. 'dashboards-basic',
  1373. 'dashboards-edit',
  1374. 'discover-query',
  1375. ],
  1376. }),
  1377. router: {
  1378. location: {
  1379. ...LocationFixture(),
  1380. query: {
  1381. release: ['not-selected-1'],
  1382. },
  1383. },
  1384. },
  1385. });
  1386. render(
  1387. <ViewEditDashboard
  1388. {...RouteComponentPropsFixture()}
  1389. organization={testData.organization}
  1390. params={{orgId: 'org-slug', dashboardId: '1'}}
  1391. router={testData.router}
  1392. location={testData.router.location}
  1393. >
  1394. {null}
  1395. </ViewEditDashboard>,
  1396. {router: testData.router, organization: testData.organization}
  1397. );
  1398. await screen.findByText(/not-selected-1/);
  1399. screen.getByText('Save');
  1400. screen.getByText('Cancel');
  1401. });
  1402. it('resets release in URL params', async function () {
  1403. MockApiClient.addMockResponse({
  1404. url: '/organizations/org-slug/dashboards/1/',
  1405. body: DashboardFixture(widgets, {
  1406. id: '1',
  1407. title: 'Custom Errors',
  1408. filters: {
  1409. release: ['abc'],
  1410. },
  1411. }),
  1412. });
  1413. const testData = initializeOrg({
  1414. organization: OrganizationFixture({
  1415. features: [
  1416. 'global-views',
  1417. 'dashboards-basic',
  1418. 'dashboards-edit',
  1419. 'discover-query',
  1420. ],
  1421. }),
  1422. router: {
  1423. location: {
  1424. ...LocationFixture(),
  1425. query: {
  1426. release: ['not-selected-1'],
  1427. },
  1428. },
  1429. },
  1430. });
  1431. render(
  1432. <ViewEditDashboard
  1433. {...RouteComponentPropsFixture()}
  1434. organization={testData.organization}
  1435. params={{orgId: 'org-slug', dashboardId: '1'}}
  1436. router={testData.router}
  1437. location={testData.router.location}
  1438. >
  1439. {null}
  1440. </ViewEditDashboard>,
  1441. {router: testData.router, organization: testData.organization}
  1442. );
  1443. await screen.findByText(/not-selected-1/);
  1444. await userEvent.click(screen.getByText('Cancel'));
  1445. // release isn't used in the redirect
  1446. expect(browserHistory.replace).toHaveBeenCalledWith(
  1447. expect.objectContaining({
  1448. query: {
  1449. end: undefined,
  1450. environment: undefined,
  1451. project: undefined,
  1452. start: undefined,
  1453. statsPeriod: undefined,
  1454. utc: undefined,
  1455. },
  1456. })
  1457. );
  1458. });
  1459. it('reflects selections in the release filter in the query params', async function () {
  1460. MockApiClient.addMockResponse({
  1461. url: '/organizations/org-slug/releases/',
  1462. body: [
  1463. ReleaseFixture({
  1464. shortVersion: 'sentry-android-shop@1.2.0',
  1465. version: 'sentry-android-shop@1.2.0',
  1466. }),
  1467. ],
  1468. });
  1469. const testData = initializeOrg({
  1470. organization: OrganizationFixture({
  1471. features: [
  1472. 'global-views',
  1473. 'dashboards-basic',
  1474. 'dashboards-edit',
  1475. 'discover-query',
  1476. ],
  1477. }),
  1478. router: {
  1479. location: LocationFixture(),
  1480. },
  1481. });
  1482. render(
  1483. <ViewEditDashboard
  1484. {...RouteComponentPropsFixture()}
  1485. organization={testData.organization}
  1486. params={{orgId: 'org-slug', dashboardId: '1'}}
  1487. router={testData.router}
  1488. location={testData.router.location}
  1489. >
  1490. {null}
  1491. </ViewEditDashboard>,
  1492. {router: testData.router, organization: testData.organization}
  1493. );
  1494. await userEvent.click(await screen.findByText('All Releases'));
  1495. await userEvent.click(screen.getByText('sentry-android-shop@1.2.0'));
  1496. await userEvent.click(document.body);
  1497. await waitFor(() => {
  1498. expect(browserHistory.push).toHaveBeenCalledWith(
  1499. expect.objectContaining({
  1500. query: expect.objectContaining({
  1501. release: ['sentry-android-shop@1.2.0'],
  1502. }),
  1503. })
  1504. );
  1505. });
  1506. });
  1507. it('persists release selections made during search requests that do not appear in default query', async function () {
  1508. // Default response
  1509. MockApiClient.addMockResponse({
  1510. url: '/organizations/org-slug/releases/',
  1511. body: [
  1512. ReleaseFixture({
  1513. shortVersion: 'sentry-android-shop@1.2.0',
  1514. version: 'sentry-android-shop@1.2.0',
  1515. }),
  1516. ],
  1517. });
  1518. // Mocked search results
  1519. MockApiClient.addMockResponse({
  1520. url: '/organizations/org-slug/releases/',
  1521. body: [
  1522. ReleaseFixture({
  1523. id: '9',
  1524. shortVersion: 'search-result',
  1525. version: 'search-result',
  1526. }),
  1527. ],
  1528. match: [MockApiClient.matchData({query: 's'})],
  1529. });
  1530. const testData = initializeOrg({
  1531. organization: OrganizationFixture({
  1532. features: [
  1533. 'global-views',
  1534. 'dashboards-basic',
  1535. 'dashboards-edit',
  1536. 'discover-basic',
  1537. 'discover-query',
  1538. ],
  1539. }),
  1540. router: {
  1541. location: LocationFixture(),
  1542. },
  1543. });
  1544. render(
  1545. <ViewEditDashboard
  1546. {...RouteComponentPropsFixture()}
  1547. organization={testData.organization}
  1548. params={{orgId: 'org-slug', dashboardId: '1'}}
  1549. router={testData.router}
  1550. location={testData.router.location}
  1551. >
  1552. {null}
  1553. </ViewEditDashboard>,
  1554. {router: testData.router, organization: testData.organization}
  1555. );
  1556. await userEvent.click(await screen.findByText('All Releases'));
  1557. await userEvent.type(screen.getAllByPlaceholderText('Search\u2026')[2], 's');
  1558. await userEvent.click(await screen.findByRole('option', {name: 'search-result'}));
  1559. // Validate that after search is cleared, search result still appears
  1560. expect(await screen.findByText('Latest Release(s)')).toBeInTheDocument();
  1561. expect(screen.getByRole('option', {name: 'search-result'})).toBeInTheDocument();
  1562. });
  1563. it('renders edit access selector', async function () {
  1564. render(
  1565. <EditAccessSelector
  1566. dashboard={DashboardFixture([], {id: '1', title: 'Custom Errors'})}
  1567. onChangeEditAccess={jest.fn()}
  1568. />,
  1569. {
  1570. router: initialData.router,
  1571. organization: {
  1572. features: ['dashboards-edit-access'],
  1573. ...initialData.organization,
  1574. },
  1575. }
  1576. );
  1577. await userEvent.click(await screen.findByText('Edit Access:'));
  1578. expect(screen.getByText('Creator')).toBeInTheDocument();
  1579. expect(screen.getByText('All users')).toBeInTheDocument();
  1580. });
  1581. it('creates and updates new permissions for dashboard with no edit perms initialized', async function () {
  1582. const mockPUT = MockApiClient.addMockResponse({
  1583. url: '/organizations/org-slug/dashboards/1/',
  1584. method: 'PUT',
  1585. body: DashboardFixture([], {id: '1', title: 'Custom Errors'}),
  1586. });
  1587. render(
  1588. <ViewEditDashboard
  1589. {...RouteComponentPropsFixture()}
  1590. organization={{
  1591. ...initialData.organization,
  1592. features: ['dashboards-edit-access', ...initialData.organization.features],
  1593. }}
  1594. params={{orgId: 'org-slug', dashboardId: '1'}}
  1595. router={initialData.router}
  1596. location={initialData.router.location}
  1597. >
  1598. {null}
  1599. </ViewEditDashboard>,
  1600. {
  1601. router: initialData.router,
  1602. organization: {
  1603. features: ['dashboards-edit-access', ...initialData.organization.features],
  1604. },
  1605. }
  1606. );
  1607. await userEvent.click(await screen.findByText('Edit Access:'));
  1608. // deselects 'All users' so only creator has edit access
  1609. expect(await screen.findByText('All users')).toBeEnabled();
  1610. expect(await screen.findByRole('option', {name: 'All users'})).toHaveAttribute(
  1611. 'aria-selected',
  1612. 'true'
  1613. );
  1614. await userEvent.click(screen.getByRole('option', {name: 'All users'}));
  1615. expect(await screen.findByRole('option', {name: 'All users'})).toHaveAttribute(
  1616. 'aria-selected',
  1617. 'false'
  1618. );
  1619. await userEvent.click(await screen.findByText('Save Changes'));
  1620. await waitFor(() => {
  1621. expect(mockPUT).toHaveBeenCalledTimes(1);
  1622. expect(mockPUT).toHaveBeenCalledWith(
  1623. '/organizations/org-slug/dashboards/1/',
  1624. expect.objectContaining({
  1625. data: expect.objectContaining({
  1626. permissions: {isEditableByEveryone: false, teamsWithEditAccess: []},
  1627. }),
  1628. })
  1629. );
  1630. });
  1631. });
  1632. it('creator can update permissions for dashboard', async function () {
  1633. const mockPUT = MockApiClient.addMockResponse({
  1634. url: '/organizations/org-slug/dashboards/1/',
  1635. method: 'PUT',
  1636. body: DashboardFixture([], {id: '1', title: 'Custom Errors'}),
  1637. });
  1638. MockApiClient.addMockResponse({
  1639. url: '/organizations/org-slug/dashboards/1/',
  1640. body: DashboardFixture([], {
  1641. id: '1',
  1642. title: 'Custom Errors',
  1643. createdBy: UserFixture({id: '781629'}),
  1644. permissions: {isEditableByEveryone: false},
  1645. }),
  1646. });
  1647. const currentUser = UserFixture({id: '781629'});
  1648. ConfigStore.set('user', currentUser);
  1649. render(
  1650. <ViewEditDashboard
  1651. {...RouteComponentPropsFixture()}
  1652. organization={{
  1653. ...initialData.organization,
  1654. features: ['dashboards-edit-access', ...initialData.organization.features],
  1655. }}
  1656. params={{orgId: 'org-slug', dashboardId: '1'}}
  1657. router={initialData.router}
  1658. location={initialData.router.location}
  1659. >
  1660. {null}
  1661. </ViewEditDashboard>,
  1662. {
  1663. router: initialData.router,
  1664. organization: {
  1665. features: ['dashboards-edit-access', ...initialData.organization.features],
  1666. },
  1667. }
  1668. );
  1669. await userEvent.click(await screen.findByText('Edit Access:'));
  1670. // selects 'All users' so everyone has edit access
  1671. expect(await screen.findByText('All users')).toBeEnabled();
  1672. expect(await screen.findByRole('option', {name: 'All users'})).toHaveAttribute(
  1673. 'aria-selected',
  1674. 'false'
  1675. );
  1676. await userEvent.click(screen.getByRole('option', {name: 'All users'}));
  1677. expect(await screen.findByRole('option', {name: 'All users'})).toHaveAttribute(
  1678. 'aria-selected',
  1679. 'true'
  1680. );
  1681. await userEvent.click(await screen.findByText('Save Changes'));
  1682. await waitFor(() => {
  1683. expect(mockPUT).toHaveBeenCalledTimes(1);
  1684. expect(mockPUT).toHaveBeenCalledWith(
  1685. '/organizations/org-slug/dashboards/1/',
  1686. expect.objectContaining({
  1687. data: expect.objectContaining({
  1688. permissions: {isEditableByEveryone: true, teamsWithEditAccess: []},
  1689. }),
  1690. })
  1691. );
  1692. });
  1693. });
  1694. it('creator can update permissions with teams for dashboard', async function () {
  1695. const mockPUT = MockApiClient.addMockResponse({
  1696. url: '/organizations/org-slug/dashboards/1/',
  1697. method: 'PUT',
  1698. body: DashboardFixture([], {id: '1', title: 'Custom Errors'}),
  1699. });
  1700. MockApiClient.addMockResponse({
  1701. url: '/organizations/org-slug/dashboards/1/',
  1702. body: DashboardFixture([], {
  1703. id: '1',
  1704. title: 'Custom Errors',
  1705. createdBy: UserFixture({id: '781629'}),
  1706. permissions: {isEditableByEveryone: false},
  1707. }),
  1708. });
  1709. const currentUser = UserFixture({id: '781629'});
  1710. ConfigStore.set('user', currentUser);
  1711. const teamData = [
  1712. {
  1713. id: '1',
  1714. slug: 'team1',
  1715. name: 'Team 1',
  1716. },
  1717. {
  1718. id: '2',
  1719. slug: 'team2',
  1720. name: 'Team 2',
  1721. },
  1722. {
  1723. id: '3',
  1724. slug: 'team3',
  1725. name: 'Team 3',
  1726. },
  1727. ];
  1728. const teams = teamData.map(data => TeamFixture(data));
  1729. TeamStore.loadInitialData(teams);
  1730. render(
  1731. <ViewEditDashboard
  1732. {...RouteComponentPropsFixture()}
  1733. organization={{
  1734. ...initialData.organization,
  1735. features: ['dashboards-edit-access', ...initialData.organization.features],
  1736. }}
  1737. params={{orgId: 'org-slug', dashboardId: '1'}}
  1738. router={initialData.router}
  1739. location={initialData.router.location}
  1740. >
  1741. {null}
  1742. </ViewEditDashboard>,
  1743. {
  1744. router: initialData.router,
  1745. organization: {
  1746. features: ['dashboards-edit-access', ...initialData.organization.features],
  1747. },
  1748. }
  1749. );
  1750. await userEvent.click(await screen.findByText('Edit Access:'));
  1751. expect(await screen.findByText('All users')).toBeEnabled();
  1752. expect(await screen.findByRole('option', {name: 'All users'})).toHaveAttribute(
  1753. 'aria-selected',
  1754. 'false'
  1755. );
  1756. await userEvent.click(screen.getByRole('option', {name: '#team1'}));
  1757. await userEvent.click(screen.getByRole('option', {name: '#team2'}));
  1758. await userEvent.click(await screen.findByText('Save Changes'));
  1759. await waitFor(() => {
  1760. expect(mockPUT).toHaveBeenCalledTimes(1);
  1761. expect(mockPUT).toHaveBeenCalledWith(
  1762. '/organizations/org-slug/dashboards/1/',
  1763. expect.objectContaining({
  1764. data: expect.objectContaining({
  1765. permissions: {isEditableByEveryone: false, teamsWithEditAccess: [1, 2]},
  1766. }),
  1767. })
  1768. );
  1769. });
  1770. });
  1771. it('disables edit dashboard and add widget button if user cannot edit dashboard', async function () {
  1772. MockApiClient.addMockResponse({
  1773. url: '/organizations/org-slug/dashboards/',
  1774. body: [
  1775. DashboardFixture([], {
  1776. id: '1',
  1777. title: 'Custom Errors',
  1778. createdBy: UserFixture({id: '238900'}),
  1779. permissions: {isEditableByEveryone: false},
  1780. }),
  1781. ],
  1782. });
  1783. MockApiClient.addMockResponse({
  1784. url: '/organizations/org-slug/dashboards/1/',
  1785. body: DashboardFixture([], {
  1786. id: '1',
  1787. title: 'Custom Errors',
  1788. createdBy: UserFixture({id: '238900'}),
  1789. permissions: {isEditableByEveryone: false},
  1790. }),
  1791. });
  1792. const currentUser = UserFixture({id: '781629', isSuperuser: false});
  1793. ConfigStore.set('user', currentUser);
  1794. render(
  1795. <ViewEditDashboard
  1796. {...RouteComponentPropsFixture()}
  1797. organization={{
  1798. ...initialData.organization,
  1799. features: ['dashboards-edit-access', ...initialData.organization.features],
  1800. access: ['org:read'],
  1801. }}
  1802. params={{orgId: 'org-slug', dashboardId: '1'}}
  1803. router={initialData.router}
  1804. location={initialData.router.location}
  1805. >
  1806. {null}
  1807. </ViewEditDashboard>,
  1808. {
  1809. router: initialData.router,
  1810. organization: {
  1811. features: ['dashboards-edit-access', ...initialData.organization.features],
  1812. access: ['org:read'],
  1813. },
  1814. }
  1815. );
  1816. await screen.findByText('Edit Access:');
  1817. expect(screen.getByRole('button', {name: 'Edit Dashboard'})).toBeDisabled();
  1818. expect(screen.getByRole('button', {name: 'Add Widget'})).toBeDisabled();
  1819. });
  1820. it('disables widget edit, duplicate, and delete button when user does not have edit perms', async function () {
  1821. const widget = {
  1822. displayType: types.DisplayType.TABLE,
  1823. interval: '1d',
  1824. queries: [
  1825. {
  1826. name: 'Test Widget',
  1827. fields: ['count()', 'count_unique(user)', 'epm()', 'project'],
  1828. columns: ['project'],
  1829. aggregates: ['count()', 'count_unique(user)', 'epm()'],
  1830. conditions: '',
  1831. orderby: '',
  1832. },
  1833. ],
  1834. title: 'Transactions',
  1835. id: '1',
  1836. widgetType: types.WidgetType.DISCOVER,
  1837. };
  1838. const mockDashboard = DashboardFixture([widget], {
  1839. id: '1',
  1840. title: 'Custom Errors',
  1841. createdBy: UserFixture({id: '238900'}),
  1842. permissions: {isEditableByEveryone: false},
  1843. });
  1844. MockApiClient.addMockResponse({
  1845. url: '/organizations/org-slug/dashboards/',
  1846. body: mockDashboard,
  1847. });
  1848. MockApiClient.addMockResponse({
  1849. url: '/organizations/org-slug/dashboards/1/',
  1850. body: mockDashboard,
  1851. });
  1852. const currentUser = UserFixture({id: '781629'});
  1853. ConfigStore.set('user', currentUser);
  1854. render(
  1855. <ViewEditDashboard
  1856. {...RouteComponentPropsFixture()}
  1857. organization={{
  1858. ...initialData.organization,
  1859. features: ['dashboards-edit-access', ...initialData.organization.features],
  1860. access: ['org:read'],
  1861. }}
  1862. params={{orgId: 'org-slug', dashboardId: '1'}}
  1863. router={initialData.router}
  1864. location={initialData.router.location}
  1865. >
  1866. {null}
  1867. </ViewEditDashboard>,
  1868. {
  1869. router: initialData.router,
  1870. organization: {
  1871. features: ['dashboards-edit-access', ...initialData.organization.features],
  1872. access: ['org:read'],
  1873. },
  1874. }
  1875. );
  1876. await screen.findByText('Edit Access:');
  1877. expect(screen.getByRole('button', {name: 'Edit Dashboard'})).toBeDisabled();
  1878. expect(screen.getByRole('button', {name: 'Add Widget'})).toBeDisabled();
  1879. await userEvent.click(await screen.findByLabelText('Widget actions'));
  1880. expect(
  1881. screen.getByRole('menuitemradio', {name: 'Duplicate Widget'})
  1882. ).toHaveAttribute('aria-disabled', 'true');
  1883. expect(screen.getByRole('menuitemradio', {name: 'Delete Widget'})).toHaveAttribute(
  1884. 'aria-disabled',
  1885. 'true'
  1886. );
  1887. expect(screen.getByRole('menuitemradio', {name: 'Edit Widget'})).toHaveAttribute(
  1888. 'aria-disabled',
  1889. 'true'
  1890. );
  1891. });
  1892. describe('widget builder redesign', function () {
  1893. beforeEach(function () {
  1894. initialData = initializeOrg({
  1895. organization: OrganizationFixture({
  1896. features: [
  1897. 'global-views',
  1898. 'dashboards-basic',
  1899. 'dashboards-edit',
  1900. 'discover-query',
  1901. 'performance-discover-dataset-selector',
  1902. 'dashboards-widget-builder-redesign',
  1903. ],
  1904. }),
  1905. });
  1906. });
  1907. it('opens the widget builder slideout when clicking add widget', async function () {
  1908. render(
  1909. <DashboardDetail
  1910. {...RouteComponentPropsFixture()}
  1911. initialState={DashboardState.VIEW}
  1912. dashboard={DashboardFixture([])}
  1913. dashboards={[]}
  1914. onDashboardUpdate={jest.fn()}
  1915. newWidget={undefined}
  1916. onSetNewWidget={() => {}}
  1917. />,
  1918. {organization: initialData.organization}
  1919. );
  1920. await userEvent.click(await screen.findByRole('button', {name: 'Add Widget'}));
  1921. expect(await screen.findByText('Create Custom Widget')).toBeInTheDocument();
  1922. });
  1923. it('opens the widget builder slideout when clicking add widget in edit mode', async function () {
  1924. render(
  1925. <DashboardDetail
  1926. {...RouteComponentPropsFixture()}
  1927. initialState={DashboardState.EDIT}
  1928. dashboard={DashboardFixture([])}
  1929. dashboards={[]}
  1930. onDashboardUpdate={jest.fn()}
  1931. newWidget={undefined}
  1932. onSetNewWidget={() => {}}
  1933. />,
  1934. {organization: initialData.organization}
  1935. );
  1936. await userEvent.click(await screen.findByTestId('widget-add'));
  1937. expect(await screen.findByText('Create Custom Widget')).toBeInTheDocument();
  1938. });
  1939. });
  1940. describe('discover split', function () {
  1941. it('calls the dashboard callbacks with the correct widgetType for discover split', function () {
  1942. const widget = {
  1943. displayType: types.DisplayType.TABLE,
  1944. interval: '1d',
  1945. queries: [
  1946. {
  1947. name: 'Test Widget',
  1948. fields: ['count()', 'count_unique(user)', 'epm()', 'project'],
  1949. columns: ['project'],
  1950. aggregates: ['count()', 'count_unique(user)', 'epm()'],
  1951. conditions: '',
  1952. orderby: '',
  1953. },
  1954. ],
  1955. title: 'Transactions',
  1956. id: '1',
  1957. widgetType: types.WidgetType.DISCOVER,
  1958. };
  1959. const mockDashboard = DashboardFixture([widget], {
  1960. id: '1',
  1961. title: 'Custom Errors',
  1962. });
  1963. const mockModifiedDashboard = DashboardFixture([widget], {
  1964. id: '1',
  1965. title: 'Custom Errors',
  1966. });
  1967. const mockOnDashboardUpdate = jest.fn();
  1968. const mockStateSetter = jest
  1969. .fn()
  1970. .mockImplementation(fn => fn({modifiedDashboard: mockModifiedDashboard}));
  1971. handleUpdateDashboardSplit({
  1972. widgetId: '1',
  1973. splitDecision: types.WidgetType.ERRORS,
  1974. dashboard: mockDashboard,
  1975. modifiedDashboard: mockModifiedDashboard,
  1976. onDashboardUpdate: mockOnDashboardUpdate,
  1977. stateSetter: mockStateSetter,
  1978. });
  1979. expect(mockOnDashboardUpdate).toHaveBeenCalledWith({
  1980. ...mockDashboard,
  1981. widgets: [{...widget, widgetType: types.WidgetType.ERRORS}],
  1982. });
  1983. expect(mockStateSetter).toHaveReturnedWith({
  1984. modifiedDashboard: {
  1985. ...mockModifiedDashboard,
  1986. widgets: [{...widget, widgetType: types.WidgetType.ERRORS}],
  1987. },
  1988. });
  1989. });
  1990. });
  1991. });
  1992. });