detail.spec.tsx 59 KB

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