detail.spec.jsx 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  1. import {browserHistory} from 'react-router';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {
  4. act,
  5. render,
  6. screen,
  7. userEvent,
  8. waitFor,
  9. within,
  10. } from 'sentry-test/reactTestingLibrary';
  11. import * as modals from 'sentry/actionCreators/modal';
  12. import ProjectsStore from 'sentry/stores/projectsStore';
  13. import CreateDashboard from 'sentry/views/dashboards/create';
  14. import * as types from 'sentry/views/dashboards/types';
  15. import ViewEditDashboard from 'sentry/views/dashboards/view';
  16. import {OrganizationContext} from 'sentry/views/organizationContext';
  17. jest.mock('sentry/components/charts/worldMapChart', () => ({
  18. WorldMapChart: () => null,
  19. }));
  20. describe('Dashboards > Detail', function () {
  21. const organization = TestStubs.Organization({
  22. features: ['global-views', 'dashboards-basic', 'dashboards-edit', 'discover-query'],
  23. });
  24. const projects = [TestStubs.Project()];
  25. describe('prebuilt dashboards', function () {
  26. let initialData;
  27. beforeEach(function () {
  28. act(() => ProjectsStore.loadInitialData(projects));
  29. initialData = initializeOrg({organization});
  30. MockApiClient.addMockResponse({
  31. url: '/organizations/org-slug/tags/',
  32. body: [],
  33. });
  34. MockApiClient.addMockResponse({
  35. url: '/organizations/org-slug/projects/',
  36. body: [TestStubs.Project()],
  37. });
  38. MockApiClient.addMockResponse({
  39. url: '/organizations/org-slug/dashboards/',
  40. body: [
  41. TestStubs.Dashboard([], {id: 'default-overview', title: 'Default'}),
  42. TestStubs.Dashboard([], {id: '1', title: 'Custom Errors'}),
  43. ],
  44. });
  45. MockApiClient.addMockResponse({
  46. url: '/organizations/org-slug/dashboards/default-overview/',
  47. body: TestStubs.Dashboard([], {id: 'default-overview', title: 'Default'}),
  48. });
  49. MockApiClient.addMockResponse({
  50. url: '/organizations/org-slug/dashboards/1/visit/',
  51. method: 'POST',
  52. body: [],
  53. statusCode: 200,
  54. });
  55. MockApiClient.addMockResponse({
  56. url: '/organizations/org-slug/users/',
  57. method: 'GET',
  58. body: [],
  59. });
  60. MockApiClient.addMockResponse({
  61. url: '/organizations/org-slug/sdk-updates/',
  62. body: [],
  63. });
  64. MockApiClient.addMockResponse({
  65. url: '/prompts-activity/',
  66. body: {},
  67. });
  68. MockApiClient.addMockResponse({
  69. url: '/organizations/org-slug/events-geo/',
  70. body: {data: [], meta: {}},
  71. });
  72. MockApiClient.addMockResponse({
  73. url: '/organizations/org-slug/events/',
  74. method: 'GET',
  75. body: [],
  76. });
  77. MockApiClient.addMockResponse({
  78. url: '/organizations/org-slug/events-stats/',
  79. body: {data: []},
  80. });
  81. MockApiClient.addMockResponse({
  82. method: 'GET',
  83. url: '/organizations/org-slug/issues/',
  84. body: [],
  85. });
  86. MockApiClient.addMockResponse({
  87. url: '/organizations/org-slug/releases/',
  88. body: [],
  89. });
  90. });
  91. afterEach(function () {
  92. MockApiClient.clearMockResponses();
  93. });
  94. it('assigns unique IDs to all widgets so grid keys are unique', async function () {
  95. MockApiClient.addMockResponse({
  96. url: '/organizations/org-slug/events-stats/',
  97. body: {data: []},
  98. });
  99. MockApiClient.addMockResponse({
  100. url: '/organizations/org-slug/dashboards/default-overview/',
  101. body: TestStubs.Dashboard(
  102. [
  103. TestStubs.Widget(
  104. [
  105. {
  106. name: '',
  107. conditions: 'event.type:error',
  108. fields: ['count()'],
  109. aggregates: ['count()'],
  110. columns: [],
  111. },
  112. ],
  113. {
  114. title: 'Default Widget 1',
  115. interval: '1d',
  116. }
  117. ),
  118. TestStubs.Widget(
  119. [
  120. {
  121. name: '',
  122. conditions: 'event.type:transaction',
  123. fields: ['count()'],
  124. aggregates: ['count()'],
  125. columns: [],
  126. },
  127. ],
  128. {
  129. title: 'Default Widget 2',
  130. interval: '1d',
  131. }
  132. ),
  133. ],
  134. {id: 'default-overview', title: 'Default'}
  135. ),
  136. });
  137. initialData = initializeOrg({
  138. organization: TestStubs.Organization({
  139. features: ['global-views', 'dashboards-basic', 'discover-query'],
  140. projects: [TestStubs.Project()],
  141. }),
  142. });
  143. render(
  144. <OrganizationContext.Provider value={initialData.organization}>
  145. <ViewEditDashboard
  146. organization={initialData.organization}
  147. params={{orgId: 'org-slug', dashboardId: 'default-overview'}}
  148. router={initialData.router}
  149. location={initialData.router.location}
  150. />
  151. </OrganizationContext.Provider>,
  152. {context: initialData.routerContext}
  153. );
  154. expect(await screen.findByText('Default Widget 1')).toBeInTheDocument();
  155. expect(screen.getByText('Default Widget 2')).toBeInTheDocument();
  156. });
  157. it('opens the widget viewer modal in a prebuilt dashboard using the widget id specified in the url', async () => {
  158. const openWidgetViewerModal = jest.spyOn(modals, 'openWidgetViewerModal');
  159. render(
  160. <CreateDashboard
  161. organization={initialData.organization}
  162. params={{templateId: 'default-template', widgetId: '2'}}
  163. router={initialData.router}
  164. location={{...initialData.router.location, pathname: '/widget/2/'}}
  165. />,
  166. {context: initialData.routerContext, organization: initialData.organization}
  167. );
  168. await waitFor(() => {
  169. expect(openWidgetViewerModal).toHaveBeenCalledWith(
  170. expect.objectContaining({
  171. organization: initialData.organization,
  172. widget: expect.objectContaining({
  173. displayType: 'line',
  174. interval: '5m',
  175. queries: [
  176. {
  177. aggregates: ['count()'],
  178. columns: [],
  179. conditions: '!event.type:transaction',
  180. fields: ['count()'],
  181. name: 'Events',
  182. orderby: 'count()',
  183. },
  184. ],
  185. title: 'Events',
  186. widgetType: 'discover',
  187. }),
  188. onClose: expect.anything(),
  189. })
  190. );
  191. });
  192. });
  193. });
  194. describe('custom dashboards', function () {
  195. let initialData, widgets, mockVisit, mockPut;
  196. beforeEach(function () {
  197. window.confirm = jest.fn();
  198. initialData = initializeOrg({
  199. organization,
  200. router: {
  201. location: TestStubs.location(),
  202. },
  203. });
  204. widgets = [
  205. TestStubs.Widget(
  206. [
  207. {
  208. name: '',
  209. conditions: 'event.type:error',
  210. fields: ['count()'],
  211. columns: [],
  212. aggregates: ['count()'],
  213. },
  214. ],
  215. {
  216. title: 'Errors',
  217. interval: '1d',
  218. widgetType: 'discover',
  219. id: '1',
  220. }
  221. ),
  222. TestStubs.Widget(
  223. [
  224. {
  225. name: '',
  226. conditions: 'event.type:transaction',
  227. fields: ['count()'],
  228. columns: [],
  229. aggregates: ['count()'],
  230. },
  231. ],
  232. {
  233. title: 'Transactions',
  234. interval: '1d',
  235. widgetType: 'discover',
  236. id: '2',
  237. }
  238. ),
  239. TestStubs.Widget(
  240. [
  241. {
  242. name: '',
  243. conditions: 'event.type:transaction transaction:/api/cats',
  244. fields: ['p50()'],
  245. columns: [],
  246. aggregates: ['p50()'],
  247. },
  248. ],
  249. {
  250. title: 'p50 of /api/cats',
  251. interval: '1d',
  252. id: '3',
  253. }
  254. ),
  255. ];
  256. mockVisit = MockApiClient.addMockResponse({
  257. url: '/organizations/org-slug/dashboards/1/visit/',
  258. method: 'POST',
  259. body: [],
  260. statusCode: 200,
  261. });
  262. MockApiClient.addMockResponse({
  263. url: '/organizations/org-slug/tags/',
  264. body: [],
  265. });
  266. MockApiClient.addMockResponse({
  267. url: '/organizations/org-slug/projects/',
  268. body: [TestStubs.Project()],
  269. });
  270. MockApiClient.addMockResponse({
  271. url: '/organizations/org-slug/dashboards/',
  272. body: [
  273. TestStubs.Dashboard([], {
  274. id: 'default-overview',
  275. title: 'Default',
  276. widgetDisplay: ['area'],
  277. }),
  278. TestStubs.Dashboard([], {
  279. id: '1',
  280. title: 'Custom Errors',
  281. widgetDisplay: ['area'],
  282. }),
  283. ],
  284. });
  285. MockApiClient.addMockResponse({
  286. url: '/organizations/org-slug/dashboards/1/',
  287. body: TestStubs.Dashboard(widgets, {
  288. id: '1',
  289. title: 'Custom Errors',
  290. filters: {},
  291. }),
  292. });
  293. mockPut = MockApiClient.addMockResponse({
  294. url: '/organizations/org-slug/dashboards/1/',
  295. method: 'PUT',
  296. body: TestStubs.Dashboard(widgets, {id: '1', title: 'Custom Errors'}),
  297. });
  298. MockApiClient.addMockResponse({
  299. url: '/organizations/org-slug/events-stats/',
  300. body: {data: []},
  301. });
  302. MockApiClient.addMockResponse({
  303. method: 'POST',
  304. url: '/organizations/org-slug/dashboards/widgets/',
  305. body: [],
  306. });
  307. MockApiClient.addMockResponse({
  308. method: 'GET',
  309. url: '/organizations/org-slug/recent-searches/',
  310. body: [],
  311. });
  312. MockApiClient.addMockResponse({
  313. method: 'GET',
  314. url: '/organizations/org-slug/issues/',
  315. body: [],
  316. });
  317. MockApiClient.addMockResponse({
  318. url: '/organizations/org-slug/events/',
  319. method: 'GET',
  320. body: [],
  321. });
  322. MockApiClient.addMockResponse({
  323. url: '/organizations/org-slug/users/',
  324. method: 'GET',
  325. body: [],
  326. });
  327. MockApiClient.addMockResponse({
  328. url: '/organizations/org-slug/events-geo/',
  329. body: {data: [], meta: {}},
  330. });
  331. MockApiClient.addMockResponse({
  332. url: '/organizations/org-slug/releases/',
  333. body: [],
  334. });
  335. MockApiClient.addMockResponse({
  336. url: '/organizations/org-slug/sdk-updates/',
  337. body: [],
  338. });
  339. MockApiClient.addMockResponse({
  340. url: '/prompts-activity/',
  341. body: {},
  342. });
  343. });
  344. afterEach(function () {
  345. MockApiClient.clearMockResponses();
  346. jest.clearAllMocks();
  347. });
  348. it('can remove widgets', async function () {
  349. const updateMock = MockApiClient.addMockResponse({
  350. url: '/organizations/org-slug/dashboards/1/',
  351. method: 'PUT',
  352. body: TestStubs.Dashboard([widgets[0]], {id: '1', title: 'Custom Errors'}),
  353. });
  354. render(
  355. <OrganizationContext.Provider value={initialData.organization}>
  356. <ViewEditDashboard
  357. organization={initialData.organization}
  358. params={{orgId: 'org-slug', dashboardId: '1'}}
  359. router={initialData.router}
  360. location={initialData.router.location}
  361. />
  362. </OrganizationContext.Provider>,
  363. {context: initialData.routerContext}
  364. );
  365. await waitFor(() => expect(mockVisit).toHaveBeenCalledTimes(1));
  366. // Enter edit mode.
  367. await userEvent.click(screen.getByRole('button', {name: 'Edit Dashboard'}));
  368. // Remove the second and third widgets
  369. await userEvent.click(screen.getAllByRole('button', {name: 'Delete Widget'})[1]);
  370. await userEvent.click(screen.getAllByRole('button', {name: 'Delete Widget'})[1]);
  371. // Save changes
  372. await userEvent.click(screen.getByRole('button', {name: 'Save and Finish'}));
  373. expect(updateMock).toHaveBeenCalled();
  374. expect(updateMock).toHaveBeenCalledWith(
  375. '/organizations/org-slug/dashboards/1/',
  376. expect.objectContaining({
  377. data: expect.objectContaining({
  378. title: 'Custom Errors',
  379. widgets: [expect.objectContaining(widgets[0])],
  380. }),
  381. })
  382. );
  383. // Visit should not be called again on dashboard update
  384. expect(mockVisit).toHaveBeenCalledTimes(1);
  385. });
  386. it('appends dashboard-level filters to series request', async function () {
  387. MockApiClient.addMockResponse({
  388. url: '/organizations/org-slug/dashboards/1/',
  389. body: TestStubs.Dashboard(widgets, {
  390. id: '1',
  391. title: 'Custom Errors',
  392. filters: {release: ['abc@1.2.0']},
  393. }),
  394. });
  395. const mock = MockApiClient.addMockResponse({
  396. url: '/organizations/org-slug/events-stats/',
  397. body: [],
  398. });
  399. render(
  400. <OrganizationContext.Provider value={initialData.organization}>
  401. <ViewEditDashboard
  402. organization={initialData.organization}
  403. params={{orgId: 'org-slug', dashboardId: '1'}}
  404. router={initialData.router}
  405. location={initialData.router.location}
  406. />
  407. </OrganizationContext.Provider>,
  408. {context: initialData.routerContext}
  409. );
  410. await waitFor(() =>
  411. expect(mock).toHaveBeenLastCalledWith(
  412. '/organizations/org-slug/events-stats/',
  413. expect.objectContaining({
  414. query: expect.objectContaining({
  415. query: 'event.type:transaction transaction:/api/cats release:abc@1.2.0 ',
  416. }),
  417. })
  418. )
  419. );
  420. });
  421. it('shows add widget option', async function () {
  422. render(
  423. <OrganizationContext.Provider value={initialData.organization}>
  424. <ViewEditDashboard
  425. organization={initialData.organization}
  426. params={{orgId: 'org-slug', dashboardId: '1'}}
  427. router={initialData.router}
  428. location={initialData.router.location}
  429. />
  430. </OrganizationContext.Provider>,
  431. {context: initialData.routerContext}
  432. );
  433. // Enter edit mode.
  434. await userEvent.click(screen.getByRole('button', {name: 'Edit Dashboard'}));
  435. expect(await screen.findByRole('button', {name: 'Add widget'})).toBeInTheDocument();
  436. });
  437. it('shows top level release filter', async function () {
  438. const mockReleases = MockApiClient.addMockResponse({
  439. url: '/organizations/org-slug/releases/',
  440. body: [TestStubs.Release()],
  441. });
  442. initialData = initializeOrg({
  443. organization: TestStubs.Organization({
  444. features: [
  445. 'global-views',
  446. 'dashboards-basic',
  447. 'dashboards-edit',
  448. 'discover-query',
  449. ],
  450. projects: [TestStubs.Project()],
  451. }),
  452. });
  453. render(
  454. <OrganizationContext.Provider value={initialData.organization}>
  455. <ViewEditDashboard
  456. organization={initialData.organization}
  457. params={{orgId: 'org-slug', dashboardId: '1'}}
  458. router={initialData.router}
  459. location={initialData.router.location}
  460. />
  461. </OrganizationContext.Provider>,
  462. {context: initialData.routerContext}
  463. );
  464. expect(await screen.findByText('All Releases')).toBeInTheDocument();
  465. expect(mockReleases).toHaveBeenCalledTimes(1);
  466. });
  467. it('hides add widget option', async function () {
  468. types.MAX_WIDGETS = 1;
  469. render(
  470. <OrganizationContext.Provider value={initialData.organization}>
  471. <ViewEditDashboard
  472. organization={initialData.organization}
  473. params={{orgId: 'org-slug', dashboardId: '1'}}
  474. router={initialData.router}
  475. location={initialData.router.location}
  476. />
  477. </OrganizationContext.Provider>,
  478. {context: initialData.routerContext}
  479. );
  480. // Enter edit mode.
  481. await userEvent.click(await screen.findByRole('button', {name: 'Edit Dashboard'}));
  482. expect(screen.queryByRole('button', {name: 'Add widget'})).not.toBeInTheDocument();
  483. });
  484. it('renders successfully if more widgets than stored layouts', async function () {
  485. // A case where someone has async added widgets to a dashboard
  486. MockApiClient.addMockResponse({
  487. url: '/organizations/org-slug/dashboards/1/',
  488. body: TestStubs.Dashboard(
  489. [
  490. TestStubs.Widget(
  491. [
  492. {
  493. name: '',
  494. conditions: 'event.type:error',
  495. fields: ['count()'],
  496. aggregates: ['count()'],
  497. columns: [],
  498. },
  499. ],
  500. {
  501. title: 'First Widget',
  502. interval: '1d',
  503. id: '1',
  504. layout: {i: 'grid-item-1', x: 0, y: 0, w: 2, h: 6},
  505. }
  506. ),
  507. TestStubs.Widget(
  508. [
  509. {
  510. name: '',
  511. conditions: 'event.type:error',
  512. fields: ['count()'],
  513. aggregates: ['count()'],
  514. columns: [],
  515. },
  516. ],
  517. {
  518. title: 'Second Widget',
  519. interval: '1d',
  520. id: '2',
  521. }
  522. ),
  523. ],
  524. {id: '1', title: 'Custom Errors'}
  525. ),
  526. });
  527. render(
  528. <ViewEditDashboard
  529. organization={initialData.organization}
  530. params={{orgId: 'org-slug', dashboardId: '1'}}
  531. router={initialData.router}
  532. location={initialData.router.location}
  533. />,
  534. {context: initialData.routerContext, organization: initialData.organization}
  535. );
  536. expect(await screen.findByText('First Widget')).toBeInTheDocument();
  537. expect(await screen.findByText('Second Widget')).toBeInTheDocument();
  538. });
  539. it('does not trigger request if layout not updated', async () => {
  540. MockApiClient.addMockResponse({
  541. url: '/organizations/org-slug/dashboards/1/',
  542. body: TestStubs.Dashboard(
  543. [
  544. TestStubs.Widget(
  545. [
  546. {
  547. name: '',
  548. conditions: 'event.type:error',
  549. fields: ['count()'],
  550. aggregates: ['count()'],
  551. columns: [],
  552. },
  553. ],
  554. {
  555. title: 'First Widget',
  556. interval: '1d',
  557. id: '1',
  558. layout: {i: 'grid-item-1', x: 0, y: 0, w: 2, h: 6},
  559. }
  560. ),
  561. ],
  562. {id: '1', title: 'Custom Errors'}
  563. ),
  564. });
  565. render(
  566. <ViewEditDashboard
  567. organization={initialData.organization}
  568. params={{orgId: 'org-slug', dashboardId: '1'}}
  569. router={initialData.router}
  570. location={initialData.router.location}
  571. />,
  572. {context: initialData.routerContext, organization: initialData.organization}
  573. );
  574. await userEvent.click(await screen.findByText('Edit Dashboard'));
  575. await userEvent.click(await screen.findByText('Save and Finish'));
  576. expect(screen.getByText('Edit Dashboard')).toBeInTheDocument();
  577. expect(mockPut).not.toHaveBeenCalled();
  578. });
  579. it('renders the custom resize handler for a widget', async () => {
  580. MockApiClient.addMockResponse({
  581. url: '/organizations/org-slug/dashboards/1/',
  582. body: TestStubs.Dashboard(
  583. [
  584. TestStubs.Widget(
  585. [
  586. {
  587. name: '',
  588. conditions: 'event.type:error',
  589. fields: ['count()'],
  590. aggregates: ['count()'],
  591. columns: [],
  592. },
  593. ],
  594. {
  595. title: 'First Widget',
  596. interval: '1d',
  597. id: '1',
  598. layout: {i: 'grid-item-1', x: 0, y: 0, w: 2, h: 6},
  599. }
  600. ),
  601. ],
  602. {id: '1', title: 'Custom Errors'}
  603. ),
  604. });
  605. render(
  606. <ViewEditDashboard
  607. organization={initialData.organization}
  608. params={{orgId: 'org-slug', dashboardId: '1'}}
  609. router={initialData.router}
  610. location={initialData.router.location}
  611. />,
  612. {context: initialData.routerContext, organization: initialData.organization}
  613. );
  614. await userEvent.click(await screen.findByText('Edit Dashboard'));
  615. const widget = screen.getByText('First Widget').closest('.react-grid-item');
  616. const resizeHandle = within(widget).getByTestId('custom-resize-handle');
  617. expect(resizeHandle).toBeVisible();
  618. });
  619. it('does not trigger an alert when the widgets have no layout and user cancels without changes', async () => {
  620. MockApiClient.addMockResponse({
  621. url: '/organizations/org-slug/dashboards/1/',
  622. body: TestStubs.Dashboard(
  623. [
  624. TestStubs.Widget(
  625. [
  626. {
  627. name: '',
  628. conditions: 'event.type:error',
  629. fields: ['count()'],
  630. aggregates: ['count()'],
  631. columns: [],
  632. },
  633. ],
  634. {
  635. title: 'First Widget',
  636. interval: '1d',
  637. id: '1',
  638. layout: null,
  639. }
  640. ),
  641. ],
  642. {id: '1', title: 'Custom Errors'}
  643. ),
  644. });
  645. render(
  646. <ViewEditDashboard
  647. organization={initialData.organization}
  648. params={{orgId: 'org-slug', dashboardId: '1'}}
  649. router={initialData.router}
  650. location={initialData.router.location}
  651. />,
  652. {context: initialData.routerContext, organization: initialData.organization}
  653. );
  654. await userEvent.click(await screen.findByText('Edit Dashboard'));
  655. await userEvent.click(await screen.findByText('Cancel'));
  656. expect(window.confirm).not.toHaveBeenCalled();
  657. });
  658. it('opens the widget viewer modal using the widget id specified in the url', async () => {
  659. const openWidgetViewerModal = jest.spyOn(modals, 'openWidgetViewerModal');
  660. const widget = TestStubs.Widget(
  661. [
  662. {
  663. name: '',
  664. conditions: 'event.type:error',
  665. fields: ['count()'],
  666. aggregates: ['count()'],
  667. columns: [],
  668. orderby: '',
  669. },
  670. ],
  671. {
  672. title: 'First Widget',
  673. interval: '1d',
  674. id: '1',
  675. layout: null,
  676. }
  677. );
  678. MockApiClient.addMockResponse({
  679. url: '/organizations/org-slug/dashboards/1/',
  680. body: TestStubs.Dashboard([widget], {id: '1', title: 'Custom Errors'}),
  681. });
  682. render(
  683. <ViewEditDashboard
  684. organization={initialData.organization}
  685. params={{orgId: 'org-slug', dashboardId: '1', widgetId: '1'}}
  686. router={initialData.router}
  687. location={{...initialData.router.location, pathname: '/widget/123/'}}
  688. />,
  689. {context: initialData.routerContext, organization: initialData.organization}
  690. );
  691. await waitFor(() => {
  692. expect(openWidgetViewerModal).toHaveBeenCalledWith(
  693. expect.objectContaining({
  694. organization: initialData.organization,
  695. widget,
  696. onClose: expect.anything(),
  697. })
  698. );
  699. });
  700. });
  701. it('redirects user to dashboard url if widget is not found', async () => {
  702. const openWidgetViewerModal = jest.spyOn(modals, 'openWidgetViewerModal');
  703. MockApiClient.addMockResponse({
  704. url: '/organizations/org-slug/dashboards/1/',
  705. body: TestStubs.Dashboard([], {id: '1', title: 'Custom Errors'}),
  706. });
  707. render(
  708. <ViewEditDashboard
  709. organization={initialData.organization}
  710. params={{orgId: 'org-slug', dashboardId: '1', widgetId: '123'}}
  711. router={initialData.router}
  712. location={{...initialData.router.location, pathname: '/widget/123/'}}
  713. />,
  714. {context: initialData.routerContext, organization: initialData.organization}
  715. );
  716. expect(await screen.findByText('All Releases')).toBeInTheDocument();
  717. expect(openWidgetViewerModal).not.toHaveBeenCalled();
  718. expect(initialData.router.replace).toHaveBeenCalledWith(
  719. expect.objectContaining({
  720. pathname: '/organizations/org-slug/dashboard/1/',
  721. query: {},
  722. })
  723. );
  724. });
  725. it('saves a new dashboard with the page filters', async () => {
  726. const mockPOST = MockApiClient.addMockResponse({
  727. url: '/organizations/org-slug/dashboards/',
  728. method: 'POST',
  729. body: [],
  730. });
  731. render(
  732. <CreateDashboard
  733. organization={initialData.organization}
  734. params={{orgId: 'org-slug'}}
  735. router={initialData.router}
  736. location={{
  737. ...initialData.router.location,
  738. query: {
  739. ...initialData.router.location.query,
  740. statsPeriod: '7d',
  741. project: [2],
  742. environment: ['alpha', 'beta'],
  743. },
  744. }}
  745. />,
  746. {
  747. context: initialData.routerContext,
  748. organization: initialData.organization,
  749. }
  750. );
  751. await userEvent.click(await screen.findByText('Save and Finish'));
  752. expect(mockPOST).toHaveBeenCalledWith(
  753. '/organizations/org-slug/dashboards/',
  754. expect.objectContaining({
  755. data: expect.objectContaining({
  756. projects: [2],
  757. environment: ['alpha', 'beta'],
  758. period: '7d',
  759. }),
  760. })
  761. );
  762. });
  763. it('saves a template with the page filters', async () => {
  764. const mockPOST = MockApiClient.addMockResponse({
  765. url: '/organizations/org-slug/dashboards/',
  766. method: 'POST',
  767. body: [],
  768. });
  769. render(
  770. <CreateDashboard
  771. organization={initialData.organization}
  772. params={{orgId: 'org-slug', templateId: 'default-template'}}
  773. router={initialData.router}
  774. location={{
  775. ...initialData.router.location,
  776. query: {
  777. ...initialData.router.location.query,
  778. statsPeriod: '7d',
  779. project: [2],
  780. environment: ['alpha', 'beta'],
  781. },
  782. }}
  783. />,
  784. {
  785. context: initialData.routerContext,
  786. organization: initialData.organization,
  787. }
  788. );
  789. await userEvent.click(await screen.findByText('Add Dashboard'));
  790. expect(mockPOST).toHaveBeenCalledWith(
  791. '/organizations/org-slug/dashboards/',
  792. expect.objectContaining({
  793. data: expect.objectContaining({
  794. projects: [2],
  795. environment: ['alpha', 'beta'],
  796. period: '7d',
  797. }),
  798. })
  799. );
  800. });
  801. it('does not render save and cancel buttons on templates', async () => {
  802. MockApiClient.addMockResponse({
  803. url: '/organizations/org-slug/releases/',
  804. body: [
  805. TestStubs.Release({
  806. shortVersion: 'sentry-android-shop@1.2.0',
  807. version: 'sentry-android-shop@1.2.0',
  808. }),
  809. ],
  810. });
  811. render(
  812. <CreateDashboard
  813. organization={initialData.organization}
  814. params={{orgId: 'org-slug', templateId: 'default-template'}}
  815. router={initialData.router}
  816. location={initialData.router.location}
  817. />,
  818. {
  819. context: initialData.routerContext,
  820. organization: initialData.organization,
  821. }
  822. );
  823. await userEvent.click(await screen.findByText('24H'));
  824. await userEvent.click(screen.getByText('Last 7 days'));
  825. await screen.findByText('7D');
  826. expect(screen.queryByText('Cancel')).not.toBeInTheDocument();
  827. expect(screen.queryByText('Save')).not.toBeInTheDocument();
  828. });
  829. it('can save dashboard filters in existing dashboard', async () => {
  830. MockApiClient.addMockResponse({
  831. url: '/organizations/org-slug/releases/',
  832. body: [
  833. TestStubs.Release({
  834. shortVersion: 'sentry-android-shop@1.2.0',
  835. version: 'sentry-android-shop@1.2.0',
  836. }),
  837. ],
  838. });
  839. const testData = initializeOrg({
  840. organization: TestStubs.Organization({
  841. features: [
  842. 'global-views',
  843. 'dashboards-basic',
  844. 'dashboards-edit',
  845. 'discover-query',
  846. ],
  847. }),
  848. router: {
  849. location: {
  850. ...TestStubs.location(),
  851. query: {
  852. statsPeriod: '7d',
  853. release: ['sentry-android-shop@1.2.0'],
  854. },
  855. },
  856. },
  857. });
  858. render(
  859. <ViewEditDashboard
  860. organization={testData.organization}
  861. params={{orgId: 'org-slug', dashboardId: '1'}}
  862. router={testData.router}
  863. location={testData.router.location}
  864. />,
  865. {context: testData.routerContext, organization: testData.organization}
  866. );
  867. await userEvent.click(await screen.findByText('Save'));
  868. expect(mockPut).toHaveBeenCalledWith(
  869. '/organizations/org-slug/dashboards/1/',
  870. expect.objectContaining({
  871. data: expect.objectContaining({
  872. period: '7d',
  873. filters: {release: ['sentry-android-shop@1.2.0']},
  874. }),
  875. })
  876. );
  877. });
  878. it('can clear dashboard filters in compact select', async () => {
  879. MockApiClient.addMockResponse({
  880. url: '/organizations/org-slug/dashboards/1/',
  881. body: TestStubs.Dashboard(widgets, {
  882. id: '1',
  883. title: 'Custom Errors',
  884. filters: {release: ['sentry-android-shop@1.2.0']},
  885. }),
  886. });
  887. MockApiClient.addMockResponse({
  888. url: '/organizations/org-slug/releases/',
  889. body: [
  890. TestStubs.Release({
  891. shortVersion: 'sentry-android-shop@1.2.0',
  892. version: 'sentry-android-shop@1.2.0',
  893. }),
  894. ],
  895. });
  896. const testData = initializeOrg({
  897. organization: TestStubs.Organization({
  898. features: [
  899. 'global-views',
  900. 'dashboards-basic',
  901. 'dashboards-edit',
  902. 'discover-query',
  903. ],
  904. }),
  905. router: {
  906. location: {
  907. ...TestStubs.location(),
  908. query: {
  909. statsPeriod: '7d',
  910. },
  911. },
  912. },
  913. });
  914. render(
  915. <ViewEditDashboard
  916. organization={testData.organization}
  917. params={{orgId: 'org-slug', dashboardId: '1'}}
  918. router={testData.router}
  919. location={testData.router.location}
  920. />,
  921. {context: testData.routerContext, organization: testData.organization}
  922. );
  923. await screen.findByText('7D');
  924. await userEvent.click(await screen.findByText('sentry-android-shop@1.2.0'));
  925. await userEvent.click(screen.getByText('Clear'));
  926. screen.getByText('All Releases');
  927. await userEvent.click(document.body);
  928. expect(browserHistory.push).toHaveBeenCalledWith(
  929. expect.objectContaining({
  930. query: expect.objectContaining({
  931. release: '',
  932. }),
  933. })
  934. );
  935. });
  936. it('can save absolute time range in existing dashboard', async () => {
  937. const testData = initializeOrg({
  938. organization: TestStubs.Organization({
  939. features: [
  940. 'global-views',
  941. 'dashboards-basic',
  942. 'dashboards-edit',
  943. 'discover-query',
  944. ],
  945. }),
  946. router: {
  947. location: {
  948. ...TestStubs.location(),
  949. query: {
  950. start: '2022-07-14T07:00:00',
  951. end: '2022-07-19T23:59:59',
  952. utc: 'true',
  953. },
  954. },
  955. },
  956. });
  957. render(
  958. <ViewEditDashboard
  959. organization={testData.organization}
  960. params={{orgId: 'org-slug', dashboardId: '1'}}
  961. router={testData.router}
  962. location={testData.router.location}
  963. />,
  964. {context: testData.routerContext, organization: testData.organization}
  965. );
  966. await userEvent.click(await screen.findByText('Save'));
  967. expect(mockPut).toHaveBeenCalledWith(
  968. '/organizations/org-slug/dashboards/1/',
  969. expect.objectContaining({
  970. data: expect.objectContaining({
  971. start: '2022-07-14T07:00:00.000',
  972. end: '2022-07-19T23:59:59.000',
  973. utc: true,
  974. }),
  975. })
  976. );
  977. });
  978. it('can clear dashboard filters in existing dashboard', async () => {
  979. MockApiClient.addMockResponse({
  980. url: '/organizations/org-slug/releases/',
  981. body: [
  982. TestStubs.Release({
  983. shortVersion: 'sentry-android-shop@1.2.0',
  984. version: 'sentry-android-shop@1.2.0',
  985. }),
  986. ],
  987. });
  988. const testData = initializeOrg({
  989. organization: TestStubs.Organization({
  990. features: [
  991. 'global-views',
  992. 'dashboards-basic',
  993. 'dashboards-edit',
  994. 'discover-query',
  995. ],
  996. }),
  997. router: {
  998. location: {
  999. ...TestStubs.location(),
  1000. query: {
  1001. statsPeriod: '7d',
  1002. project: [1, 2],
  1003. environment: ['alpha', 'beta'],
  1004. },
  1005. },
  1006. },
  1007. });
  1008. render(
  1009. <ViewEditDashboard
  1010. organization={testData.organization}
  1011. params={{orgId: 'org-slug', dashboardId: '1'}}
  1012. router={testData.router}
  1013. location={testData.router.location}
  1014. />,
  1015. {context: testData.routerContext, organization: testData.organization}
  1016. );
  1017. await screen.findByText('7D');
  1018. await userEvent.click(await screen.findByText('All Releases'));
  1019. await userEvent.click(screen.getByText('sentry-android-shop@1.2.0'));
  1020. await userEvent.keyboard('{Escape}');
  1021. await userEvent.click(screen.getByText('Cancel'));
  1022. screen.getByText('All Releases');
  1023. expect(browserHistory.replace).toHaveBeenCalledWith(
  1024. expect.objectContaining({
  1025. query: expect.objectContaining({
  1026. project: undefined,
  1027. statsPeriod: undefined,
  1028. environment: undefined,
  1029. }),
  1030. })
  1031. );
  1032. });
  1033. it('disables the Edit Dashboard button when there are unsaved filters', async () => {
  1034. MockApiClient.addMockResponse({
  1035. url: '/organizations/org-slug/releases/',
  1036. body: [
  1037. TestStubs.Release({
  1038. shortVersion: 'sentry-android-shop@1.2.0',
  1039. version: 'sentry-android-shop@1.2.0',
  1040. }),
  1041. ],
  1042. });
  1043. const testData = initializeOrg({
  1044. organization: TestStubs.Organization({
  1045. features: [
  1046. 'global-views',
  1047. 'dashboards-basic',
  1048. 'dashboards-edit',
  1049. 'discover-basic',
  1050. 'discover-query',
  1051. ],
  1052. }),
  1053. router: {
  1054. location: {
  1055. ...TestStubs.location(),
  1056. query: {
  1057. statsPeriod: '7d',
  1058. project: [1, 2],
  1059. environment: ['alpha', 'beta'],
  1060. },
  1061. },
  1062. },
  1063. });
  1064. render(
  1065. <ViewEditDashboard
  1066. organization={testData.organization}
  1067. params={{orgId: 'org-slug', dashboardId: '1'}}
  1068. router={testData.router}
  1069. location={testData.router.location}
  1070. />,
  1071. {context: testData.routerContext, organization: testData.organization}
  1072. );
  1073. expect(await screen.findByText('Save')).toBeInTheDocument();
  1074. expect(screen.getByText('Cancel')).toBeInTheDocument();
  1075. expect(screen.getByRole('button', {name: 'Edit Dashboard'})).toBeDisabled();
  1076. });
  1077. it('ignores the order of selection of page filters to render unsaved filters', async () => {
  1078. const testProjects = [
  1079. TestStubs.Project({id: '1', name: 'first', environments: ['alpha', 'beta']}),
  1080. TestStubs.Project({id: '2', name: 'second', environments: ['alpha', 'beta']}),
  1081. ];
  1082. act(() => ProjectsStore.loadInitialData(testProjects));
  1083. MockApiClient.addMockResponse({
  1084. url: '/organizations/org-slug/projects/',
  1085. body: testProjects,
  1086. });
  1087. MockApiClient.addMockResponse({
  1088. url: '/organizations/org-slug/dashboards/1/',
  1089. body: TestStubs.Dashboard(widgets, {
  1090. id: '1',
  1091. title: 'Custom Errors',
  1092. filters: {},
  1093. environment: ['alpha', 'beta'],
  1094. }),
  1095. });
  1096. const testData = initializeOrg({
  1097. organization: TestStubs.Organization({
  1098. features: [
  1099. 'global-views',
  1100. 'dashboards-basic',
  1101. 'dashboards-edit',
  1102. 'discover-query',
  1103. ],
  1104. }),
  1105. router: {
  1106. location: {
  1107. ...TestStubs.location(),
  1108. query: {
  1109. environment: ['beta', 'alpha'], // Reversed order from saved dashboard
  1110. },
  1111. },
  1112. },
  1113. });
  1114. render(
  1115. <ViewEditDashboard
  1116. organization={testData.organization}
  1117. params={{orgId: 'org-slug', dashboardId: '1'}}
  1118. router={testData.router}
  1119. location={testData.router.location}
  1120. />,
  1121. {context: testData.routerContext, organization: testData.organization}
  1122. );
  1123. await waitFor(() => expect(screen.queryAllByText('Loading\u2026')).toEqual([]));
  1124. await screen.findByText(/beta, alpha/);
  1125. // Save and Cancel should not appear because alpha, beta is the same as beta, alpha
  1126. expect(screen.queryByText('Save')).not.toBeInTheDocument();
  1127. expect(screen.queryByText('Cancel')).not.toBeInTheDocument();
  1128. });
  1129. it('uses releases from the URL query params', async function () {
  1130. const testData = initializeOrg({
  1131. organization: TestStubs.Organization({
  1132. features: [
  1133. 'global-views',
  1134. 'dashboards-basic',
  1135. 'dashboards-edit',
  1136. 'discover-query',
  1137. ],
  1138. }),
  1139. router: {
  1140. location: {
  1141. ...TestStubs.location(),
  1142. query: {
  1143. release: ['not-selected-1'],
  1144. },
  1145. },
  1146. },
  1147. });
  1148. render(
  1149. <ViewEditDashboard
  1150. organization={testData.organization}
  1151. params={{orgId: 'org-slug', dashboardId: '1'}}
  1152. router={testData.router}
  1153. location={testData.router.location}
  1154. />,
  1155. {context: testData.routerContext, organization: testData.organization}
  1156. );
  1157. await screen.findByText(/not-selected-1/);
  1158. screen.getByText('Save');
  1159. screen.getByText('Cancel');
  1160. });
  1161. it('resets release in URL params', async function () {
  1162. MockApiClient.addMockResponse({
  1163. url: '/organizations/org-slug/dashboards/1/',
  1164. body: TestStubs.Dashboard(widgets, {
  1165. id: '1',
  1166. title: 'Custom Errors',
  1167. filters: {
  1168. release: ['abc'],
  1169. },
  1170. }),
  1171. });
  1172. const testData = initializeOrg({
  1173. organization: TestStubs.Organization({
  1174. features: [
  1175. 'global-views',
  1176. 'dashboards-basic',
  1177. 'dashboards-edit',
  1178. 'discover-query',
  1179. ],
  1180. }),
  1181. router: {
  1182. location: {
  1183. ...TestStubs.location(),
  1184. query: {
  1185. release: ['not-selected-1'],
  1186. },
  1187. },
  1188. },
  1189. });
  1190. render(
  1191. <ViewEditDashboard
  1192. organization={testData.organization}
  1193. params={{orgId: 'org-slug', dashboardId: '1'}}
  1194. router={testData.router}
  1195. location={testData.router.location}
  1196. />,
  1197. {context: testData.routerContext, organization: testData.organization}
  1198. );
  1199. await screen.findByText(/not-selected-1/);
  1200. await userEvent.click(screen.getByText('Cancel'));
  1201. // release isn't used in the redirect
  1202. expect(browserHistory.replace).toHaveBeenCalledWith(
  1203. expect.objectContaining({
  1204. query: {
  1205. end: undefined,
  1206. environment: undefined,
  1207. project: undefined,
  1208. start: undefined,
  1209. statsPeriod: undefined,
  1210. utc: undefined,
  1211. },
  1212. })
  1213. );
  1214. });
  1215. it('reflects selections in the release filter in the query params', async function () {
  1216. MockApiClient.addMockResponse({
  1217. url: '/organizations/org-slug/releases/',
  1218. body: [
  1219. TestStubs.Release({
  1220. shortVersion: 'sentry-android-shop@1.2.0',
  1221. version: 'sentry-android-shop@1.2.0',
  1222. }),
  1223. ],
  1224. });
  1225. const testData = initializeOrg({
  1226. organization: TestStubs.Organization({
  1227. features: [
  1228. 'global-views',
  1229. 'dashboards-basic',
  1230. 'dashboards-edit',
  1231. 'discover-query',
  1232. ],
  1233. }),
  1234. router: {
  1235. location: TestStubs.location(),
  1236. },
  1237. });
  1238. render(
  1239. <ViewEditDashboard
  1240. organization={testData.organization}
  1241. params={{orgId: 'org-slug', dashboardId: '1'}}
  1242. router={testData.router}
  1243. location={testData.router.location}
  1244. />,
  1245. {context: testData.routerContext, organization: testData.organization}
  1246. );
  1247. await userEvent.click(await screen.findByText('All Releases'));
  1248. await userEvent.click(screen.getByText('sentry-android-shop@1.2.0'));
  1249. await userEvent.click(document.body);
  1250. expect(browserHistory.push).toHaveBeenCalledWith(
  1251. expect.objectContaining({
  1252. query: expect.objectContaining({
  1253. release: ['sentry-android-shop@1.2.0'],
  1254. }),
  1255. })
  1256. );
  1257. });
  1258. it('persists release selections made during search requests that do not appear in default query', async function () {
  1259. // Default response
  1260. MockApiClient.addMockResponse({
  1261. url: '/organizations/org-slug/releases/',
  1262. body: [
  1263. TestStubs.Release({
  1264. shortVersion: 'sentry-android-shop@1.2.0',
  1265. version: 'sentry-android-shop@1.2.0',
  1266. }),
  1267. ],
  1268. });
  1269. // Mocked search results
  1270. MockApiClient.addMockResponse({
  1271. url: '/organizations/org-slug/releases/',
  1272. body: [
  1273. TestStubs.Release({
  1274. id: '9',
  1275. shortVersion: 'search-result',
  1276. version: 'search-result',
  1277. }),
  1278. ],
  1279. match: [MockApiClient.matchData({query: 's'})],
  1280. });
  1281. const testData = initializeOrg({
  1282. organization: TestStubs.Organization({
  1283. features: [
  1284. 'global-views',
  1285. 'dashboards-basic',
  1286. 'dashboards-edit',
  1287. 'discover-basic',
  1288. 'discover-query',
  1289. ],
  1290. }),
  1291. router: {
  1292. location: TestStubs.location(),
  1293. },
  1294. });
  1295. render(
  1296. <ViewEditDashboard
  1297. organization={testData.organization}
  1298. params={{orgId: 'org-slug', dashboardId: '1'}}
  1299. router={testData.router}
  1300. location={testData.router.location}
  1301. />,
  1302. {context: testData.routerContext, organization: testData.organization}
  1303. );
  1304. await userEvent.click(await screen.findByText('All Releases'));
  1305. await userEvent.type(screen.getByPlaceholderText('Search\u2026'), 's');
  1306. await userEvent.click(await screen.findByRole('option', {name: 'search-result'}));
  1307. // Validate that after search is cleared, search result still appears
  1308. expect(await screen.findByText('Latest Release(s)')).toBeInTheDocument();
  1309. expect(screen.getByRole('option', {name: 'search-result'})).toBeInTheDocument();
  1310. });
  1311. });
  1312. });