DashboardModel.test.ts 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. import { keys as _keys } from 'lodash';
  2. import { getDashboardModel } from '../../../../test/helpers/getDashboardModel';
  3. import { expect } from '../../../../test/lib/common';
  4. import { variableAdapters } from '../../variables/adapters';
  5. import { createAdHocVariableAdapter } from '../../variables/adhoc/adapter';
  6. import { createCustomVariableAdapter } from '../../variables/custom/adapter';
  7. import { createQueryVariableAdapter } from '../../variables/query/adapter';
  8. import { setTimeSrv, TimeSrv } from '../services/TimeSrv';
  9. import { DashboardModel } from '../state/DashboardModel';
  10. import { PanelModel } from '../state/PanelModel';
  11. jest.mock('app/core/services/context_srv', () => ({}));
  12. variableAdapters.setInit(() => [
  13. createQueryVariableAdapter(),
  14. createAdHocVariableAdapter(),
  15. createCustomVariableAdapter(),
  16. ]);
  17. describe('DashboardModel', () => {
  18. describe('when creating new dashboard model defaults only', () => {
  19. let model: DashboardModel;
  20. beforeEach(() => {
  21. model = new DashboardModel({}, {});
  22. });
  23. it('should have title', () => {
  24. expect(model.title).toBe('No Title');
  25. });
  26. it('should have meta', () => {
  27. expect(model.meta.canSave).toBe(true);
  28. expect(model.meta.canShare).toBe(true);
  29. });
  30. it('should have default properties', () => {
  31. expect(model.panels.length).toBe(0);
  32. });
  33. });
  34. describe('when getting next panel id', () => {
  35. let model: DashboardModel;
  36. beforeEach(() => {
  37. model = new DashboardModel({
  38. panels: [{ id: 5 }],
  39. });
  40. });
  41. it('should return max id + 1', () => {
  42. expect(model.getNextPanelId()).toBe(6);
  43. });
  44. });
  45. describe('getSaveModelClone', () => {
  46. it('should sort keys', () => {
  47. const model = new DashboardModel({});
  48. model.autoUpdate = null;
  49. const saveModel = model.getSaveModelClone();
  50. const keys = _keys(saveModel);
  51. expect(keys[0]).toBe('annotations');
  52. expect(keys[1]).toBe('editable');
  53. });
  54. it('should remove add panel panels', () => {
  55. const model = new DashboardModel({});
  56. model.addPanel({
  57. type: 'add-panel',
  58. });
  59. model.addPanel({
  60. type: 'graph',
  61. });
  62. model.addPanel({
  63. type: 'add-panel',
  64. });
  65. const saveModel = model.getSaveModelClone();
  66. const panels = saveModel.panels;
  67. expect(panels.length).toBe(1);
  68. });
  69. it('should save model in edit mode', () => {
  70. const model = new DashboardModel({});
  71. model.addPanel({ type: 'graph' });
  72. const panel = model.initEditPanel(model.panels[0]);
  73. panel.title = 'updated';
  74. const saveModel = model.getSaveModelClone();
  75. const savedPanel = saveModel.panels[0];
  76. expect(savedPanel.title).toBe('updated');
  77. expect(savedPanel.id).toBe(model.panels[0].id);
  78. });
  79. });
  80. describe('row and panel manipulation', () => {
  81. let dashboard: DashboardModel;
  82. beforeEach(() => {
  83. dashboard = new DashboardModel({});
  84. });
  85. it('adding panel should new up panel model', () => {
  86. dashboard.addPanel({ type: 'test', title: 'test' });
  87. expect(dashboard.panels[0] instanceof PanelModel).toBe(true);
  88. });
  89. it('duplicate panel should try to add to the right if there is space', () => {
  90. const panel = { id: 10, gridPos: { x: 0, y: 0, w: 6, h: 2 } };
  91. dashboard.addPanel(panel);
  92. dashboard.duplicatePanel(dashboard.panels[0]);
  93. expect(dashboard.panels[1].gridPos).toMatchObject({
  94. x: 6,
  95. y: 0,
  96. h: 2,
  97. w: 6,
  98. });
  99. });
  100. it('duplicate panel should remove repeat data', () => {
  101. const panel = {
  102. id: 10,
  103. gridPos: { x: 0, y: 0, w: 6, h: 2 },
  104. repeat: 'asd',
  105. scopedVars: { test: 'asd' },
  106. };
  107. dashboard.addPanel(panel);
  108. dashboard.duplicatePanel(dashboard.panels[0]);
  109. expect(dashboard.panels[1].repeat).toBe(undefined);
  110. expect(dashboard.panels[1].scopedVars).toBe(undefined);
  111. });
  112. });
  113. describe('Given editable false dashboard', () => {
  114. let model: DashboardModel;
  115. beforeEach(() => {
  116. model = new DashboardModel({ editable: false });
  117. });
  118. it('Should set meta canEdit and canSave to false', () => {
  119. expect(model.meta.canSave).toBe(false);
  120. expect(model.meta.canEdit).toBe(false);
  121. });
  122. it('getSaveModelClone should remove meta', () => {
  123. const clone = model.getSaveModelClone();
  124. expect(clone.meta).toBe(undefined);
  125. });
  126. });
  127. describe('when loading dashboard with old influxdb query schema', () => {
  128. let model: DashboardModel;
  129. let target: any;
  130. beforeEach(() => {
  131. model = new DashboardModel({
  132. panels: [
  133. {
  134. type: 'graph',
  135. grid: {},
  136. yaxes: [{}, {}],
  137. targets: [
  138. {
  139. alias: '$tag_datacenter $tag_source $col',
  140. column: 'value',
  141. measurement: 'logins.count',
  142. fields: [
  143. {
  144. func: 'mean',
  145. name: 'value',
  146. mathExpr: '*2',
  147. asExpr: 'value',
  148. },
  149. {
  150. name: 'one-minute',
  151. func: 'mean',
  152. mathExpr: '*3',
  153. asExpr: 'one-minute',
  154. },
  155. ],
  156. tags: [],
  157. fill: 'previous',
  158. function: 'mean',
  159. groupBy: [
  160. {
  161. interval: 'auto',
  162. type: 'time',
  163. },
  164. {
  165. key: 'source',
  166. type: 'tag',
  167. },
  168. {
  169. type: 'tag',
  170. key: 'datacenter',
  171. },
  172. ],
  173. },
  174. ],
  175. },
  176. ],
  177. });
  178. target = model.panels[0].targets[0];
  179. });
  180. it('should update query schema', () => {
  181. expect(target.fields).toBe(undefined);
  182. expect(target.select.length).toBe(2);
  183. expect(target.select[0].length).toBe(4);
  184. expect(target.select[0][0].type).toBe('field');
  185. expect(target.select[0][1].type).toBe('mean');
  186. expect(target.select[0][2].type).toBe('math');
  187. expect(target.select[0][3].type).toBe('alias');
  188. });
  189. });
  190. describe('when creating dashboard model with missing list for annoations or templating', () => {
  191. let model: DashboardModel;
  192. beforeEach(() => {
  193. model = new DashboardModel({
  194. annotations: {
  195. enable: true,
  196. },
  197. templating: {
  198. enable: true,
  199. },
  200. });
  201. });
  202. it('should add empty list', () => {
  203. expect(model.annotations.list.length).toBe(1);
  204. expect(model.templating.list.length).toBe(0);
  205. });
  206. it('should add builtin annotation query', () => {
  207. expect(model.annotations.list[0].builtIn).toBe(1);
  208. expect(model.templating.list.length).toBe(0);
  209. });
  210. });
  211. describe('Formatting epoch timestamp when timezone is set as utc', () => {
  212. let dashboard: DashboardModel;
  213. beforeEach(() => {
  214. dashboard = new DashboardModel({ timezone: 'utc' });
  215. });
  216. it('Should format timestamp with second resolution by default', () => {
  217. expect(dashboard.formatDate(1234567890000)).toBe('2009-02-13 23:31:30');
  218. });
  219. it('Should format timestamp with second resolution even if second format is passed as parameter', () => {
  220. expect(dashboard.formatDate(1234567890007, 'YYYY-MM-DD HH:mm:ss')).toBe('2009-02-13 23:31:30');
  221. });
  222. it('Should format timestamp with millisecond resolution if format is passed as parameter', () => {
  223. expect(dashboard.formatDate(1234567890007, 'YYYY-MM-DD HH:mm:ss.SSS')).toBe('2009-02-13 23:31:30.007');
  224. });
  225. });
  226. describe('isSubMenuVisible with empty lists', () => {
  227. let model: DashboardModel;
  228. beforeEach(() => {
  229. model = new DashboardModel({});
  230. });
  231. it('should not show submenu', () => {
  232. expect(model.isSubMenuVisible()).toBe(false);
  233. });
  234. });
  235. describe('isSubMenuVisible with annotation', () => {
  236. let model: DashboardModel;
  237. beforeEach(() => {
  238. model = new DashboardModel({
  239. annotations: {
  240. list: [{}],
  241. },
  242. });
  243. });
  244. it('should show submmenu', () => {
  245. expect(model.isSubMenuVisible()).toBe(true);
  246. });
  247. });
  248. describe('isSubMenuVisible with template var', () => {
  249. let model: DashboardModel;
  250. beforeEach(() => {
  251. model = new DashboardModel(
  252. {
  253. templating: {
  254. list: [{}],
  255. },
  256. },
  257. {},
  258. // getVariablesFromState stub to return a variable
  259. () => [{} as any]
  260. );
  261. });
  262. it('should enable submmenu', () => {
  263. expect(model.isSubMenuVisible()).toBe(true);
  264. });
  265. });
  266. describe('isSubMenuVisible with hidden template var', () => {
  267. let model: DashboardModel;
  268. beforeEach(() => {
  269. model = new DashboardModel({
  270. templating: {
  271. list: [{ hide: 2 }],
  272. },
  273. });
  274. });
  275. it('should not enable submmenu', () => {
  276. expect(model.isSubMenuVisible()).toBe(false);
  277. });
  278. });
  279. describe('isSubMenuVisible with hidden annotation toggle', () => {
  280. let dashboard: DashboardModel;
  281. beforeEach(() => {
  282. dashboard = new DashboardModel({
  283. annotations: {
  284. list: [{ hide: true }],
  285. },
  286. });
  287. });
  288. it('should not enable submmenu', () => {
  289. expect(dashboard.isSubMenuVisible()).toBe(false);
  290. });
  291. });
  292. describe('When collapsing row', () => {
  293. let dashboard: DashboardModel;
  294. beforeEach(() => {
  295. dashboard = new DashboardModel({
  296. panels: [
  297. { id: 1, type: 'graph', gridPos: { x: 0, y: 0, w: 24, h: 2 } },
  298. { id: 2, type: 'row', gridPos: { x: 0, y: 2, w: 24, h: 2 } },
  299. { id: 3, type: 'graph', gridPos: { x: 0, y: 4, w: 12, h: 2 } },
  300. { id: 4, type: 'graph', gridPos: { x: 12, y: 4, w: 12, h: 2 } },
  301. { id: 5, type: 'row', gridPos: { x: 0, y: 6, w: 24, h: 2 } },
  302. ],
  303. });
  304. dashboard.toggleRow(dashboard.panels[1]);
  305. });
  306. it('should not impact hasUnsavedChanges', () => {
  307. expect(dashboard.hasUnsavedChanges()).toBe(false);
  308. });
  309. it('should impact hasUnsavedChanges if panels have changes when row is collapsed', () => {
  310. dashboard.panels[0].setProperty('title', 'new title');
  311. expect(dashboard.hasUnsavedChanges()).toBe(true);
  312. });
  313. it('should remove panels and put them inside collapsed row', () => {
  314. expect(dashboard.panels.length).toBe(3);
  315. expect(dashboard.panels[1].panels?.length).toBe(2);
  316. });
  317. describe('and when removing row and its panels', () => {
  318. beforeEach(() => {
  319. dashboard.removeRow(dashboard.panels[1], true);
  320. });
  321. it('should remove row and its panels', () => {
  322. expect(dashboard.panels.length).toBe(2);
  323. });
  324. });
  325. describe('and when removing only the row', () => {
  326. beforeEach(() => {
  327. dashboard.removeRow(dashboard.panels[1], false);
  328. });
  329. it('should only remove row', () => {
  330. expect(dashboard.panels.length).toBe(4);
  331. });
  332. });
  333. });
  334. describe('When expanding row', () => {
  335. let dashboard: DashboardModel;
  336. beforeEach(() => {
  337. dashboard = new DashboardModel({
  338. panels: [
  339. { id: 1, type: 'graph', gridPos: { x: 0, y: 0, w: 24, h: 6 } },
  340. {
  341. id: 2,
  342. type: 'row',
  343. gridPos: { x: 0, y: 6, w: 24, h: 1 },
  344. collapsed: true,
  345. panels: [
  346. { id: 3, type: 'graph', gridPos: { x: 0, y: 7, w: 12, h: 2 } },
  347. { id: 4, type: 'graph', gridPos: { x: 12, y: 7, w: 12, h: 2 } },
  348. ],
  349. },
  350. { id: 5, type: 'row', gridPos: { x: 0, y: 7, w: 1, h: 1 } },
  351. ],
  352. });
  353. dashboard.toggleRow(dashboard.panels[1]);
  354. });
  355. it('should add panels back', () => {
  356. expect(dashboard.panels.length).toBe(5);
  357. });
  358. it('should add them below row in array', () => {
  359. expect(dashboard.panels[2].id).toBe(3);
  360. expect(dashboard.panels[3].id).toBe(4);
  361. });
  362. it('should position them below row', () => {
  363. expect(dashboard.panels[2].gridPos).toMatchObject({
  364. x: 0,
  365. y: 7,
  366. w: 12,
  367. h: 2,
  368. });
  369. });
  370. it('should move panels below down', () => {
  371. expect(dashboard.panels[4].gridPos).toMatchObject({
  372. x: 0,
  373. y: 9,
  374. w: 1,
  375. h: 1,
  376. });
  377. });
  378. describe('and when removing row and its panels', () => {
  379. beforeEach(() => {
  380. dashboard.removeRow(dashboard.panels[1], true);
  381. });
  382. it('should remove row and its panels', () => {
  383. expect(dashboard.panels.length).toBe(2);
  384. });
  385. });
  386. describe('and when removing only the row', () => {
  387. beforeEach(() => {
  388. dashboard.removeRow(dashboard.panels[1], false);
  389. });
  390. it('should only remove row', () => {
  391. expect(dashboard.panels.length).toBe(4);
  392. });
  393. });
  394. });
  395. describe('When expanding row with panels that do not contain an x and y pos', () => {
  396. let dashboard: DashboardModel;
  397. beforeEach(() => {
  398. dashboard = new DashboardModel({
  399. panels: [
  400. { id: 1, type: 'graph', gridPos: { x: 0, y: 0, w: 24, h: 6 } },
  401. {
  402. id: 2,
  403. type: 'row',
  404. gridPos: { x: 0, y: 6, w: 24, h: 1 },
  405. collapsed: true,
  406. panels: [
  407. { id: 3, type: 'graph', gridPos: { w: 12, h: 2 } },
  408. { id: 4, type: 'graph', gridPos: { w: 12, h: 2 } },
  409. ],
  410. },
  411. { id: 5, type: 'row', gridPos: { x: 0, y: 7, w: 1, h: 1 } },
  412. ],
  413. });
  414. dashboard.toggleRow(dashboard.panels[1]);
  415. });
  416. it('should correctly set the x and y values for the inner panels', () => {
  417. expect(dashboard.panels[2].gridPos).toMatchObject({
  418. x: 0,
  419. y: 7,
  420. w: 12,
  421. h: 2,
  422. });
  423. expect(dashboard.panels[3].gridPos).toMatchObject({
  424. x: 0,
  425. y: 7,
  426. w: 12,
  427. h: 2,
  428. });
  429. });
  430. });
  431. describe('Given model with time', () => {
  432. let model: DashboardModel;
  433. let consoleWarnSpy: jest.SpyInstance;
  434. beforeEach(() => {
  435. consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
  436. model = new DashboardModel({
  437. time: {
  438. from: 'now-6h',
  439. to: 'now',
  440. },
  441. });
  442. expect(model.hasTimeChanged()).toBeFalsy();
  443. model.time = {
  444. from: 'now-3h',
  445. to: 'now-1h',
  446. };
  447. });
  448. afterEach(() => {
  449. consoleWarnSpy.mockRestore();
  450. });
  451. it('hasTimeChanged should be true', () => {
  452. expect(model.hasTimeChanged()).toBeTruthy();
  453. });
  454. it('getSaveModelClone should return original time when saveTimerange=false', () => {
  455. const options = { saveTimerange: false };
  456. const saveModel = model.getSaveModelClone(options);
  457. expect(saveModel.time.from).toBe('now-6h');
  458. expect(saveModel.time.to).toBe('now');
  459. });
  460. it('getSaveModelClone should return updated time when saveTimerange=true', () => {
  461. const options = { saveTimerange: true };
  462. const saveModel = model.getSaveModelClone(options);
  463. expect(saveModel.time.from).toBe('now-3h');
  464. expect(saveModel.time.to).toBe('now-1h');
  465. });
  466. it('hasTimeChanged should be false when reset original time', () => {
  467. model.resetOriginalTime();
  468. expect(model.hasTimeChanged()).toBeFalsy();
  469. });
  470. it('getSaveModelClone should return original time when saveTimerange=false', () => {
  471. const options = { saveTimerange: false };
  472. const saveModel = model.getSaveModelClone(options);
  473. expect(saveModel.time.from).toBe('now-6h');
  474. expect(saveModel.time.to).toBe('now');
  475. });
  476. it('getSaveModelClone should return updated time when saveTimerange=true', () => {
  477. const options = { saveTimerange: true };
  478. const saveModel = model.getSaveModelClone(options);
  479. expect(saveModel.time.from).toBe('now-3h');
  480. expect(saveModel.time.to).toBe('now-1h');
  481. });
  482. it('getSaveModelClone should remove repeated panels and scopedVars', () => {
  483. const dashboardJSON = {
  484. panels: [
  485. { id: 1, type: 'row', repeat: 'dc', gridPos: { x: 0, y: 0, h: 1, w: 24 } },
  486. { id: 2, repeat: 'app', repeatDirection: 'h', gridPos: { x: 0, y: 1, h: 2, w: 8 } },
  487. ],
  488. templating: {
  489. list: [
  490. {
  491. name: 'dc',
  492. type: 'custom',
  493. current: {
  494. text: 'dc1 + dc2',
  495. value: ['dc1', 'dc2'],
  496. },
  497. options: [
  498. { text: 'dc1', value: 'dc1', selected: true },
  499. { text: 'dc2', value: 'dc2', selected: true },
  500. ],
  501. },
  502. {
  503. name: 'app',
  504. type: 'custom',
  505. current: {
  506. text: 'se1 + se2',
  507. value: ['se1', 'se2'],
  508. },
  509. options: [
  510. { text: 'se1', value: 'se1', selected: true },
  511. { text: 'se2', value: 'se2', selected: true },
  512. ],
  513. },
  514. ],
  515. },
  516. };
  517. const model = getDashboardModel(dashboardJSON);
  518. model.processRepeats();
  519. expect(model.panels.filter((x) => x.type === 'row')).toHaveLength(2);
  520. expect(model.panels.filter((x) => x.type !== 'row')).toHaveLength(4);
  521. expect(model.panels.find((x) => x.type !== 'row')?.scopedVars?.dc.value).toBe('dc1');
  522. expect(model.panels.find((x) => x.type !== 'row')?.scopedVars?.app.value).toBe('se1');
  523. const saveModel = model.getSaveModelClone();
  524. expect(saveModel.panels.length).toBe(2);
  525. expect(saveModel.panels[0].scopedVars).toBe(undefined);
  526. expect(saveModel.panels[1].scopedVars).toBe(undefined);
  527. model.collapseRows();
  528. const savedModelWithCollapsedRows: any = model.getSaveModelClone();
  529. expect(savedModelWithCollapsedRows.panels[0].panels.length).toBe(1);
  530. });
  531. it('getSaveModelClone should not remove repeated panels and scopedVars during snapshot', () => {
  532. const dashboardJSON = {
  533. panels: [
  534. { id: 1, type: 'row', repeat: 'dc', gridPos: { x: 0, y: 0, h: 1, w: 24 } },
  535. { id: 2, repeat: 'app', repeatDirection: 'h', gridPos: { x: 0, y: 1, h: 2, w: 8 } },
  536. ],
  537. templating: {
  538. list: [
  539. {
  540. name: 'dc',
  541. type: 'custom',
  542. current: {
  543. text: 'dc1 + dc2',
  544. value: ['dc1', 'dc2'],
  545. },
  546. options: [
  547. { text: 'dc1', value: 'dc1', selected: true },
  548. { text: 'dc2', value: 'dc2', selected: true },
  549. ],
  550. },
  551. {
  552. name: 'app',
  553. type: 'custom',
  554. current: {
  555. text: 'se1 + se2',
  556. value: ['se1', 'se2'],
  557. },
  558. options: [
  559. { text: 'se1', value: 'se1', selected: true },
  560. { text: 'se2', value: 'se2', selected: true },
  561. ],
  562. },
  563. ],
  564. },
  565. };
  566. const model = getDashboardModel(dashboardJSON);
  567. model.processRepeats();
  568. expect(model.panels.filter((x) => x.type === 'row')).toHaveLength(2);
  569. expect(model.panels.filter((x) => x.type !== 'row')).toHaveLength(4);
  570. expect(model.panels.find((x) => x.type !== 'row')?.scopedVars?.dc.value).toBe('dc1');
  571. expect(model.panels.find((x) => x.type !== 'row')?.scopedVars?.app.value).toBe('se1');
  572. model.snapshot = { timestamp: new Date() };
  573. const saveModel = model.getSaveModelClone();
  574. expect(saveModel.panels.filter((x) => x.type === 'row')).toHaveLength(2);
  575. expect(saveModel.panels.filter((x) => x.type !== 'row')).toHaveLength(4);
  576. expect(saveModel.panels.find((x) => x.type !== 'row')?.scopedVars?.dc.value).toBe('dc1');
  577. expect(saveModel.panels.find((x) => x.type !== 'row')?.scopedVars?.app.value).toBe('se1');
  578. model.collapseRows();
  579. const savedModelWithCollapsedRows: any = model.getSaveModelClone();
  580. expect(savedModelWithCollapsedRows.panels[0].panels.length).toBe(2);
  581. });
  582. });
  583. describe('Given model with template variable of type query', () => {
  584. let model: DashboardModel;
  585. beforeEach(() => {
  586. const json = {
  587. templating: {
  588. list: [
  589. {
  590. name: 'Server',
  591. type: 'query',
  592. current: {
  593. selected: true,
  594. text: 'server_001',
  595. value: 'server_001',
  596. },
  597. },
  598. ],
  599. },
  600. };
  601. model = getDashboardModel(json);
  602. expect(model.hasVariableValuesChanged()).toBeFalsy();
  603. });
  604. it('hasVariableValuesChanged should be false when adding a template variable', () => {
  605. model.templating.list.push({
  606. name: 'Server2',
  607. type: 'query',
  608. current: {
  609. selected: true,
  610. text: 'server_002',
  611. value: 'server_002',
  612. },
  613. });
  614. expect(model.hasVariableValuesChanged()).toBeFalsy();
  615. });
  616. it('hasVariableValuesChanged should be false when removing existing template variable', () => {
  617. model.templating.list = [];
  618. expect(model.hasVariableValuesChanged()).toBeFalsy();
  619. });
  620. it('hasVariableValuesChanged should be true when changing value of template variable', () => {
  621. model.templating.list[0].current.text = 'server_002';
  622. expect(model.hasVariableValuesChanged()).toBeTruthy();
  623. });
  624. it('getSaveModelClone should return original variable when saveVariables=false', () => {
  625. model.templating.list[0].current.text = 'server_002';
  626. const options = { saveVariables: false };
  627. const saveModel = model.getSaveModelClone(options);
  628. expect(saveModel.templating.list[0].current.text).toBe('server_001');
  629. });
  630. it('getSaveModelClone should return updated variable when saveVariables=true', () => {
  631. model.templating.list[0].current.text = 'server_002';
  632. const options = { saveVariables: true };
  633. const saveModel = model.getSaveModelClone(options);
  634. expect(saveModel.templating.list[0].current.text).toBe('server_002');
  635. });
  636. });
  637. describe('Given model with template variable of type adhoc', () => {
  638. let model: DashboardModel;
  639. beforeEach(() => {
  640. const json = {
  641. templating: {
  642. list: [
  643. {
  644. name: 'Filter',
  645. type: 'adhoc',
  646. filters: [
  647. {
  648. key: '@hostname',
  649. operator: '=',
  650. value: 'server 20',
  651. },
  652. ],
  653. },
  654. ],
  655. },
  656. };
  657. model = getDashboardModel(json);
  658. expect(model.hasVariableValuesChanged()).toBeFalsy();
  659. });
  660. it('hasVariableValuesChanged should be false when adding a template variable', () => {
  661. model.templating.list.push({
  662. name: 'Filter',
  663. type: 'adhoc',
  664. filters: [
  665. {
  666. key: '@hostname',
  667. operator: '=',
  668. value: 'server 1',
  669. },
  670. ],
  671. });
  672. expect(model.hasVariableValuesChanged()).toBeFalsy();
  673. });
  674. it('hasVariableValuesChanged should be false when removing existing template variable', () => {
  675. model.templating.list = [];
  676. expect(model.hasVariableValuesChanged()).toBeFalsy();
  677. });
  678. it('hasVariableValuesChanged should be true when changing value of filter', () => {
  679. model.templating.list[0].filters[0].value = 'server 1';
  680. expect(model.hasVariableValuesChanged()).toBeTruthy();
  681. });
  682. it('hasVariableValuesChanged should be true when adding an additional condition', () => {
  683. model.templating.list[0].filters[0].condition = 'AND';
  684. model.templating.list[0].filters[1] = {
  685. key: '@metric',
  686. operator: '=',
  687. value: 'logins.count',
  688. };
  689. expect(model.hasVariableValuesChanged()).toBeTruthy();
  690. });
  691. it('getSaveModelClone should return original variable when saveVariables=false', () => {
  692. model.templating.list[0].filters[0].value = 'server 1';
  693. const options = { saveVariables: false };
  694. const saveModel = model.getSaveModelClone(options);
  695. expect(saveModel.templating.list[0].filters[0].value).toBe('server 20');
  696. });
  697. it('getSaveModelClone should return updated variable when saveVariables=true', () => {
  698. model.templating.list[0].filters[0].value = 'server 1';
  699. const options = { saveVariables: true };
  700. const saveModel = model.getSaveModelClone(options);
  701. expect(saveModel.templating.list[0].filters[0].value).toBe('server 1');
  702. });
  703. });
  704. describe('Given a dashboard with one panel legend on and two off', () => {
  705. let model: DashboardModel;
  706. beforeEach(() => {
  707. const data = {
  708. panels: [
  709. { id: 1, type: 'graph', gridPos: { x: 0, y: 0, w: 24, h: 2 }, legend: { show: true } },
  710. { id: 3, type: 'graph', gridPos: { x: 0, y: 4, w: 12, h: 2 }, legend: { show: false } },
  711. { id: 4, type: 'graph', gridPos: { x: 12, y: 4, w: 12, h: 2 }, legend: { show: false } },
  712. ],
  713. };
  714. model = new DashboardModel(data);
  715. });
  716. it('toggleLegendsForAll should toggle all legends on on first execution', () => {
  717. model.toggleLegendsForAll();
  718. const legendsOn = model.panels.filter((panel) => panel.legend!.show === true);
  719. expect(legendsOn.length).toBe(3);
  720. });
  721. it('toggleLegendsForAll should toggle all legends off on second execution', () => {
  722. model.toggleLegendsForAll();
  723. model.toggleLegendsForAll();
  724. const legendsOn = model.panels.filter((panel) => panel.legend!.show === true);
  725. expect(legendsOn.length).toBe(0);
  726. });
  727. });
  728. describe('canAddAnnotations', () => {
  729. it.each`
  730. canEdit | canMakeEditable | expected
  731. ${false} | ${false} | ${false}
  732. ${false} | ${true} | ${true}
  733. ${true} | ${false} | ${true}
  734. ${true} | ${true} | ${true}
  735. `(
  736. 'when called with canEdit:{$canEdit}, canMakeEditable:{$canMakeEditable} and expected:{$expected}',
  737. ({ canEdit, canMakeEditable, expected }) => {
  738. const dashboard = new DashboardModel({});
  739. dashboard.meta.canEdit = canEdit;
  740. dashboard.meta.canMakeEditable = canMakeEditable;
  741. const result = dashboard.canEditDashboard();
  742. expect(result).toBe(expected);
  743. }
  744. );
  745. });
  746. describe('canEditPanel', () => {
  747. it('returns false if the dashboard cannot be edited', () => {
  748. const dashboard = new DashboardModel({
  749. panels: [
  750. { id: 1, type: 'row', gridPos: { x: 0, y: 0, w: 24, h: 6 } },
  751. { id: 2, type: 'graph', gridPos: { x: 0, y: 7, w: 12, h: 2 } },
  752. ],
  753. });
  754. dashboard.meta.canEdit = false;
  755. const panel = dashboard.getPanelById(2);
  756. expect(dashboard.canEditPanel(panel)).toBe(false);
  757. });
  758. it('returns false if no panel is passed in', () => {
  759. const dashboard = new DashboardModel({
  760. panels: [
  761. { id: 1, type: 'row', gridPos: { x: 0, y: 0, w: 24, h: 6 } },
  762. { id: 2, type: 'graph', gridPos: { x: 0, y: 7, w: 12, h: 2 } },
  763. ],
  764. });
  765. expect(dashboard.canEditPanel()).toBe(false);
  766. });
  767. it('returns false if the panel is a repeat', () => {
  768. const dashboard = new DashboardModel({
  769. panels: [
  770. { id: 1, type: 'row', gridPos: { x: 0, y: 0, w: 24, h: 6 } },
  771. { id: 2, type: 'graph', gridPos: { x: 0, y: 7, w: 12, h: 2 } },
  772. { id: 3, type: 'graph', gridPos: { x: 0, y: 7, w: 12, h: 2 }, repeatPanelId: 2 },
  773. ],
  774. });
  775. const panel = dashboard.getPanelById(3);
  776. expect(dashboard.canEditPanel(panel)).toBe(false);
  777. });
  778. it('returns false if the panel is a row', () => {
  779. const dashboard = new DashboardModel({
  780. panels: [
  781. { id: 1, type: 'row', gridPos: { x: 0, y: 0, w: 24, h: 6 } },
  782. { id: 2, type: 'graph', gridPos: { x: 0, y: 7, w: 12, h: 2 } },
  783. ],
  784. });
  785. const panel = dashboard.getPanelById(1);
  786. expect(dashboard.canEditPanel(panel)).toBe(false);
  787. });
  788. it('returns true otherwise', () => {
  789. const dashboard = new DashboardModel({
  790. panels: [
  791. { id: 1, type: 'row', gridPos: { x: 0, y: 0, w: 24, h: 6 } },
  792. { id: 2, type: 'graph', gridPos: { x: 0, y: 7, w: 12, h: 2 } },
  793. ],
  794. });
  795. const panel = dashboard.getPanelById(2);
  796. expect(dashboard.canEditPanel(panel)).toBe(true);
  797. });
  798. });
  799. });
  800. describe('exitViewPanel', () => {
  801. function getTestContext() {
  802. const panel: any = { setIsViewing: jest.fn() };
  803. const dashboard = new DashboardModel({});
  804. dashboard.startRefresh = jest.fn();
  805. dashboard.panelInView = panel;
  806. return { dashboard, panel };
  807. }
  808. describe('when called', () => {
  809. it('then panelInView is set to undefined', () => {
  810. const { dashboard, panel } = getTestContext();
  811. dashboard.exitViewPanel(panel);
  812. expect(dashboard.panelInView).toBeUndefined();
  813. });
  814. it('then setIsViewing is called on panel', () => {
  815. const { dashboard, panel } = getTestContext();
  816. dashboard.exitViewPanel(panel);
  817. expect(panel.setIsViewing).toHaveBeenCalledWith(false);
  818. });
  819. it('then startRefresh is not called', () => {
  820. const { dashboard, panel } = getTestContext();
  821. dashboard.exitViewPanel(panel);
  822. expect(dashboard.startRefresh).not.toHaveBeenCalled();
  823. });
  824. });
  825. });
  826. describe('exitPanelEditor', () => {
  827. function getTestContext(pauseAutoRefresh = false) {
  828. const panel: any = { destroy: jest.fn() };
  829. const dashboard = new DashboardModel({});
  830. const timeSrvMock = {
  831. pauseAutoRefresh: jest.fn(),
  832. resumeAutoRefresh: jest.fn(),
  833. setAutoRefresh: jest.fn(),
  834. } as unknown as TimeSrv;
  835. dashboard.startRefresh = jest.fn();
  836. dashboard.panelInEdit = panel;
  837. if (pauseAutoRefresh) {
  838. timeSrvMock.autoRefreshPaused = true;
  839. }
  840. setTimeSrv(timeSrvMock);
  841. return { dashboard, panel, timeSrvMock };
  842. }
  843. describe('when called', () => {
  844. it('then panelInEdit is set to undefined', () => {
  845. const { dashboard } = getTestContext();
  846. dashboard.exitPanelEditor();
  847. expect(dashboard.panelInEdit).toBeUndefined();
  848. });
  849. it('then destroy is called on panel', () => {
  850. const { dashboard, panel } = getTestContext();
  851. dashboard.exitPanelEditor();
  852. expect(panel.destroy).toHaveBeenCalled();
  853. });
  854. it('then startRefresh is not called', () => {
  855. const { dashboard } = getTestContext();
  856. dashboard.exitPanelEditor();
  857. expect(dashboard.startRefresh).not.toHaveBeenCalled();
  858. });
  859. it('then auto refresh property is resumed', () => {
  860. const { dashboard, timeSrvMock } = getTestContext(true);
  861. dashboard.exitPanelEditor();
  862. expect(timeSrvMock.resumeAutoRefresh).toHaveBeenCalled();
  863. });
  864. });
  865. });
  866. describe('initEditPanel', () => {
  867. function getTestContext() {
  868. const dashboard = new DashboardModel({});
  869. const timeSrvMock = {
  870. pauseAutoRefresh: jest.fn(),
  871. resumeAutoRefresh: jest.fn(),
  872. } as unknown as TimeSrv;
  873. setTimeSrv(timeSrvMock);
  874. return { dashboard, timeSrvMock };
  875. }
  876. describe('when called', () => {
  877. it('then panelInEdit is not undefined', () => {
  878. const { dashboard } = getTestContext();
  879. dashboard.addPanel({ type: 'timeseries' });
  880. dashboard.initEditPanel(dashboard.panels[0]);
  881. expect(dashboard.panelInEdit).not.toBeUndefined();
  882. });
  883. it('then auto-refresh is paused', () => {
  884. const { dashboard, timeSrvMock } = getTestContext();
  885. dashboard.addPanel({ type: 'timeseries' });
  886. dashboard.initEditPanel(dashboard.panels[0]);
  887. expect(timeSrvMock.pauseAutoRefresh).toHaveBeenCalled();
  888. });
  889. });
  890. });