dashboard_model.jest.ts 18 KB

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