detail.spec.jsx 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  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. userEvent.click(screen.getByRole('button', {name: 'Edit Dashboard'}));
  368. // Remove the second and third widgets
  369. userEvent.click(screen.getAllByRole('button', {name: 'Delete Widget'})[1]);
  370. userEvent.click(screen.getAllByRole('button', {name: 'Delete Widget'})[1]);
  371. // Save changes
  372. 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. 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. 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. await act(async () => {
  528. render(
  529. <ViewEditDashboard
  530. organization={initialData.organization}
  531. params={{orgId: 'org-slug', dashboardId: '1'}}
  532. router={initialData.router}
  533. location={initialData.router.location}
  534. />,
  535. {context: initialData.routerContext, organization: initialData.organization}
  536. );
  537. await tick();
  538. });
  539. await screen.findByText('First Widget');
  540. await screen.findByText('Second Widget');
  541. });
  542. it('does not trigger request if layout not updated', async () => {
  543. MockApiClient.addMockResponse({
  544. url: '/organizations/org-slug/dashboards/1/',
  545. body: TestStubs.Dashboard(
  546. [
  547. TestStubs.Widget(
  548. [
  549. {
  550. name: '',
  551. conditions: 'event.type:error',
  552. fields: ['count()'],
  553. aggregates: ['count()'],
  554. columns: [],
  555. },
  556. ],
  557. {
  558. title: 'First Widget',
  559. interval: '1d',
  560. id: '1',
  561. layout: {i: 'grid-item-1', x: 0, y: 0, w: 2, h: 6},
  562. }
  563. ),
  564. ],
  565. {id: '1', title: 'Custom Errors'}
  566. ),
  567. });
  568. await act(async () => {
  569. render(
  570. <ViewEditDashboard
  571. organization={initialData.organization}
  572. params={{orgId: 'org-slug', dashboardId: '1'}}
  573. router={initialData.router}
  574. location={initialData.router.location}
  575. />,
  576. {context: initialData.routerContext, organization: initialData.organization}
  577. );
  578. await tick();
  579. userEvent.click(screen.getByText('Edit Dashboard'));
  580. userEvent.click(screen.getByText('Save and Finish'));
  581. await tick();
  582. });
  583. expect(screen.getByText('Edit Dashboard')).toBeInTheDocument();
  584. expect(mockPut).not.toHaveBeenCalled();
  585. });
  586. it('renders the custom resize handler for a widget', async () => {
  587. MockApiClient.addMockResponse({
  588. url: '/organizations/org-slug/dashboards/1/',
  589. body: TestStubs.Dashboard(
  590. [
  591. TestStubs.Widget(
  592. [
  593. {
  594. name: '',
  595. conditions: 'event.type:error',
  596. fields: ['count()'],
  597. aggregates: ['count()'],
  598. columns: [],
  599. },
  600. ],
  601. {
  602. title: 'First Widget',
  603. interval: '1d',
  604. id: '1',
  605. layout: {i: 'grid-item-1', x: 0, y: 0, w: 2, h: 6},
  606. }
  607. ),
  608. ],
  609. {id: '1', title: 'Custom Errors'}
  610. ),
  611. });
  612. await act(async () => {
  613. render(
  614. <ViewEditDashboard
  615. organization={initialData.organization}
  616. params={{orgId: 'org-slug', dashboardId: '1'}}
  617. router={initialData.router}
  618. location={initialData.router.location}
  619. />,
  620. {context: initialData.routerContext, organization: initialData.organization}
  621. );
  622. await tick();
  623. });
  624. userEvent.click(await screen.findByText('Edit Dashboard'));
  625. const widget = screen.getByText('First Widget').closest('.react-grid-item');
  626. const resizeHandle = within(widget).getByTestId('custom-resize-handle');
  627. expect(resizeHandle).toBeVisible();
  628. });
  629. it('does not trigger an alert when the widgets have no layout and user cancels without changes', async () => {
  630. MockApiClient.addMockResponse({
  631. url: '/organizations/org-slug/dashboards/1/',
  632. body: TestStubs.Dashboard(
  633. [
  634. TestStubs.Widget(
  635. [
  636. {
  637. name: '',
  638. conditions: 'event.type:error',
  639. fields: ['count()'],
  640. aggregates: ['count()'],
  641. columns: [],
  642. },
  643. ],
  644. {
  645. title: 'First Widget',
  646. interval: '1d',
  647. id: '1',
  648. layout: null,
  649. }
  650. ),
  651. ],
  652. {id: '1', title: 'Custom Errors'}
  653. ),
  654. });
  655. await act(async () => {
  656. render(
  657. <ViewEditDashboard
  658. organization={initialData.organization}
  659. params={{orgId: 'org-slug', dashboardId: '1'}}
  660. router={initialData.router}
  661. location={initialData.router.location}
  662. />,
  663. {context: initialData.routerContext, organization: initialData.organization}
  664. );
  665. await tick();
  666. });
  667. await act(async () => {
  668. userEvent.click(await screen.findByText('Edit Dashboard'));
  669. userEvent.click(await screen.findByText('Cancel'));
  670. });
  671. expect(window.confirm).not.toHaveBeenCalled();
  672. });
  673. it('opens the widget viewer modal using the widget id specified in the url', async () => {
  674. const openWidgetViewerModal = jest.spyOn(modals, 'openWidgetViewerModal');
  675. const widget = TestStubs.Widget(
  676. [
  677. {
  678. name: '',
  679. conditions: 'event.type:error',
  680. fields: ['count()'],
  681. aggregates: ['count()'],
  682. columns: [],
  683. orderby: '',
  684. },
  685. ],
  686. {
  687. title: 'First Widget',
  688. interval: '1d',
  689. id: '1',
  690. layout: null,
  691. }
  692. );
  693. MockApiClient.addMockResponse({
  694. url: '/organizations/org-slug/dashboards/1/',
  695. body: TestStubs.Dashboard([widget], {id: '1', title: 'Custom Errors'}),
  696. });
  697. await act(async () => {
  698. render(
  699. <ViewEditDashboard
  700. organization={initialData.organization}
  701. params={{orgId: 'org-slug', dashboardId: '1', widgetId: '1'}}
  702. router={initialData.router}
  703. location={{...initialData.router.location, pathname: '/widget/123/'}}
  704. />,
  705. {context: initialData.routerContext, organization: initialData.organization}
  706. );
  707. await tick();
  708. });
  709. expect(openWidgetViewerModal).toHaveBeenCalledWith(
  710. expect.objectContaining({
  711. organization: initialData.organization,
  712. widget,
  713. onClose: expect.anything(),
  714. })
  715. );
  716. });
  717. it('redirects user to dashboard url if widget is not found', async () => {
  718. const openWidgetViewerModal = jest.spyOn(modals, 'openWidgetViewerModal');
  719. MockApiClient.addMockResponse({
  720. url: '/organizations/org-slug/dashboards/1/',
  721. body: TestStubs.Dashboard([], {id: '1', title: 'Custom Errors'}),
  722. });
  723. render(
  724. <ViewEditDashboard
  725. organization={initialData.organization}
  726. params={{orgId: 'org-slug', dashboardId: '1', widgetId: '123'}}
  727. router={initialData.router}
  728. location={{...initialData.router.location, pathname: '/widget/123/'}}
  729. />,
  730. {context: initialData.routerContext, organization: initialData.organization}
  731. );
  732. await screen.findByText('All Releases');
  733. expect(openWidgetViewerModal).not.toHaveBeenCalled();
  734. expect(initialData.router.replace).toHaveBeenCalledWith(
  735. expect.objectContaining({
  736. pathname: '/organizations/org-slug/dashboard/1/',
  737. query: {},
  738. })
  739. );
  740. });
  741. it('saves a new dashboard with the page filters', async () => {
  742. const mockPOST = MockApiClient.addMockResponse({
  743. url: '/organizations/org-slug/dashboards/',
  744. method: 'POST',
  745. body: [],
  746. });
  747. render(
  748. <CreateDashboard
  749. organization={initialData.organization}
  750. params={{orgId: 'org-slug'}}
  751. router={initialData.router}
  752. location={{
  753. ...initialData.router.location,
  754. query: {
  755. ...initialData.router.location.query,
  756. statsPeriod: '7d',
  757. project: [2],
  758. environment: ['alpha', 'beta'],
  759. },
  760. }}
  761. />,
  762. {
  763. context: initialData.routerContext,
  764. organization: initialData.organization,
  765. }
  766. );
  767. userEvent.click(await screen.findByText('Save and Finish'));
  768. expect(mockPOST).toHaveBeenCalledWith(
  769. '/organizations/org-slug/dashboards/',
  770. expect.objectContaining({
  771. data: expect.objectContaining({
  772. projects: [2],
  773. environment: ['alpha', 'beta'],
  774. period: '7d',
  775. }),
  776. })
  777. );
  778. });
  779. it('saves a template with the page filters', async () => {
  780. const mockPOST = MockApiClient.addMockResponse({
  781. url: '/organizations/org-slug/dashboards/',
  782. method: 'POST',
  783. body: [],
  784. });
  785. render(
  786. <CreateDashboard
  787. organization={initialData.organization}
  788. params={{orgId: 'org-slug', templateId: 'default-template'}}
  789. router={initialData.router}
  790. location={{
  791. ...initialData.router.location,
  792. query: {
  793. ...initialData.router.location.query,
  794. statsPeriod: '7d',
  795. project: [2],
  796. environment: ['alpha', 'beta'],
  797. },
  798. }}
  799. />,
  800. {
  801. context: initialData.routerContext,
  802. organization: initialData.organization,
  803. }
  804. );
  805. userEvent.click(await screen.findByText('Add Dashboard'));
  806. expect(mockPOST).toHaveBeenCalledWith(
  807. '/organizations/org-slug/dashboards/',
  808. expect.objectContaining({
  809. data: expect.objectContaining({
  810. projects: [2],
  811. environment: ['alpha', 'beta'],
  812. period: '7d',
  813. }),
  814. })
  815. );
  816. });
  817. it('does not render save and cancel buttons on templates', async () => {
  818. MockApiClient.addMockResponse({
  819. url: '/organizations/org-slug/releases/',
  820. body: [
  821. TestStubs.Release({
  822. shortVersion: 'sentry-android-shop@1.2.0',
  823. version: 'sentry-android-shop@1.2.0',
  824. }),
  825. ],
  826. });
  827. render(
  828. <CreateDashboard
  829. organization={initialData.organization}
  830. params={{orgId: 'org-slug', templateId: 'default-template'}}
  831. router={initialData.router}
  832. location={initialData.router.location}
  833. />,
  834. {
  835. context: initialData.routerContext,
  836. organization: initialData.organization,
  837. }
  838. );
  839. userEvent.click(await screen.findByText('24H'));
  840. userEvent.click(screen.getByText('Last 7 days'));
  841. await screen.findByText('7D');
  842. expect(screen.queryByText('Cancel')).not.toBeInTheDocument();
  843. expect(screen.queryByText('Save')).not.toBeInTheDocument();
  844. });
  845. it('can save dashboard filters in existing dashboard', async () => {
  846. MockApiClient.addMockResponse({
  847. url: '/organizations/org-slug/releases/',
  848. body: [
  849. TestStubs.Release({
  850. shortVersion: 'sentry-android-shop@1.2.0',
  851. version: 'sentry-android-shop@1.2.0',
  852. }),
  853. ],
  854. });
  855. const testData = initializeOrg({
  856. organization: TestStubs.Organization({
  857. features: [
  858. 'global-views',
  859. 'dashboards-basic',
  860. 'dashboards-edit',
  861. 'discover-query',
  862. ],
  863. }),
  864. router: {
  865. location: {
  866. ...TestStubs.location(),
  867. query: {
  868. statsPeriod: '7d',
  869. release: ['sentry-android-shop@1.2.0'],
  870. },
  871. },
  872. },
  873. });
  874. render(
  875. <ViewEditDashboard
  876. organization={testData.organization}
  877. params={{orgId: 'org-slug', dashboardId: '1'}}
  878. router={testData.router}
  879. location={testData.router.location}
  880. />,
  881. {context: testData.routerContext, organization: testData.organization}
  882. );
  883. userEvent.click(await screen.findByText('Save'));
  884. expect(mockPut).toHaveBeenCalledWith(
  885. '/organizations/org-slug/dashboards/1/',
  886. expect.objectContaining({
  887. data: expect.objectContaining({
  888. period: '7d',
  889. filters: {release: ['sentry-android-shop@1.2.0']},
  890. }),
  891. })
  892. );
  893. });
  894. it('can clear dashboard filters in compact select', async () => {
  895. MockApiClient.addMockResponse({
  896. url: '/organizations/org-slug/dashboards/1/',
  897. body: TestStubs.Dashboard(widgets, {
  898. id: '1',
  899. title: 'Custom Errors',
  900. filters: {release: ['sentry-android-shop@1.2.0']},
  901. }),
  902. });
  903. MockApiClient.addMockResponse({
  904. url: '/organizations/org-slug/releases/',
  905. body: [
  906. TestStubs.Release({
  907. shortVersion: 'sentry-android-shop@1.2.0',
  908. version: 'sentry-android-shop@1.2.0',
  909. }),
  910. ],
  911. });
  912. const testData = initializeOrg({
  913. organization: TestStubs.Organization({
  914. features: [
  915. 'global-views',
  916. 'dashboards-basic',
  917. 'dashboards-edit',
  918. 'discover-query',
  919. ],
  920. }),
  921. router: {
  922. location: {
  923. ...TestStubs.location(),
  924. query: {
  925. statsPeriod: '7d',
  926. },
  927. },
  928. },
  929. });
  930. render(
  931. <ViewEditDashboard
  932. organization={testData.organization}
  933. params={{orgId: 'org-slug', dashboardId: '1'}}
  934. router={testData.router}
  935. location={testData.router.location}
  936. />,
  937. {context: testData.routerContext, organization: testData.organization}
  938. );
  939. await screen.findByText('7D');
  940. userEvent.click(await screen.findByText('sentry-android-shop@1.2.0'));
  941. userEvent.click(screen.getByText('Clear'));
  942. screen.getByText('All Releases');
  943. userEvent.click(document.body);
  944. expect(browserHistory.push).toHaveBeenCalledWith(
  945. expect.objectContaining({
  946. query: expect.objectContaining({
  947. release: '',
  948. }),
  949. })
  950. );
  951. });
  952. it('can save absolute time range in existing dashboard', async () => {
  953. const testData = initializeOrg({
  954. organization: TestStubs.Organization({
  955. features: [
  956. 'global-views',
  957. 'dashboards-basic',
  958. 'dashboards-edit',
  959. 'discover-query',
  960. ],
  961. }),
  962. router: {
  963. location: {
  964. ...TestStubs.location(),
  965. query: {
  966. start: '2022-07-14T07:00:00',
  967. end: '2022-07-19T23:59:59',
  968. utc: 'true',
  969. },
  970. },
  971. },
  972. });
  973. render(
  974. <ViewEditDashboard
  975. organization={testData.organization}
  976. params={{orgId: 'org-slug', dashboardId: '1'}}
  977. router={testData.router}
  978. location={testData.router.location}
  979. />,
  980. {context: testData.routerContext, organization: testData.organization}
  981. );
  982. userEvent.click(await screen.findByText('Save'));
  983. expect(mockPut).toHaveBeenCalledWith(
  984. '/organizations/org-slug/dashboards/1/',
  985. expect.objectContaining({
  986. data: expect.objectContaining({
  987. start: '2022-07-14T07:00:00.000',
  988. end: '2022-07-19T23:59:59.000',
  989. utc: true,
  990. }),
  991. })
  992. );
  993. });
  994. it('can clear dashboard filters in existing dashboard', async () => {
  995. MockApiClient.addMockResponse({
  996. url: '/organizations/org-slug/releases/',
  997. body: [
  998. TestStubs.Release({
  999. shortVersion: 'sentry-android-shop@1.2.0',
  1000. version: 'sentry-android-shop@1.2.0',
  1001. }),
  1002. ],
  1003. });
  1004. const testData = initializeOrg({
  1005. organization: TestStubs.Organization({
  1006. features: [
  1007. 'global-views',
  1008. 'dashboards-basic',
  1009. 'dashboards-edit',
  1010. 'discover-query',
  1011. ],
  1012. }),
  1013. router: {
  1014. location: {
  1015. ...TestStubs.location(),
  1016. query: {
  1017. statsPeriod: '7d',
  1018. project: [1, 2],
  1019. environment: ['alpha', 'beta'],
  1020. },
  1021. },
  1022. },
  1023. });
  1024. render(
  1025. <ViewEditDashboard
  1026. organization={testData.organization}
  1027. params={{orgId: 'org-slug', dashboardId: '1'}}
  1028. router={testData.router}
  1029. location={testData.router.location}
  1030. />,
  1031. {context: testData.routerContext, organization: testData.organization}
  1032. );
  1033. await screen.findByText('7D');
  1034. userEvent.click(await screen.findByText('All Releases'));
  1035. userEvent.click(screen.getByText('sentry-android-shop@1.2.0'));
  1036. userEvent.keyboard('{esc}');
  1037. userEvent.click(screen.getByText('Cancel'));
  1038. screen.getByText('All Releases');
  1039. expect(browserHistory.replace).toHaveBeenCalledWith(
  1040. expect.objectContaining({
  1041. query: expect.objectContaining({
  1042. project: undefined,
  1043. statsPeriod: undefined,
  1044. environment: undefined,
  1045. }),
  1046. })
  1047. );
  1048. });
  1049. it('disables the Edit Dashboard button when there are unsaved filters', async () => {
  1050. MockApiClient.addMockResponse({
  1051. url: '/organizations/org-slug/releases/',
  1052. body: [
  1053. TestStubs.Release({
  1054. shortVersion: 'sentry-android-shop@1.2.0',
  1055. version: 'sentry-android-shop@1.2.0',
  1056. }),
  1057. ],
  1058. });
  1059. const testData = initializeOrg({
  1060. organization: TestStubs.Organization({
  1061. features: [
  1062. 'global-views',
  1063. 'dashboards-basic',
  1064. 'dashboards-edit',
  1065. 'discover-basic',
  1066. 'discover-query',
  1067. ],
  1068. }),
  1069. router: {
  1070. location: {
  1071. ...TestStubs.location(),
  1072. query: {
  1073. statsPeriod: '7d',
  1074. project: [1, 2],
  1075. environment: ['alpha', 'beta'],
  1076. },
  1077. },
  1078. },
  1079. });
  1080. render(
  1081. <ViewEditDashboard
  1082. organization={testData.organization}
  1083. params={{orgId: 'org-slug', dashboardId: '1'}}
  1084. router={testData.router}
  1085. location={testData.router.location}
  1086. />,
  1087. {context: testData.routerContext, organization: testData.organization}
  1088. );
  1089. expect(await screen.findByText('Save')).toBeInTheDocument();
  1090. expect(screen.getByText('Cancel')).toBeInTheDocument();
  1091. expect(screen.getByRole('button', {name: 'Edit Dashboard'})).toBeDisabled();
  1092. });
  1093. it('ignores the order of selection of page filters to render unsaved filters', async () => {
  1094. const testProjects = [
  1095. TestStubs.Project({id: '1', name: 'first', environments: ['alpha', 'beta']}),
  1096. TestStubs.Project({id: '2', name: 'second', environments: ['alpha', 'beta']}),
  1097. ];
  1098. act(() => ProjectsStore.loadInitialData(testProjects));
  1099. MockApiClient.addMockResponse({
  1100. url: '/organizations/org-slug/projects/',
  1101. body: testProjects,
  1102. });
  1103. MockApiClient.addMockResponse({
  1104. url: '/organizations/org-slug/dashboards/1/',
  1105. body: TestStubs.Dashboard(widgets, {
  1106. id: '1',
  1107. title: 'Custom Errors',
  1108. filters: {},
  1109. environment: ['alpha', 'beta'],
  1110. }),
  1111. });
  1112. const testData = initializeOrg({
  1113. organization: TestStubs.Organization({
  1114. features: [
  1115. 'global-views',
  1116. 'dashboards-basic',
  1117. 'dashboards-edit',
  1118. 'discover-query',
  1119. ],
  1120. }),
  1121. router: {
  1122. location: {
  1123. ...TestStubs.location(),
  1124. query: {
  1125. environment: ['beta', 'alpha'], // Reversed order from saved dashboard
  1126. },
  1127. },
  1128. },
  1129. });
  1130. render(
  1131. <ViewEditDashboard
  1132. organization={testData.organization}
  1133. params={{orgId: 'org-slug', dashboardId: '1'}}
  1134. router={testData.router}
  1135. location={testData.router.location}
  1136. />,
  1137. {context: testData.routerContext, organization: testData.organization}
  1138. );
  1139. await waitFor(() => expect(screen.queryAllByText('Loading\u2026')).toEqual([]));
  1140. await screen.findByText(/beta, alpha/);
  1141. // Save and Cancel should not appear because alpha, beta is the same as beta, alpha
  1142. expect(screen.queryByText('Save')).not.toBeInTheDocument();
  1143. expect(screen.queryByText('Cancel')).not.toBeInTheDocument();
  1144. });
  1145. it('uses releases from the URL query params', async function () {
  1146. const testData = initializeOrg({
  1147. organization: TestStubs.Organization({
  1148. features: [
  1149. 'global-views',
  1150. 'dashboards-basic',
  1151. 'dashboards-edit',
  1152. 'discover-query',
  1153. ],
  1154. }),
  1155. router: {
  1156. location: {
  1157. ...TestStubs.location(),
  1158. query: {
  1159. release: ['not-selected-1'],
  1160. },
  1161. },
  1162. },
  1163. });
  1164. render(
  1165. <ViewEditDashboard
  1166. organization={testData.organization}
  1167. params={{orgId: 'org-slug', dashboardId: '1'}}
  1168. router={testData.router}
  1169. location={testData.router.location}
  1170. />,
  1171. {context: testData.routerContext, organization: testData.organization}
  1172. );
  1173. await screen.findByText(/not-selected-1/);
  1174. screen.getByText('Save');
  1175. screen.getByText('Cancel');
  1176. });
  1177. it('resets release in URL params', async function () {
  1178. MockApiClient.addMockResponse({
  1179. url: '/organizations/org-slug/dashboards/1/',
  1180. body: TestStubs.Dashboard(widgets, {
  1181. id: '1',
  1182. title: 'Custom Errors',
  1183. filters: {
  1184. release: ['abc'],
  1185. },
  1186. }),
  1187. });
  1188. const testData = initializeOrg({
  1189. organization: TestStubs.Organization({
  1190. features: [
  1191. 'global-views',
  1192. 'dashboards-basic',
  1193. 'dashboards-edit',
  1194. 'discover-query',
  1195. ],
  1196. }),
  1197. router: {
  1198. location: {
  1199. ...TestStubs.location(),
  1200. query: {
  1201. release: ['not-selected-1'],
  1202. },
  1203. },
  1204. },
  1205. });
  1206. render(
  1207. <ViewEditDashboard
  1208. organization={testData.organization}
  1209. params={{orgId: 'org-slug', dashboardId: '1'}}
  1210. router={testData.router}
  1211. location={testData.router.location}
  1212. />,
  1213. {context: testData.routerContext, organization: testData.organization}
  1214. );
  1215. await screen.findByText(/not-selected-1/);
  1216. userEvent.click(screen.getByText('Cancel'));
  1217. // release isn't used in the redirect
  1218. expect(browserHistory.replace).toHaveBeenCalledWith(
  1219. expect.objectContaining({
  1220. query: {
  1221. end: undefined,
  1222. environment: undefined,
  1223. project: undefined,
  1224. start: undefined,
  1225. statsPeriod: undefined,
  1226. utc: undefined,
  1227. },
  1228. })
  1229. );
  1230. });
  1231. it('reflects selections in the release filter in the query params', async function () {
  1232. MockApiClient.addMockResponse({
  1233. url: '/organizations/org-slug/releases/',
  1234. body: [
  1235. TestStubs.Release({
  1236. shortVersion: 'sentry-android-shop@1.2.0',
  1237. version: 'sentry-android-shop@1.2.0',
  1238. }),
  1239. ],
  1240. });
  1241. const testData = initializeOrg({
  1242. organization: TestStubs.Organization({
  1243. features: [
  1244. 'global-views',
  1245. 'dashboards-basic',
  1246. 'dashboards-edit',
  1247. 'discover-query',
  1248. ],
  1249. }),
  1250. router: {
  1251. location: TestStubs.location(),
  1252. },
  1253. });
  1254. render(
  1255. <ViewEditDashboard
  1256. organization={testData.organization}
  1257. params={{orgId: 'org-slug', dashboardId: '1'}}
  1258. router={testData.router}
  1259. location={testData.router.location}
  1260. />,
  1261. {context: testData.routerContext, organization: testData.organization}
  1262. );
  1263. userEvent.click(await screen.findByText('All Releases'));
  1264. userEvent.click(screen.getByText('sentry-android-shop@1.2.0'));
  1265. userEvent.click(document.body);
  1266. expect(browserHistory.push).toHaveBeenCalledWith(
  1267. expect.objectContaining({
  1268. query: expect.objectContaining({
  1269. release: ['sentry-android-shop@1.2.0'],
  1270. }),
  1271. })
  1272. );
  1273. });
  1274. it('persists release selections made during search requests that do not appear in default query', async function () {
  1275. // Default response
  1276. MockApiClient.addMockResponse({
  1277. url: '/organizations/org-slug/releases/',
  1278. body: [
  1279. TestStubs.Release({
  1280. shortVersion: 'sentry-android-shop@1.2.0',
  1281. version: 'sentry-android-shop@1.2.0',
  1282. }),
  1283. ],
  1284. });
  1285. // Mocked search results
  1286. MockApiClient.addMockResponse({
  1287. url: '/organizations/org-slug/releases/',
  1288. body: [
  1289. TestStubs.Release({
  1290. id: '9',
  1291. shortVersion: 'search-result',
  1292. version: 'search-result',
  1293. }),
  1294. ],
  1295. match: [MockApiClient.matchData({query: 's'})],
  1296. });
  1297. const testData = initializeOrg({
  1298. organization: TestStubs.Organization({
  1299. features: [
  1300. 'global-views',
  1301. 'dashboards-basic',
  1302. 'dashboards-edit',
  1303. 'discover-basic',
  1304. 'discover-query',
  1305. ],
  1306. }),
  1307. router: {
  1308. location: TestStubs.location(),
  1309. },
  1310. });
  1311. render(
  1312. <ViewEditDashboard
  1313. organization={testData.organization}
  1314. params={{orgId: 'org-slug', dashboardId: '1'}}
  1315. router={testData.router}
  1316. location={testData.router.location}
  1317. />,
  1318. {context: testData.routerContext, organization: testData.organization}
  1319. );
  1320. userEvent.click(await screen.findByText('All Releases'));
  1321. userEvent.type(screen.getByText('Search\u2026'), 's');
  1322. await act(async () => {
  1323. userEvent.click(await screen.findByText('search-result'));
  1324. });
  1325. // Validate that after search is cleared, search result still appears
  1326. await screen.findByText('Latest Release(s)');
  1327. screen.getByTestId('search-result');
  1328. });
  1329. });
  1330. });