detail.spec.tsx 62 KB

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