dashboardSrv-specs.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. beforeEach(function() {
  87. model = _dashboardSrv.create({
  88. services: { filter: { time: { from: 'now-1d', to: 'now'}, list: [{}] }},
  89. pulldowns: [
  90. {type: 'filtering', enable: true},
  91. {type: 'annotations', enable: true, annotations: [{name: 'old'}]}
  92. ],
  93. rows: [
  94. {
  95. panels: [
  96. {
  97. type: 'graphite', legend: true, aliasYAxis: { test: 2 }, grid: { min: 1, max: 10 },
  98. targets: [{refId: 'A'}, {}],
  99. },
  100. {
  101. type: 'singlestat', legend: true, thresholds: '10,20,30', aliasYAxis: { test: 2 }, grid: { min: 1, max: 10 },
  102. targets: [{refId: 'A'}, {}],
  103. }
  104. ]
  105. }
  106. ]
  107. });
  108. graph = model.rows[0].panels[0];
  109. singlestat = model.rows[0].panels[1];
  110. });
  111. it('should have title', function() {
  112. expect(model.title).to.be('No Title');
  113. });
  114. it('should have panel id', function() {
  115. expect(graph.id).to.be(1);
  116. });
  117. it('should move time and filtering list', function() {
  118. expect(model.time.from).to.be('now-1d');
  119. expect(model.templating.list[0].allFormat).to.be('glob');
  120. });
  121. it('graphite panel should change name too graph', function() {
  122. expect(graph.type).to.be('graph');
  123. });
  124. it('single stat panel should have two thresholds', function() {
  125. expect(singlestat.thresholds).to.be('20,30');
  126. });
  127. it('queries without refId should get it', function() {
  128. expect(graph.targets[1].refId).to.be('B');
  129. });
  130. it('update legend setting', function() {
  131. expect(graph.legend.show).to.be(true);
  132. });
  133. it('update grid options', function() {
  134. expect(graph.grid.leftMin).to.be(1);
  135. expect(graph.grid.leftMax).to.be(10);
  136. });
  137. it('move aliasYAxis to series override', function() {
  138. expect(graph.seriesOverrides[0].alias).to.be("test");
  139. expect(graph.seriesOverrides[0].yaxis).to.be(2);
  140. });
  141. it('should move pulldowns to new schema', function() {
  142. expect(model.annotations.list[0].name).to.be('old');
  143. });
  144. it('dashboard schema version should be set to latest', function() {
  145. expect(model.schemaVersion).to.be(9);
  146. });
  147. });
  148. describe('when creating dashboard model with missing list for annoations or templating', function() {
  149. var model;
  150. beforeEach(function() {
  151. model = _dashboardSrv.create({
  152. annotations: {
  153. enable: true,
  154. },
  155. templating: {
  156. enable: true
  157. }
  158. });
  159. });
  160. it('should add empty list', function() {
  161. expect(model.annotations.list.length).to.be(0);
  162. expect(model.templating.list.length).to.be(0);
  163. });
  164. });
  165. describe('Given editable false dashboard', function() {
  166. var model;
  167. beforeEach(function() {
  168. model = _dashboardSrv.create({
  169. editable: false,
  170. });
  171. });
  172. it('Should set meta canEdit and canSave to false', function() {
  173. expect(model.meta.canSave).to.be(false);
  174. expect(model.meta.canEdit).to.be(false);
  175. });
  176. it('getSaveModelClone should remove meta', function() {
  177. var clone = model.getSaveModelClone();
  178. expect(clone.meta).to.be(undefined);
  179. });
  180. });
  181. describe('when loading dashboard with old influxdb query schema', function() {
  182. var model;
  183. var target;
  184. beforeEach(function() {
  185. model = _dashboardSrv.create({
  186. rows: [{
  187. panels: [{
  188. type: 'graph',
  189. targets: [{
  190. "alias": "$tag_datacenter $tag_source $col",
  191. "column": "value",
  192. "measurement": "logins.count",
  193. "fields": [
  194. {
  195. "func": "mean",
  196. "name": "value",
  197. "mathExpr": "*2",
  198. "asExpr": "value"
  199. },
  200. {
  201. "name": "one-minute",
  202. "func": "mean",
  203. "mathExpr": "*3",
  204. "asExpr": "one-minute"
  205. }
  206. ],
  207. "tags": [],
  208. "fill": "previous",
  209. "function": "mean",
  210. "groupBy": [
  211. {
  212. "interval": "auto",
  213. "type": "time"
  214. },
  215. {
  216. "key": "source",
  217. "type": "tag"
  218. },
  219. {
  220. "type": "tag",
  221. "key": "datacenter"
  222. }
  223. ],
  224. }]
  225. }]
  226. }]
  227. });
  228. target = model.rows[0].panels[0].targets[0];
  229. });
  230. it('should update query schema', function() {
  231. expect(target.fields).to.be(undefined);
  232. expect(target.select.length).to.be(2);
  233. expect(target.select[0].length).to.be(4);
  234. expect(target.select[0][0].type).to.be('field');
  235. expect(target.select[0][1].type).to.be('mean');
  236. expect(target.select[0][2].type).to.be('math');
  237. expect(target.select[0][3].type).to.be('alias');
  238. });
  239. });
  240. describe('when creating dashboard model with missing list for annoations or templating', function() {
  241. var model;
  242. beforeEach(function() {
  243. model = _dashboardSrv.create({
  244. annotations: {
  245. enable: true,
  246. },
  247. templating: {
  248. enable: true
  249. }
  250. });
  251. });
  252. it('should add empty list', function() {
  253. expect(model.annotations.list.length).to.be(0);
  254. expect(model.templating.list.length).to.be(0);
  255. });
  256. });
  257. });
  258. });