dashboardSrv-specs.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. define([
  2. 'app/features/dashboard/dashboardSrv'
  3. ], function() {
  4. 'use strict';
  5. describe('dashboardSrv', function() {
  6. var _dashboardSrv;
  7. beforeEach(module('grafana.services'));
  8. beforeEach(inject(function(dashboardSrv) {
  9. _dashboardSrv = dashboardSrv;
  10. }));
  11. describe('when creating new dashboard with defaults only', function() {
  12. var model;
  13. beforeEach(function() {
  14. model = _dashboardSrv.create({}, {});
  15. });
  16. it('should have title', function() {
  17. expect(model.title).to.be('No Title');
  18. });
  19. it('should have meta', function() {
  20. expect(model.meta.canSave).to.be(true);
  21. expect(model.meta.canShare).to.be(true);
  22. });
  23. it('should have default properties', function() {
  24. expect(model.rows.length).to.be(0);
  25. });
  26. });
  27. describe('when getting next panel id', function() {
  28. var model;
  29. beforeEach(function() {
  30. model = _dashboardSrv.create({
  31. rows: [{ panels: [{ id: 5 }]}]
  32. });
  33. });
  34. it('should return max id + 1', function() {
  35. expect(model.getNextPanelId()).to.be(6);
  36. });
  37. });
  38. describe('row and panel manipulation', function() {
  39. var dashboard;
  40. beforeEach(function() {
  41. dashboard = _dashboardSrv.create({});
  42. });
  43. it('row span should sum spans', function() {
  44. var spanLeft = dashboard.rowSpan({ panels: [{ span: 2 }, { span: 3 }] });
  45. expect(spanLeft).to.be(5);
  46. });
  47. it('adding default should split span in half', function() {
  48. dashboard.rows = [{ panels: [{ span: 12, id: 7 }] }];
  49. dashboard.addPanel({span: 4}, dashboard.rows[0]);
  50. expect(dashboard.rows[0].panels[0].span).to.be(6);
  51. expect(dashboard.rows[0].panels[1].span).to.be(6);
  52. expect(dashboard.rows[0].panels[1].id).to.be(8);
  53. });
  54. it('duplicate panel should try to add it to same row', function() {
  55. var panel = { span: 4, attr: '123', id: 10 };
  56. dashboard.rows = [{ panels: [panel] }];
  57. dashboard.duplicatePanel(panel, dashboard.rows[0]);
  58. expect(dashboard.rows[0].panels[0].span).to.be(4);
  59. expect(dashboard.rows[0].panels[1].span).to.be(4);
  60. expect(dashboard.rows[0].panels[1].attr).to.be('123');
  61. expect(dashboard.rows[0].panels[1].id).to.be(11);
  62. });
  63. it('duplicate panel should remove repeat data', function() {
  64. var panel = { span: 4, attr: '123', id: 10, repeat: 'asd', scopedVars: { test: 'asd' }};
  65. dashboard.rows = [{ panels: [panel] }];
  66. dashboard.duplicatePanel(panel, dashboard.rows[0]);
  67. expect(dashboard.rows[0].panels[1].repeat).to.be(undefined);
  68. expect(dashboard.rows[0].panels[1].scopedVars).to.be(undefined);
  69. });
  70. });
  71. describe('when creating dashboard with editable false', function() {
  72. var model;
  73. beforeEach(function() {
  74. model = _dashboardSrv.create({
  75. editable: false
  76. });
  77. });
  78. it('should set editable false', function() {
  79. expect(model.editable).to.be(false);
  80. });
  81. });
  82. describe('when creating dashboard with old schema', function() {
  83. var model;
  84. var graph;
  85. var singlestat;
  86. var table;
  87. beforeEach(function() {
  88. model = _dashboardSrv.create({
  89. services: { filter: { time: { from: 'now-1d', to: 'now'}, list: [{}] }},
  90. pulldowns: [
  91. {type: 'filtering', enable: true},
  92. {type: 'annotations', enable: true, annotations: [{name: 'old'}]}
  93. ],
  94. rows: [
  95. {
  96. panels: [
  97. {
  98. type: 'graphite', legend: true, aliasYAxis: { test: 2 }, grid: { min: 1, max: 10 },
  99. targets: [{refId: 'A'}, {}],
  100. },
  101. {
  102. type: 'singlestat', legend: true, thresholds: '10,20,30', aliasYAxis: { test: 2 }, grid: { min: 1, max: 10 },
  103. targets: [{refId: 'A'}, {}],
  104. },
  105. {
  106. type: 'table', legend: true, styles: [{ thresholds: ["10", "20", "30"]}, { thresholds: ["100", "200", "300"]}],
  107. targets: [{refId: 'A'}, {}],
  108. }
  109. ]
  110. }
  111. ]
  112. });
  113. graph = model.rows[0].panels[0];
  114. singlestat = model.rows[0].panels[1];
  115. table = model.rows[0].panels[2];
  116. });
  117. it('should have title', function() {
  118. expect(model.title).to.be('No Title');
  119. });
  120. it('should have panel id', function() {
  121. expect(graph.id).to.be(1);
  122. });
  123. it('should move time and filtering list', function() {
  124. expect(model.time.from).to.be('now-1d');
  125. expect(model.templating.list[0].allFormat).to.be('glob');
  126. });
  127. it('graphite panel should change name too graph', function() {
  128. expect(graph.type).to.be('graph');
  129. });
  130. it('single stat panel should have two thresholds', function() {
  131. expect(singlestat.thresholds).to.be('20,30');
  132. });
  133. it('queries without refId should get it', function() {
  134. expect(graph.targets[1].refId).to.be('B');
  135. });
  136. it('update legend setting', function() {
  137. expect(graph.legend.show).to.be(true);
  138. });
  139. it('update grid options', function() {
  140. expect(graph.grid.leftMin).to.be(1);
  141. expect(graph.grid.leftMax).to.be(10);
  142. });
  143. it('move aliasYAxis to series override', function() {
  144. expect(graph.seriesOverrides[0].alias).to.be("test");
  145. expect(graph.seriesOverrides[0].yaxis).to.be(2);
  146. });
  147. it('should move pulldowns to new schema', function() {
  148. expect(model.annotations.list[0].name).to.be('old');
  149. });
  150. it('table panel should only have two thresholds values', function() {
  151. expect(table.styles[0].thresholds[0]).to.be("20");
  152. expect(table.styles[0].thresholds[1]).to.be("30");
  153. expect(table.styles[1].thresholds[0]).to.be("200");
  154. expect(table.styles[1].thresholds[1]).to.be("300");
  155. });
  156. it('dashboard schema version should be set to latest', function() {
  157. expect(model.schemaVersion).to.be(10);
  158. });
  159. });
  160. describe('when creating dashboard model with missing list for annoations or templating', function() {
  161. var model;
  162. beforeEach(function() {
  163. model = _dashboardSrv.create({
  164. annotations: {
  165. enable: true,
  166. },
  167. templating: {
  168. enable: true
  169. }
  170. });
  171. });
  172. it('should add empty list', function() {
  173. expect(model.annotations.list.length).to.be(0);
  174. expect(model.templating.list.length).to.be(0);
  175. });
  176. });
  177. describe('Given editable false dashboard', function() {
  178. var model;
  179. beforeEach(function() {
  180. model = _dashboardSrv.create({
  181. editable: false,
  182. });
  183. });
  184. it('Should set meta canEdit and canSave to false', function() {
  185. expect(model.meta.canSave).to.be(false);
  186. expect(model.meta.canEdit).to.be(false);
  187. });
  188. it('getSaveModelClone should remove meta', function() {
  189. var clone = model.getSaveModelClone();
  190. expect(clone.meta).to.be(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 = _dashboardSrv.create({
  198. rows: [{
  199. panels: [{
  200. type: 'graph',
  201. targets: [{
  202. "alias": "$tag_datacenter $tag_source $col",
  203. "column": "value",
  204. "measurement": "logins.count",
  205. "fields": [
  206. {
  207. "func": "mean",
  208. "name": "value",
  209. "mathExpr": "*2",
  210. "asExpr": "value"
  211. },
  212. {
  213. "name": "one-minute",
  214. "func": "mean",
  215. "mathExpr": "*3",
  216. "asExpr": "one-minute"
  217. }
  218. ],
  219. "tags": [],
  220. "fill": "previous",
  221. "function": "mean",
  222. "groupBy": [
  223. {
  224. "interval": "auto",
  225. "type": "time"
  226. },
  227. {
  228. "key": "source",
  229. "type": "tag"
  230. },
  231. {
  232. "type": "tag",
  233. "key": "datacenter"
  234. }
  235. ],
  236. }]
  237. }]
  238. }]
  239. });
  240. target = model.rows[0].panels[0].targets[0];
  241. });
  242. it('should update query schema', function() {
  243. expect(target.fields).to.be(undefined);
  244. expect(target.select.length).to.be(2);
  245. expect(target.select[0].length).to.be(4);
  246. expect(target.select[0][0].type).to.be('field');
  247. expect(target.select[0][1].type).to.be('mean');
  248. expect(target.select[0][2].type).to.be('math');
  249. expect(target.select[0][3].type).to.be('alias');
  250. });
  251. });
  252. describe('when creating dashboard model with missing list for annoations or templating', function() {
  253. var model;
  254. beforeEach(function() {
  255. model = _dashboardSrv.create({
  256. annotations: {
  257. enable: true,
  258. },
  259. templating: {
  260. enable: true
  261. }
  262. });
  263. });
  264. it('should add empty list', function() {
  265. expect(model.annotations.list.length).to.be(0);
  266. expect(model.templating.list.length).to.be(0);
  267. });
  268. });
  269. });
  270. });