dashboard_model.jest.ts 10 KB

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