dashboard_model.jest.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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(
  183. "2009-02-13 23:31:30"
  184. );
  185. });
  186. it("Should format timestamp with millisecond resolution if format is passed as parameter", function() {
  187. expect(
  188. dashboard.formatDate(1234567890007, "YYYY-MM-DD HH:mm:ss.SSS")
  189. ).toBe("2009-02-13 23:31:30.007");
  190. });
  191. });
  192. describe("updateSubmenuVisibility with empty lists", function() {
  193. var model;
  194. beforeEach(function() {
  195. model = new DashboardModel({});
  196. model.updateSubmenuVisibility();
  197. });
  198. it("should not enable submmenu", function() {
  199. expect(model.meta.submenuEnabled).toBe(false);
  200. });
  201. });
  202. describe("updateSubmenuVisibility with annotation", function() {
  203. var model;
  204. beforeEach(function() {
  205. model = new DashboardModel({
  206. annotations: {
  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 template var", function() {
  217. var model;
  218. beforeEach(function() {
  219. model = new DashboardModel({
  220. templating: {
  221. list: [{}]
  222. }
  223. });
  224. model.updateSubmenuVisibility();
  225. });
  226. it("should enable submmenu", function() {
  227. expect(model.meta.submenuEnabled).toBe(true);
  228. });
  229. });
  230. describe("updateSubmenuVisibility with hidden template var", function() {
  231. var model;
  232. beforeEach(function() {
  233. model = new DashboardModel({
  234. templating: {
  235. list: [{ hide: 2 }]
  236. }
  237. });
  238. model.updateSubmenuVisibility();
  239. });
  240. it("should not enable submmenu", function() {
  241. expect(model.meta.submenuEnabled).toBe(false);
  242. });
  243. });
  244. describe("updateSubmenuVisibility with hidden annotation toggle", function() {
  245. var dashboard;
  246. beforeEach(function() {
  247. dashboard = new DashboardModel({
  248. annotations: {
  249. list: [{ hide: true }]
  250. }
  251. });
  252. dashboard.updateSubmenuVisibility();
  253. });
  254. it("should not enable submmenu", function() {
  255. expect(dashboard.meta.submenuEnabled).toBe(false);
  256. });
  257. });
  258. describe("When collapsing row", function() {
  259. var dashboard;
  260. beforeEach(function() {
  261. dashboard = new DashboardModel({
  262. panels: [
  263. { id: 1, type: "graph", gridPos: { x: 0, y: 0, w: 24, h: 2 } },
  264. { id: 2, type: "row", gridPos: { x: 0, y: 2, w: 24, h: 2 } },
  265. { id: 3, type: "graph", gridPos: { x: 0, y: 4, w: 12, h: 2 } },
  266. { id: 4, type: "graph", gridPos: { x: 12, y: 4, w: 12, h: 2 } },
  267. { id: 5, type: "row", gridPos: { x: 0, y: 6, w: 24, h: 2 } }
  268. ]
  269. });
  270. dashboard.toggleRow(dashboard.panels[1]);
  271. });
  272. it("should remove panels and put them inside collapsed row", function() {
  273. expect(dashboard.panels.length).toBe(3);
  274. expect(dashboard.panels[1].panels.length).toBe(2);
  275. });
  276. describe("and when removing row and its panels", function() {
  277. beforeEach(function() {
  278. dashboard.removeRow(dashboard.panels[1], true);
  279. });
  280. it("should remove row and its panels", function() {
  281. expect(dashboard.panels.length).toBe(2);
  282. });
  283. });
  284. describe("and when removing only the row", function() {
  285. beforeEach(function() {
  286. dashboard.removeRow(dashboard.panels[1], false);
  287. });
  288. it("should only remove row", function() {
  289. expect(dashboard.panels.length).toBe(4);
  290. });
  291. });
  292. });
  293. describe("When expanding row", function() {
  294. var dashboard;
  295. beforeEach(function() {
  296. dashboard = new DashboardModel({
  297. panels: [
  298. { id: 1, type: "graph", gridPos: { x: 0, y: 0, w: 24, h: 6 } },
  299. {
  300. id: 2,
  301. type: "row",
  302. gridPos: { x: 0, y: 6, w: 24, h: 2 },
  303. collapsed: true,
  304. panels: [
  305. { id: 3, type: "graph", gridPos: { x: 0, y: 2, w: 12, h: 2 } },
  306. { id: 4, type: "graph", gridPos: { x: 12, y: 2, w: 12, h: 2 } }
  307. ]
  308. },
  309. { id: 5, type: "row", gridPos: { x: 0, y: 6, w: 1, h: 1 } }
  310. ]
  311. });
  312. dashboard.toggleRow(dashboard.panels[1]);
  313. });
  314. it("should add panels back", function() {
  315. expect(dashboard.panels.length).toBe(5);
  316. });
  317. it("should add them below row in array", function() {
  318. expect(dashboard.panels[2].id).toBe(3);
  319. expect(dashboard.panels[3].id).toBe(4);
  320. });
  321. it("should position them below row", function() {
  322. expect(dashboard.panels[2].gridPos).toMatchObject({
  323. x: 0,
  324. y: 8,
  325. w: 12,
  326. h: 2
  327. });
  328. });
  329. it("should move panels below down", function() {
  330. expect(dashboard.panels[4].gridPos).toMatchObject({
  331. x: 0,
  332. y: 10,
  333. w: 1,
  334. h: 1
  335. });
  336. });
  337. describe("and when removing row and its panels", function() {
  338. beforeEach(function() {
  339. dashboard.removeRow(dashboard.panels[1], true);
  340. });
  341. it("should remove row and its panels", function() {
  342. expect(dashboard.panels.length).toBe(2);
  343. });
  344. });
  345. describe("and when removing only the row", function() {
  346. beforeEach(function() {
  347. dashboard.removeRow(dashboard.panels[1], false);
  348. });
  349. it("should only remove row", function() {
  350. expect(dashboard.panels.length).toBe(4);
  351. });
  352. });
  353. });
  354. });