dashboard_model.jest.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. });
  42. describe('row and panel manipulation', function() {
  43. var dashboard;
  44. beforeEach(function() {
  45. dashboard = new DashboardModel({});
  46. });
  47. it('adding panel should new up panel model', function() {
  48. dashboard.addPanel({ type: 'test', title: 'test' });
  49. expect(dashboard.panels[0] instanceof PanelModel).toBe(true);
  50. });
  51. it('duplicate panel should try to add to the right if there is space', function() {
  52. var panel = { id: 10, gridPos: { x: 0, y: 0, w: 6, h: 2 } };
  53. dashboard.addPanel(panel);
  54. dashboard.duplicatePanel(dashboard.panels[0]);
  55. expect(dashboard.panels[1].gridPos).toMatchObject({ x: 6, y: 0, h: 2, w: 6 });
  56. });
  57. it('duplicate panel should remove repeat data', function() {
  58. var panel = { id: 10, gridPos: { x: 0, y: 0, w: 6, h: 2 }, repeat: 'asd', scopedVars: { test: 'asd' } };
  59. dashboard.addPanel(panel);
  60. dashboard.duplicatePanel(dashboard.panels[0]);
  61. expect(dashboard.panels[1].repeat).toBe(undefined);
  62. expect(dashboard.panels[1].scopedVars).toBe(undefined);
  63. });
  64. });
  65. describe('when creating dashboard with old schema', function() {
  66. var model;
  67. var graph;
  68. var singlestat;
  69. var table;
  70. beforeEach(function() {
  71. model = new DashboardModel({
  72. services: { filter: { time: { from: 'now-1d', to: 'now' }, list: [{}] } },
  73. pulldowns: [
  74. { type: 'filtering', enable: true },
  75. { type: 'annotations', enable: true, annotations: [{ name: 'old' }] },
  76. ],
  77. panels: [
  78. {
  79. type: 'graph',
  80. legend: true,
  81. aliasYAxis: { test: 2 },
  82. y_formats: ['kbyte', 'ms'],
  83. grid: {
  84. min: 1,
  85. max: 10,
  86. rightMin: 5,
  87. rightMax: 15,
  88. leftLogBase: 1,
  89. rightLogBase: 2,
  90. threshold1: 200,
  91. threshold2: 400,
  92. threshold1Color: 'yellow',
  93. threshold2Color: 'red',
  94. },
  95. leftYAxisLabel: 'left label',
  96. targets: [{ refId: 'A' }, {}],
  97. },
  98. {
  99. type: 'singlestat',
  100. legend: true,
  101. thresholds: '10,20,30',
  102. aliasYAxis: { test: 2 },
  103. grid: { min: 1, max: 10 },
  104. targets: [{ refId: 'A' }, {}],
  105. },
  106. {
  107. type: 'table',
  108. legend: true,
  109. styles: [{ thresholds: ['10', '20', '30'] }, { thresholds: ['100', '200', '300'] }],
  110. targets: [{ refId: 'A' }, {}],
  111. },
  112. ],
  113. });
  114. graph = model.panels[0];
  115. singlestat = model.panels[1];
  116. table = model.panels[2];
  117. });
  118. it('should have title', function() {
  119. expect(model.title).toBe('No Title');
  120. });
  121. it('should have panel id', function() {
  122. expect(graph.id).toBe(1);
  123. });
  124. it('should move time and filtering list', function() {
  125. expect(model.time.from).toBe('now-1d');
  126. expect(model.templating.list[0].allFormat).toBe('glob');
  127. });
  128. it('graphite panel should change name too graph', function() {
  129. expect(graph.type).toBe('graph');
  130. });
  131. it('single stat panel should have two thresholds', function() {
  132. expect(singlestat.thresholds).toBe('20,30');
  133. });
  134. it('queries without refId should get it', function() {
  135. expect(graph.targets[1].refId).toBe('B');
  136. });
  137. it('update legend setting', function() {
  138. expect(graph.legend.show).toBe(true);
  139. });
  140. it('move aliasYAxis to series override', function() {
  141. expect(graph.seriesOverrides[0].alias).toBe('test');
  142. expect(graph.seriesOverrides[0].yaxis).toBe(2);
  143. });
  144. it('should move pulldowns to new schema', function() {
  145. expect(model.annotations.list[1].name).toBe('old');
  146. });
  147. it('table panel should only have two thresholds values', function() {
  148. expect(table.styles[0].thresholds[0]).toBe('20');
  149. expect(table.styles[0].thresholds[1]).toBe('30');
  150. expect(table.styles[1].thresholds[0]).toBe('200');
  151. expect(table.styles[1].thresholds[1]).toBe('300');
  152. });
  153. it('graph grid to yaxes options', function() {
  154. expect(graph.yaxes[0].min).toBe(1);
  155. expect(graph.yaxes[0].max).toBe(10);
  156. expect(graph.yaxes[0].format).toBe('kbyte');
  157. expect(graph.yaxes[0].label).toBe('left label');
  158. expect(graph.yaxes[0].logBase).toBe(1);
  159. expect(graph.yaxes[1].min).toBe(5);
  160. expect(graph.yaxes[1].max).toBe(15);
  161. expect(graph.yaxes[1].format).toBe('ms');
  162. expect(graph.yaxes[1].logBase).toBe(2);
  163. expect(graph.grid.rightMax).toBe(undefined);
  164. expect(graph.grid.rightLogBase).toBe(undefined);
  165. expect(graph.y_formats).toBe(undefined);
  166. });
  167. it('dashboard schema version should be set to latest', function() {
  168. expect(model.schemaVersion).toBe(16);
  169. });
  170. it('graph thresholds should be migrated', function() {
  171. expect(graph.thresholds.length).toBe(2);
  172. expect(graph.thresholds[0].op).toBe('gt');
  173. expect(graph.thresholds[0].value).toBe(200);
  174. expect(graph.thresholds[0].fillColor).toBe('yellow');
  175. expect(graph.thresholds[1].value).toBe(400);
  176. expect(graph.thresholds[1].fillColor).toBe('red');
  177. });
  178. });
  179. describe('Given editable false dashboard', function() {
  180. var model;
  181. beforeEach(function() {
  182. model = new DashboardModel({ editable: false });
  183. });
  184. it('Should set meta canEdit and canSave to false', function() {
  185. expect(model.meta.canSave).toBe(false);
  186. expect(model.meta.canEdit).toBe(false);
  187. });
  188. it('getSaveModelClone should remove meta', function() {
  189. var clone = model.getSaveModelClone();
  190. expect(clone.meta).toBe(undefined);
  191. });
  192. });
  193. describe('when loading dashboard with old influxdb query schema', function() {
  194. var model;
  195. var target;
  196. beforeEach(function() {
  197. model = new DashboardModel({
  198. panels: [
  199. {
  200. type: 'graph',
  201. grid: {},
  202. yaxes: [{}, {}],
  203. targets: [
  204. {
  205. alias: '$tag_datacenter $tag_source $col',
  206. column: 'value',
  207. measurement: 'logins.count',
  208. fields: [
  209. {
  210. func: 'mean',
  211. name: 'value',
  212. mathExpr: '*2',
  213. asExpr: 'value',
  214. },
  215. {
  216. name: 'one-minute',
  217. func: 'mean',
  218. mathExpr: '*3',
  219. asExpr: 'one-minute',
  220. },
  221. ],
  222. tags: [],
  223. fill: 'previous',
  224. function: 'mean',
  225. groupBy: [
  226. {
  227. interval: 'auto',
  228. type: 'time',
  229. },
  230. {
  231. key: 'source',
  232. type: 'tag',
  233. },
  234. {
  235. type: 'tag',
  236. key: 'datacenter',
  237. },
  238. ],
  239. },
  240. ],
  241. },
  242. ],
  243. });
  244. target = model.panels[0].targets[0];
  245. });
  246. it('should update query schema', function() {
  247. expect(target.fields).toBe(undefined);
  248. expect(target.select.length).toBe(2);
  249. expect(target.select[0].length).toBe(4);
  250. expect(target.select[0][0].type).toBe('field');
  251. expect(target.select[0][1].type).toBe('mean');
  252. expect(target.select[0][2].type).toBe('math');
  253. expect(target.select[0][3].type).toBe('alias');
  254. });
  255. });
  256. describe('when creating dashboard model with missing list for annoations or templating', function() {
  257. var model;
  258. beforeEach(function() {
  259. model = new DashboardModel({
  260. annotations: {
  261. enable: true,
  262. },
  263. templating: {
  264. enable: true,
  265. },
  266. });
  267. });
  268. it('should add empty list', function() {
  269. expect(model.annotations.list.length).toBe(1);
  270. expect(model.templating.list.length).toBe(0);
  271. });
  272. it('should add builtin annotation query', function() {
  273. expect(model.annotations.list[0].builtIn).toBe(1);
  274. expect(model.templating.list.length).toBe(0);
  275. });
  276. });
  277. describe('Formatting epoch timestamp when timezone is set as utc', function() {
  278. var dashboard;
  279. beforeEach(function() {
  280. dashboard = new DashboardModel({ timezone: 'utc' });
  281. });
  282. it('Should format timestamp with second resolution by default', function() {
  283. expect(dashboard.formatDate(1234567890000)).toBe('2009-02-13 23:31:30');
  284. });
  285. it('Should format timestamp with second resolution even if second format is passed as parameter', function() {
  286. expect(dashboard.formatDate(1234567890007, 'YYYY-MM-DD HH:mm:ss')).toBe('2009-02-13 23:31:30');
  287. });
  288. it('Should format timestamp with millisecond resolution if format is passed as parameter', function() {
  289. expect(dashboard.formatDate(1234567890007, 'YYYY-MM-DD HH:mm:ss.SSS')).toBe('2009-02-13 23:31:30.007');
  290. });
  291. });
  292. describe('updateSubmenuVisibility with empty lists', function() {
  293. var model;
  294. beforeEach(function() {
  295. model = new DashboardModel({});
  296. model.updateSubmenuVisibility();
  297. });
  298. it('should not enable submmenu', function() {
  299. expect(model.meta.submenuEnabled).toBe(false);
  300. });
  301. });
  302. describe('updateSubmenuVisibility with annotation', function() {
  303. var model;
  304. beforeEach(function() {
  305. model = new DashboardModel({
  306. annotations: {
  307. list: [{}],
  308. },
  309. });
  310. model.updateSubmenuVisibility();
  311. });
  312. it('should enable submmenu', function() {
  313. expect(model.meta.submenuEnabled).toBe(true);
  314. });
  315. });
  316. describe('updateSubmenuVisibility with template var', function() {
  317. var model;
  318. beforeEach(function() {
  319. model = new DashboardModel({
  320. templating: {
  321. list: [{}],
  322. },
  323. });
  324. model.updateSubmenuVisibility();
  325. });
  326. it('should enable submmenu', function() {
  327. expect(model.meta.submenuEnabled).toBe(true);
  328. });
  329. });
  330. describe('updateSubmenuVisibility with hidden template var', function() {
  331. var model;
  332. beforeEach(function() {
  333. model = new DashboardModel({
  334. templating: {
  335. list: [{ hide: 2 }],
  336. },
  337. });
  338. model.updateSubmenuVisibility();
  339. });
  340. it('should not enable submmenu', function() {
  341. expect(model.meta.submenuEnabled).toBe(false);
  342. });
  343. });
  344. describe('updateSubmenuVisibility with hidden annotation toggle', function() {
  345. var dashboard;
  346. beforeEach(function() {
  347. dashboard = new DashboardModel({
  348. annotations: {
  349. list: [{ hide: true }],
  350. },
  351. });
  352. dashboard.updateSubmenuVisibility();
  353. });
  354. it('should not enable submmenu', function() {
  355. expect(dashboard.meta.submenuEnabled).toBe(false);
  356. });
  357. });
  358. describe('When collapsing row', function() {
  359. var dashboard;
  360. beforeEach(function() {
  361. dashboard = new DashboardModel({
  362. panels: [
  363. { id: 1, type: 'graph', gridPos: { x: 0, y: 0, w: 24, h: 2 } },
  364. { id: 2, type: 'row', gridPos: { x: 0, y: 2, w: 24, h: 2 } },
  365. { id: 3, type: 'graph', gridPos: { x: 0, y: 4, w: 12, h: 2 } },
  366. { id: 4, type: 'graph', gridPos: { x: 12, y: 4, w: 12, h: 2 } },
  367. { id: 5, type: 'row', gridPos: { x: 0, y: 6, w: 24, h: 2 } },
  368. ],
  369. });
  370. dashboard.toggleRow(dashboard.panels[1]);
  371. });
  372. it('should remove panels and put them inside collapsed row', function() {
  373. expect(dashboard.panels.length).toBe(3);
  374. expect(dashboard.panels[1].panels.length).toBe(2);
  375. });
  376. });
  377. describe('When expanding row', function() {
  378. var dashboard;
  379. beforeEach(function() {
  380. dashboard = new DashboardModel({
  381. panels: [
  382. { id: 1, type: 'graph', gridPos: { x: 0, y: 0, w: 24, h: 6 } },
  383. {
  384. id: 2,
  385. type: 'row',
  386. gridPos: { x: 0, y: 6, w: 24, h: 2 },
  387. collapsed: true,
  388. panels: [
  389. { id: 3, type: 'graph', gridPos: { x: 0, y: 2, w: 12, h: 2 } },
  390. { id: 4, type: 'graph', gridPos: { x: 12, y: 2, w: 12, h: 2 } },
  391. ],
  392. },
  393. { id: 5, type: 'graph', gridPos: { x: 0, y: 6, w: 1, h: 1 } },
  394. ],
  395. });
  396. dashboard.toggleRow(dashboard.panels[1]);
  397. });
  398. it('should add panels back', function() {
  399. expect(dashboard.panels.length).toBe(5);
  400. });
  401. it('should add them below row in array', function() {
  402. expect(dashboard.panels[2].id).toBe(3);
  403. expect(dashboard.panels[3].id).toBe(4);
  404. });
  405. it('should position them below row', function() {
  406. expect(dashboard.panels[2].gridPos).toMatchObject({ x: 0, y: 8, w: 12, h: 2 });
  407. });
  408. it('should move panels below down', function() {
  409. expect(dashboard.panels[4].gridPos).toMatchObject({ x: 0, y: 10, w: 1, h: 1 });
  410. });
  411. });
  412. });