dashboard_model.jest.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 hidden annotation toggle', function() {
  256. var dashboard;
  257. beforeEach(function() {
  258. dashboard = new DashboardModel({
  259. annotations: {
  260. list: [{ hide: true }],
  261. },
  262. });
  263. dashboard.updateSubmenuVisibility();
  264. });
  265. it('should not enable submmenu', function() {
  266. expect(dashboard.meta.submenuEnabled).toBe(false);
  267. });
  268. });
  269. describe('When collapsing row', function() {
  270. var dashboard;
  271. beforeEach(function() {
  272. dashboard = new DashboardModel({
  273. panels: [
  274. { id: 1, type: 'graph', gridPos: { x: 0, y: 0, w: 24, h: 2 } },
  275. { id: 2, type: 'row', gridPos: { x: 0, y: 2, w: 24, h: 2 } },
  276. { id: 3, type: 'graph', gridPos: { x: 0, y: 4, w: 12, h: 2 } },
  277. { id: 4, type: 'graph', gridPos: { x: 12, y: 4, w: 12, h: 2 } },
  278. { id: 5, type: 'row', gridPos: { x: 0, y: 6, w: 24, h: 2 } },
  279. ],
  280. });
  281. dashboard.toggleRow(dashboard.panels[1]);
  282. });
  283. it('should remove panels and put them inside collapsed row', function() {
  284. expect(dashboard.panels.length).toBe(3);
  285. expect(dashboard.panels[1].panels.length).toBe(2);
  286. });
  287. describe('and when removing row and its panels', function() {
  288. beforeEach(function() {
  289. dashboard.removeRow(dashboard.panels[1], true);
  290. });
  291. it('should remove row and its panels', function() {
  292. expect(dashboard.panels.length).toBe(2);
  293. });
  294. });
  295. describe('and when removing only the row', function() {
  296. beforeEach(function() {
  297. dashboard.removeRow(dashboard.panels[1], false);
  298. });
  299. it('should only remove row', function() {
  300. expect(dashboard.panels.length).toBe(4);
  301. });
  302. });
  303. });
  304. describe('When expanding row', function() {
  305. var dashboard;
  306. beforeEach(function() {
  307. dashboard = new DashboardModel({
  308. panels: [
  309. { id: 1, type: 'graph', gridPos: { x: 0, y: 0, w: 24, h: 6 } },
  310. {
  311. id: 2,
  312. type: 'row',
  313. gridPos: { x: 0, y: 6, w: 24, h: 2 },
  314. collapsed: true,
  315. panels: [
  316. { id: 3, type: 'graph', gridPos: { x: 0, y: 2, w: 12, h: 2 } },
  317. { id: 4, type: 'graph', gridPos: { x: 12, y: 2, w: 12, h: 2 } },
  318. ],
  319. },
  320. { id: 5, type: 'row', gridPos: { x: 0, y: 6, w: 1, h: 1 } },
  321. ],
  322. });
  323. dashboard.toggleRow(dashboard.panels[1]);
  324. });
  325. it('should add panels back', function() {
  326. expect(dashboard.panels.length).toBe(5);
  327. });
  328. it('should add them below row in array', function() {
  329. expect(dashboard.panels[2].id).toBe(3);
  330. expect(dashboard.panels[3].id).toBe(4);
  331. });
  332. it('should position them below row', function() {
  333. expect(dashboard.panels[2].gridPos).toMatchObject({
  334. x: 0,
  335. y: 8,
  336. w: 12,
  337. h: 2,
  338. });
  339. });
  340. it('should move panels below down', function() {
  341. expect(dashboard.panels[4].gridPos).toMatchObject({
  342. x: 0,
  343. y: 10,
  344. w: 1,
  345. h: 1,
  346. });
  347. });
  348. describe('and when removing row and its panels', function() {
  349. beforeEach(function() {
  350. dashboard.removeRow(dashboard.panels[1], true);
  351. });
  352. it('should remove row and its panels', function() {
  353. expect(dashboard.panels.length).toBe(2);
  354. });
  355. });
  356. describe('and when removing only the row', function() {
  357. beforeEach(function() {
  358. dashboard.removeRow(dashboard.panels[1], false);
  359. });
  360. it('should only remove row', function() {
  361. expect(dashboard.panels.length).toBe(4);
  362. });
  363. });
  364. });
  365. });