DashboardModel.test.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. // @ts-ignore
  2. import _ from 'lodash';
  3. import { DashboardModel } from '../state/DashboardModel';
  4. import { PanelModel } from '../state/PanelModel';
  5. jest.mock('app/core/services/context_srv', () => ({}));
  6. describe('DashboardModel', () => {
  7. describe('when creating new dashboard model defaults only', () => {
  8. let model: DashboardModel;
  9. beforeEach(() => {
  10. model = new DashboardModel({}, {});
  11. });
  12. it('should have title', () => {
  13. expect(model.title).toBe('No Title');
  14. });
  15. it('should have meta', () => {
  16. expect(model.meta.canSave).toBe(true);
  17. expect(model.meta.canShare).toBe(true);
  18. });
  19. it('should have default properties', () => {
  20. expect(model.panels.length).toBe(0);
  21. });
  22. });
  23. describe('when getting next panel id', () => {
  24. let model: DashboardModel;
  25. beforeEach(() => {
  26. model = new DashboardModel({
  27. panels: [{ id: 5 }],
  28. });
  29. });
  30. it('should return max id + 1', () => {
  31. expect(model.getNextPanelId()).toBe(6);
  32. });
  33. });
  34. describe('getSaveModelClone', () => {
  35. it('should sort keys', () => {
  36. const model = new DashboardModel({});
  37. const saveModel = model.getSaveModelClone();
  38. const keys = _.keys(saveModel);
  39. expect(keys[0]).toBe('annotations');
  40. expect(keys[1]).toBe('autoUpdate');
  41. });
  42. it('should remove add panel panels', () => {
  43. const model = new DashboardModel({});
  44. model.addPanel({
  45. type: 'add-panel',
  46. });
  47. model.addPanel({
  48. type: 'graph',
  49. });
  50. model.addPanel({
  51. type: 'add-panel',
  52. });
  53. const saveModel = model.getSaveModelClone();
  54. const panels = saveModel.panels;
  55. expect(panels.length).toBe(1);
  56. });
  57. });
  58. describe('row and panel manipulation', () => {
  59. let dashboard: DashboardModel;
  60. beforeEach(() => {
  61. dashboard = new DashboardModel({});
  62. });
  63. it('adding panel should new up panel model', () => {
  64. dashboard.addPanel({ type: 'test', title: 'test' });
  65. expect(dashboard.panels[0] instanceof PanelModel).toBe(true);
  66. });
  67. it('duplicate panel should try to add to the right if there is space', () => {
  68. const panel = { id: 10, gridPos: { x: 0, y: 0, w: 6, h: 2 } };
  69. dashboard.addPanel(panel);
  70. dashboard.duplicatePanel(dashboard.panels[0]);
  71. expect(dashboard.panels[1].gridPos).toMatchObject({
  72. x: 6,
  73. y: 0,
  74. h: 2,
  75. w: 6,
  76. });
  77. });
  78. it('duplicate panel should remove repeat data', () => {
  79. const panel = {
  80. id: 10,
  81. gridPos: { x: 0, y: 0, w: 6, h: 2 },
  82. repeat: 'asd',
  83. scopedVars: { test: 'asd' },
  84. };
  85. dashboard.addPanel(panel);
  86. dashboard.duplicatePanel(dashboard.panels[0]);
  87. expect(dashboard.panels[1].repeat).toBe(undefined);
  88. expect(dashboard.panels[1].scopedVars).toBe(undefined);
  89. });
  90. });
  91. describe('Given editable false dashboard', () => {
  92. let model: DashboardModel;
  93. beforeEach(() => {
  94. model = new DashboardModel({ editable: false });
  95. });
  96. it('Should set meta canEdit and canSave to false', () => {
  97. expect(model.meta.canSave).toBe(false);
  98. expect(model.meta.canEdit).toBe(false);
  99. });
  100. it('getSaveModelClone should remove meta', () => {
  101. const clone = model.getSaveModelClone();
  102. expect(clone.meta).toBe(undefined);
  103. });
  104. });
  105. describe('when loading dashboard with old influxdb query schema', () => {
  106. let model: DashboardModel;
  107. let target: any;
  108. beforeEach(() => {
  109. model = new DashboardModel({
  110. panels: [
  111. {
  112. type: 'graph',
  113. grid: {},
  114. yaxes: [{}, {}],
  115. targets: [
  116. {
  117. alias: '$tag_datacenter $tag_source $col',
  118. column: 'value',
  119. measurement: 'logins.count',
  120. fields: [
  121. {
  122. func: 'mean',
  123. name: 'value',
  124. mathExpr: '*2',
  125. asExpr: 'value',
  126. },
  127. {
  128. name: 'one-minute',
  129. func: 'mean',
  130. mathExpr: '*3',
  131. asExpr: 'one-minute',
  132. },
  133. ],
  134. tags: [],
  135. fill: 'previous',
  136. function: 'mean',
  137. groupBy: [
  138. {
  139. interval: 'auto',
  140. type: 'time',
  141. },
  142. {
  143. key: 'source',
  144. type: 'tag',
  145. },
  146. {
  147. type: 'tag',
  148. key: 'datacenter',
  149. },
  150. ],
  151. },
  152. ],
  153. },
  154. ],
  155. });
  156. target = model.panels[0].targets[0];
  157. });
  158. it('should update query schema', () => {
  159. expect(target.fields).toBe(undefined);
  160. expect(target.select.length).toBe(2);
  161. expect(target.select[0].length).toBe(4);
  162. expect(target.select[0][0].type).toBe('field');
  163. expect(target.select[0][1].type).toBe('mean');
  164. expect(target.select[0][2].type).toBe('math');
  165. expect(target.select[0][3].type).toBe('alias');
  166. });
  167. });
  168. describe('when creating dashboard model with missing list for annoations or templating', () => {
  169. let model: DashboardModel;
  170. beforeEach(() => {
  171. model = new DashboardModel({
  172. annotations: {
  173. enable: true,
  174. },
  175. templating: {
  176. enable: true,
  177. },
  178. });
  179. });
  180. it('should add empty list', () => {
  181. expect(model.annotations.list.length).toBe(1);
  182. expect(model.templating.list.length).toBe(0);
  183. });
  184. it('should add builtin annotation query', () => {
  185. expect(model.annotations.list[0].builtIn).toBe(1);
  186. expect(model.templating.list.length).toBe(0);
  187. });
  188. });
  189. describe('Formatting epoch timestamp when timezone is set as utc', () => {
  190. let dashboard: DashboardModel;
  191. beforeEach(() => {
  192. dashboard = new DashboardModel({ timezone: 'utc' });
  193. });
  194. it('Should format timestamp with second resolution by default', () => {
  195. expect(dashboard.formatDate(1234567890000)).toBe('2009-02-13 23:31:30');
  196. });
  197. it('Should format timestamp with second resolution even if second format is passed as parameter', () => {
  198. expect(dashboard.formatDate(1234567890007, 'YYYY-MM-DD HH:mm:ss')).toBe('2009-02-13 23:31:30');
  199. });
  200. it('Should format timestamp with millisecond resolution if format is passed as parameter', () => {
  201. expect(dashboard.formatDate(1234567890007, 'YYYY-MM-DD HH:mm:ss.SSS')).toBe('2009-02-13 23:31:30.007');
  202. });
  203. });
  204. describe('updateSubmenuVisibility with empty lists', () => {
  205. let model: DashboardModel;
  206. beforeEach(() => {
  207. model = new DashboardModel({});
  208. model.updateSubmenuVisibility();
  209. });
  210. it('should not enable submmenu', () => {
  211. expect(model.meta.submenuEnabled).toBe(false);
  212. });
  213. });
  214. describe('updateSubmenuVisibility with annotation', () => {
  215. let model: DashboardModel;
  216. beforeEach(() => {
  217. model = new DashboardModel({
  218. annotations: {
  219. list: [{}],
  220. },
  221. });
  222. model.updateSubmenuVisibility();
  223. });
  224. it('should enable submmenu', () => {
  225. expect(model.meta.submenuEnabled).toBe(true);
  226. });
  227. });
  228. describe('updateSubmenuVisibility with template var', () => {
  229. let model: DashboardModel;
  230. beforeEach(() => {
  231. model = new DashboardModel({
  232. templating: {
  233. list: [{}],
  234. },
  235. });
  236. model.updateSubmenuVisibility();
  237. });
  238. it('should enable submmenu', () => {
  239. expect(model.meta.submenuEnabled).toBe(true);
  240. });
  241. });
  242. describe('updateSubmenuVisibility with hidden template var', () => {
  243. let model: DashboardModel;
  244. beforeEach(() => {
  245. model = new DashboardModel({
  246. templating: {
  247. list: [{ hide: 2 }],
  248. },
  249. });
  250. model.updateSubmenuVisibility();
  251. });
  252. it('should not enable submmenu', () => {
  253. expect(model.meta.submenuEnabled).toBe(false);
  254. });
  255. });
  256. describe('updateSubmenuVisibility with hidden annotation toggle', () => {
  257. let dashboard: DashboardModel;
  258. beforeEach(() => {
  259. dashboard = new DashboardModel({
  260. annotations: {
  261. list: [{ hide: true }],
  262. },
  263. });
  264. dashboard.updateSubmenuVisibility();
  265. });
  266. it('should not enable submmenu', () => {
  267. expect(dashboard.meta.submenuEnabled).toBe(false);
  268. });
  269. });
  270. describe('When collapsing row', () => {
  271. let dashboard: DashboardModel;
  272. beforeEach(() => {
  273. dashboard = new DashboardModel({
  274. panels: [
  275. { id: 1, type: 'graph', gridPos: { x: 0, y: 0, w: 24, h: 2 } },
  276. { id: 2, type: 'row', gridPos: { x: 0, y: 2, w: 24, h: 2 } },
  277. { id: 3, type: 'graph', gridPos: { x: 0, y: 4, w: 12, h: 2 } },
  278. { id: 4, type: 'graph', gridPos: { x: 12, y: 4, w: 12, h: 2 } },
  279. { id: 5, type: 'row', gridPos: { x: 0, y: 6, w: 24, h: 2 } },
  280. ],
  281. });
  282. dashboard.toggleRow(dashboard.panels[1]);
  283. });
  284. it('should remove panels and put them inside collapsed row', () => {
  285. expect(dashboard.panels.length).toBe(3);
  286. expect(dashboard.panels[1].panels.length).toBe(2);
  287. });
  288. describe('and when removing row and its panels', () => {
  289. beforeEach(() => {
  290. dashboard.removeRow(dashboard.panels[1], true);
  291. });
  292. it('should remove row and its panels', () => {
  293. expect(dashboard.panels.length).toBe(2);
  294. });
  295. });
  296. describe('and when removing only the row', () => {
  297. beforeEach(() => {
  298. dashboard.removeRow(dashboard.panels[1], false);
  299. });
  300. it('should only remove row', () => {
  301. expect(dashboard.panels.length).toBe(4);
  302. });
  303. });
  304. });
  305. describe('When expanding row', () => {
  306. let dashboard: DashboardModel;
  307. beforeEach(() => {
  308. dashboard = new DashboardModel({
  309. panels: [
  310. { id: 1, type: 'graph', gridPos: { x: 0, y: 0, w: 24, h: 6 } },
  311. {
  312. id: 2,
  313. type: 'row',
  314. gridPos: { x: 0, y: 6, w: 24, h: 1 },
  315. collapsed: true,
  316. panels: [
  317. { id: 3, type: 'graph', gridPos: { x: 0, y: 7, w: 12, h: 2 } },
  318. { id: 4, type: 'graph', gridPos: { x: 12, y: 7, w: 12, h: 2 } },
  319. ],
  320. },
  321. { id: 5, type: 'row', gridPos: { x: 0, y: 7, w: 1, h: 1 } },
  322. ],
  323. });
  324. dashboard.toggleRow(dashboard.panels[1]);
  325. });
  326. it('should add panels back', () => {
  327. expect(dashboard.panels.length).toBe(5);
  328. });
  329. it('should add them below row in array', () => {
  330. expect(dashboard.panels[2].id).toBe(3);
  331. expect(dashboard.panels[3].id).toBe(4);
  332. });
  333. it('should position them below row', () => {
  334. expect(dashboard.panels[2].gridPos).toMatchObject({
  335. x: 0,
  336. y: 7,
  337. w: 12,
  338. h: 2,
  339. });
  340. });
  341. it('should move panels below down', () => {
  342. expect(dashboard.panels[4].gridPos).toMatchObject({
  343. x: 0,
  344. y: 9,
  345. w: 1,
  346. h: 1,
  347. });
  348. });
  349. describe('and when removing row and its panels', () => {
  350. beforeEach(() => {
  351. dashboard.removeRow(dashboard.panels[1], true);
  352. });
  353. it('should remove row and its panels', () => {
  354. expect(dashboard.panels.length).toBe(2);
  355. });
  356. });
  357. describe('and when removing only the row', () => {
  358. beforeEach(() => {
  359. dashboard.removeRow(dashboard.panels[1], false);
  360. });
  361. it('should only remove row', () => {
  362. expect(dashboard.panels.length).toBe(4);
  363. });
  364. });
  365. });
  366. describe('Given model with time', () => {
  367. let model: DashboardModel;
  368. beforeEach(() => {
  369. model = new DashboardModel({
  370. time: {
  371. from: 'now-6h',
  372. to: 'now',
  373. },
  374. });
  375. expect(model.hasTimeChanged()).toBeFalsy();
  376. model.time = {
  377. from: 'now-3h',
  378. to: 'now-1h',
  379. };
  380. });
  381. it('hasTimeChanged should be true', () => {
  382. expect(model.hasTimeChanged()).toBeTruthy();
  383. });
  384. it('getSaveModelClone should return original time when saveTimerange=false', () => {
  385. const options = { saveTimerange: false };
  386. const saveModel = model.getSaveModelClone(options);
  387. expect(saveModel.time.from).toBe('now-6h');
  388. expect(saveModel.time.to).toBe('now');
  389. });
  390. it('getSaveModelClone should return updated time when saveTimerange=true', () => {
  391. const options = { saveTimerange: true };
  392. const saveModel = model.getSaveModelClone(options);
  393. expect(saveModel.time.from).toBe('now-3h');
  394. expect(saveModel.time.to).toBe('now-1h');
  395. });
  396. it('hasTimeChanged should be false when reset original time', () => {
  397. model.resetOriginalTime();
  398. expect(model.hasTimeChanged()).toBeFalsy();
  399. });
  400. it('getSaveModelClone should return original time when saveTimerange=false', () => {
  401. const options = { saveTimerange: false };
  402. const saveModel = model.getSaveModelClone(options);
  403. expect(saveModel.time.from).toBe('now-6h');
  404. expect(saveModel.time.to).toBe('now');
  405. });
  406. it('getSaveModelClone should return updated time when saveTimerange=true', () => {
  407. const options = { saveTimerange: true };
  408. const saveModel = model.getSaveModelClone(options);
  409. expect(saveModel.time.from).toBe('now-3h');
  410. expect(saveModel.time.to).toBe('now-1h');
  411. });
  412. });
  413. describe('Given model with template variable of type query', () => {
  414. let model: DashboardModel;
  415. beforeEach(() => {
  416. model = new DashboardModel({
  417. templating: {
  418. list: [
  419. {
  420. name: 'Server',
  421. type: 'query',
  422. current: {
  423. selected: true,
  424. text: 'server_001',
  425. value: 'server_001',
  426. },
  427. },
  428. ],
  429. },
  430. });
  431. expect(model.hasVariableValuesChanged()).toBeFalsy();
  432. });
  433. it('hasVariableValuesChanged should be false when adding a template variable', () => {
  434. model.templating.list.push({
  435. name: 'Server2',
  436. type: 'query',
  437. current: {
  438. selected: true,
  439. text: 'server_002',
  440. value: 'server_002',
  441. },
  442. });
  443. expect(model.hasVariableValuesChanged()).toBeFalsy();
  444. });
  445. it('hasVariableValuesChanged should be false when removing existing template variable', () => {
  446. model.templating.list = [];
  447. expect(model.hasVariableValuesChanged()).toBeFalsy();
  448. });
  449. it('hasVariableValuesChanged should be true when changing value of template variable', () => {
  450. model.templating.list[0].current.text = 'server_002';
  451. expect(model.hasVariableValuesChanged()).toBeTruthy();
  452. });
  453. it('getSaveModelClone should return original variable when saveVariables=false', () => {
  454. model.templating.list[0].current.text = 'server_002';
  455. const options = { saveVariables: false };
  456. const saveModel = model.getSaveModelClone(options);
  457. expect(saveModel.templating.list[0].current.text).toBe('server_001');
  458. });
  459. it('getSaveModelClone should return updated variable when saveVariables=true', () => {
  460. model.templating.list[0].current.text = 'server_002';
  461. const options = { saveVariables: true };
  462. const saveModel = model.getSaveModelClone(options);
  463. expect(saveModel.templating.list[0].current.text).toBe('server_002');
  464. });
  465. });
  466. describe('Given model with template variable of type adhoc', () => {
  467. let model: DashboardModel;
  468. beforeEach(() => {
  469. model = new DashboardModel({
  470. templating: {
  471. list: [
  472. {
  473. name: 'Filter',
  474. type: 'adhoc',
  475. filters: [
  476. {
  477. key: '@hostname',
  478. operator: '=',
  479. value: 'server 20',
  480. },
  481. ],
  482. },
  483. ],
  484. },
  485. });
  486. expect(model.hasVariableValuesChanged()).toBeFalsy();
  487. });
  488. it('hasVariableValuesChanged should be false when adding a template variable', () => {
  489. model.templating.list.push({
  490. name: 'Filter',
  491. type: 'adhoc',
  492. filters: [
  493. {
  494. key: '@hostname',
  495. operator: '=',
  496. value: 'server 1',
  497. },
  498. ],
  499. });
  500. expect(model.hasVariableValuesChanged()).toBeFalsy();
  501. });
  502. it('hasVariableValuesChanged should be false when removing existing template variable', () => {
  503. model.templating.list = [];
  504. expect(model.hasVariableValuesChanged()).toBeFalsy();
  505. });
  506. it('hasVariableValuesChanged should be true when changing value of filter', () => {
  507. model.templating.list[0].filters[0].value = 'server 1';
  508. expect(model.hasVariableValuesChanged()).toBeTruthy();
  509. });
  510. it('hasVariableValuesChanged should be true when adding an additional condition', () => {
  511. model.templating.list[0].filters[0].condition = 'AND';
  512. model.templating.list[0].filters[1] = {
  513. key: '@metric',
  514. operator: '=',
  515. value: 'logins.count',
  516. };
  517. expect(model.hasVariableValuesChanged()).toBeTruthy();
  518. });
  519. it('getSaveModelClone should return original variable when saveVariables=false', () => {
  520. model.templating.list[0].filters[0].value = 'server 1';
  521. const options = { saveVariables: false };
  522. const saveModel = model.getSaveModelClone(options);
  523. expect(saveModel.templating.list[0].filters[0].value).toBe('server 20');
  524. });
  525. it('getSaveModelClone should return updated variable when saveVariables=true', () => {
  526. model.templating.list[0].filters[0].value = 'server 1';
  527. const options = { saveVariables: true };
  528. const saveModel = model.getSaveModelClone(options);
  529. expect(saveModel.templating.list[0].filters[0].value).toBe('server 1');
  530. });
  531. });
  532. describe('Given a dashboard with one panel legend on and two off', () => {
  533. let model: DashboardModel;
  534. beforeEach(() => {
  535. const data = {
  536. panels: [
  537. { id: 1, type: 'graph', gridPos: { x: 0, y: 0, w: 24, h: 2 }, legend: { show: true } },
  538. { id: 3, type: 'graph', gridPos: { x: 0, y: 4, w: 12, h: 2 }, legend: { show: false } },
  539. { id: 4, type: 'graph', gridPos: { x: 12, y: 4, w: 12, h: 2 }, legend: { show: false } },
  540. ],
  541. };
  542. model = new DashboardModel(data);
  543. });
  544. it('toggleLegendsForAll should toggle all legends on on first execution', () => {
  545. model.toggleLegendsForAll();
  546. const legendsOn = model.panels.filter(panel => panel.legend.show === true);
  547. expect(legendsOn.length).toBe(3);
  548. });
  549. it('toggleLegendsForAll should toggle all legends off on second execution', () => {
  550. model.toggleLegendsForAll();
  551. model.toggleLegendsForAll();
  552. const legendsOn = model.panels.filter(panel => panel.legend.show === true);
  553. expect(legendsOn.length).toBe(0);
  554. });
  555. });
  556. });