dashboard_model.jest.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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({
  56. x: 6,
  57. y: 0,
  58. h: 2,
  59. w: 6,
  60. });
  61. });
  62. it('duplicate panel should remove repeat data', function() {
  63. var panel = {
  64. id: 10,
  65. gridPos: { x: 0, y: 0, w: 6, h: 2 },
  66. repeat: 'asd',
  67. scopedVars: { test: 'asd' },
  68. };
  69. dashboard.addPanel(panel);
  70. dashboard.duplicatePanel(dashboard.panels[0]);
  71. expect(dashboard.panels[1].repeat).toBe(undefined);
  72. expect(dashboard.panels[1].scopedVars).toBe(undefined);
  73. });
  74. });
  75. describe('Given editable false dashboard', function() {
  76. var model;
  77. beforeEach(function() {
  78. model = new DashboardModel({ editable: false });
  79. });
  80. it('Should set meta canEdit and canSave to false', function() {
  81. expect(model.meta.canSave).toBe(false);
  82. expect(model.meta.canEdit).toBe(false);
  83. });
  84. it('getSaveModelClone should remove meta', function() {
  85. var clone = model.getSaveModelClone();
  86. expect(clone.meta).toBe(undefined);
  87. });
  88. });
  89. describe('when loading dashboard with old influxdb query schema', function() {
  90. var model;
  91. var target;
  92. beforeEach(function() {
  93. model = new DashboardModel({
  94. panels: [
  95. {
  96. type: 'graph',
  97. grid: {},
  98. yaxes: [{}, {}],
  99. targets: [
  100. {
  101. alias: '$tag_datacenter $tag_source $col',
  102. column: 'value',
  103. measurement: 'logins.count',
  104. fields: [
  105. {
  106. func: 'mean',
  107. name: 'value',
  108. mathExpr: '*2',
  109. asExpr: 'value',
  110. },
  111. {
  112. name: 'one-minute',
  113. func: 'mean',
  114. mathExpr: '*3',
  115. asExpr: 'one-minute',
  116. },
  117. ],
  118. tags: [],
  119. fill: 'previous',
  120. function: 'mean',
  121. groupBy: [
  122. {
  123. interval: 'auto',
  124. type: 'time',
  125. },
  126. {
  127. key: 'source',
  128. type: 'tag',
  129. },
  130. {
  131. type: 'tag',
  132. key: 'datacenter',
  133. },
  134. ],
  135. },
  136. ],
  137. },
  138. ],
  139. });
  140. target = model.panels[0].targets[0];
  141. });
  142. it('should update query schema', function() {
  143. expect(target.fields).toBe(undefined);
  144. expect(target.select.length).toBe(2);
  145. expect(target.select[0].length).toBe(4);
  146. expect(target.select[0][0].type).toBe('field');
  147. expect(target.select[0][1].type).toBe('mean');
  148. expect(target.select[0][2].type).toBe('math');
  149. expect(target.select[0][3].type).toBe('alias');
  150. });
  151. });
  152. describe('when creating dashboard model with missing list for annoations or templating', function() {
  153. var model;
  154. beforeEach(function() {
  155. model = new DashboardModel({
  156. annotations: {
  157. enable: true,
  158. },
  159. templating: {
  160. enable: true,
  161. },
  162. });
  163. });
  164. it('should add empty list', function() {
  165. expect(model.annotations.list.length).toBe(1);
  166. expect(model.templating.list.length).toBe(0);
  167. });
  168. it('should add builtin annotation query', function() {
  169. expect(model.annotations.list[0].builtIn).toBe(1);
  170. expect(model.templating.list.length).toBe(0);
  171. });
  172. });
  173. describe('Formatting epoch timestamp when timezone is set as utc', function() {
  174. var dashboard;
  175. beforeEach(function() {
  176. dashboard = new DashboardModel({ timezone: 'utc' });
  177. });
  178. it('Should format timestamp with second resolution by default', function() {
  179. expect(dashboard.formatDate(1234567890000)).toBe('2009-02-13 23:31:30');
  180. });
  181. it('Should format timestamp with second resolution even if second format is passed as parameter', function() {
  182. expect(dashboard.formatDate(1234567890007, 'YYYY-MM-DD HH:mm:ss')).toBe('2009-02-13 23:31:30');
  183. });
  184. it('Should format timestamp with millisecond resolution if format is passed as parameter', function() {
  185. expect(dashboard.formatDate(1234567890007, 'YYYY-MM-DD HH:mm:ss.SSS')).toBe('2009-02-13 23:31:30.007');
  186. });
  187. });
  188. describe('updateSubmenuVisibility with empty lists', function() {
  189. var model;
  190. beforeEach(function() {
  191. model = new DashboardModel({});
  192. model.updateSubmenuVisibility();
  193. });
  194. it('should not enable submmenu', function() {
  195. expect(model.meta.submenuEnabled).toBe(false);
  196. });
  197. });
  198. describe('updateSubmenuVisibility with annotation', function() {
  199. var model;
  200. beforeEach(function() {
  201. model = new DashboardModel({
  202. annotations: {
  203. list: [{}],
  204. },
  205. });
  206. model.updateSubmenuVisibility();
  207. });
  208. it('should enable submmenu', function() {
  209. expect(model.meta.submenuEnabled).toBe(true);
  210. });
  211. });
  212. describe('updateSubmenuVisibility with template var', function() {
  213. var model;
  214. beforeEach(function() {
  215. model = new DashboardModel({
  216. templating: {
  217. list: [{}],
  218. },
  219. });
  220. model.updateSubmenuVisibility();
  221. });
  222. it('should enable submmenu', function() {
  223. expect(model.meta.submenuEnabled).toBe(true);
  224. });
  225. });
  226. describe('updateSubmenuVisibility with hidden template var', function() {
  227. var model;
  228. beforeEach(function() {
  229. model = new DashboardModel({
  230. templating: {
  231. list: [{ hide: 2 }],
  232. },
  233. });
  234. model.updateSubmenuVisibility();
  235. });
  236. it('should not enable submmenu', function() {
  237. expect(model.meta.submenuEnabled).toBe(false);
  238. });
  239. });
  240. describe('updateSubmenuVisibility with hidden annotation toggle', function() {
  241. var dashboard;
  242. beforeEach(function() {
  243. dashboard = new DashboardModel({
  244. annotations: {
  245. list: [{ hide: true }],
  246. },
  247. });
  248. dashboard.updateSubmenuVisibility();
  249. });
  250. it('should not enable submmenu', function() {
  251. expect(dashboard.meta.submenuEnabled).toBe(false);
  252. });
  253. });
  254. describe('When collapsing row', function() {
  255. var dashboard;
  256. beforeEach(function() {
  257. dashboard = new DashboardModel({
  258. panels: [
  259. { id: 1, type: 'graph', gridPos: { x: 0, y: 0, w: 24, h: 2 } },
  260. { id: 2, type: 'row', gridPos: { x: 0, y: 2, w: 24, h: 2 } },
  261. { id: 3, type: 'graph', gridPos: { x: 0, y: 4, w: 12, h: 2 } },
  262. { id: 4, type: 'graph', gridPos: { x: 12, y: 4, w: 12, h: 2 } },
  263. { id: 5, type: 'row', gridPos: { x: 0, y: 6, w: 24, h: 2 } },
  264. ],
  265. });
  266. dashboard.toggleRow(dashboard.panels[1]);
  267. });
  268. it('should remove panels and put them inside collapsed row', function() {
  269. expect(dashboard.panels.length).toBe(3);
  270. expect(dashboard.panels[1].panels.length).toBe(2);
  271. });
  272. describe('and when removing row and its panels', function() {
  273. beforeEach(function() {
  274. dashboard.removeRow(dashboard.panels[1], true);
  275. });
  276. it('should remove row and its panels', function() {
  277. expect(dashboard.panels.length).toBe(2);
  278. });
  279. });
  280. describe('and when removing only the row', function() {
  281. beforeEach(function() {
  282. dashboard.removeRow(dashboard.panels[1], false);
  283. });
  284. it('should only remove row', function() {
  285. expect(dashboard.panels.length).toBe(4);
  286. });
  287. });
  288. });
  289. describe('When expanding row', function() {
  290. var dashboard;
  291. beforeEach(function() {
  292. dashboard = new DashboardModel({
  293. panels: [
  294. { id: 1, type: 'graph', gridPos: { x: 0, y: 0, w: 24, h: 6 } },
  295. {
  296. id: 2,
  297. type: 'row',
  298. gridPos: { x: 0, y: 6, w: 24, h: 2 },
  299. collapsed: true,
  300. panels: [
  301. { id: 3, type: 'graph', gridPos: { x: 0, y: 2, w: 12, h: 2 } },
  302. { id: 4, type: 'graph', gridPos: { x: 12, y: 2, w: 12, h: 2 } },
  303. ],
  304. },
  305. { id: 5, type: 'row', gridPos: { x: 0, y: 6, w: 1, h: 1 } },
  306. ],
  307. });
  308. dashboard.toggleRow(dashboard.panels[1]);
  309. });
  310. it('should add panels back', function() {
  311. expect(dashboard.panels.length).toBe(5);
  312. });
  313. it('should add them below row in array', function() {
  314. expect(dashboard.panels[2].id).toBe(3);
  315. expect(dashboard.panels[3].id).toBe(4);
  316. });
  317. it('should position them below row', function() {
  318. expect(dashboard.panels[2].gridPos).toMatchObject({
  319. x: 0,
  320. y: 8,
  321. w: 12,
  322. h: 2,
  323. });
  324. });
  325. it('should move panels below down', function() {
  326. expect(dashboard.panels[4].gridPos).toMatchObject({
  327. x: 0,
  328. y: 10,
  329. w: 1,
  330. h: 1,
  331. });
  332. });
  333. describe('and when removing row and its panels', function() {
  334. beforeEach(function() {
  335. dashboard.removeRow(dashboard.panels[1], true);
  336. });
  337. it('should remove row and its panels', function() {
  338. expect(dashboard.panels.length).toBe(2);
  339. });
  340. });
  341. describe('and when removing only the row', function() {
  342. beforeEach(function() {
  343. dashboard.removeRow(dashboard.panels[1], false);
  344. });
  345. it('should only remove row', function() {
  346. expect(dashboard.panels.length).toBe(4);
  347. });
  348. });
  349. });
  350. });