dashboardSrv-specs.js 9.5 KB

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