dashboardSrv-specs.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. define([
  2. '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. expect(model.nav.length).to.be(1);
  26. });
  27. });
  28. describe('when getting next panel id', function() {
  29. var model;
  30. beforeEach(function() {
  31. model = _dashboardSrv.create({
  32. rows: [{ panels: [{ id: 5 }]}]
  33. });
  34. });
  35. it('should return max id + 1', function() {
  36. expect(model.getNextPanelId()).to.be(6);
  37. });
  38. });
  39. describe('addDataQueryTo', function() {
  40. var dashboard, panel;
  41. beforeEach(function() {
  42. panel = {targets:[]};
  43. dashboard = _dashboardSrv.create({});
  44. dashboard.rows.push({panels: [panel]});
  45. });
  46. it('should add target', function() {
  47. dashboard.addDataQueryTo(panel);
  48. expect(panel.targets.length).to.be(1);
  49. });
  50. it('should set refId', function() {
  51. dashboard.addDataQueryTo(panel);
  52. expect(panel.targets[0].refId).to.be('A');
  53. });
  54. it('should set refId to first available letter', function() {
  55. panel.targets = [{refId: 'A'}];
  56. dashboard.addDataQueryTo(panel);
  57. expect(panel.targets[1].refId).to.be('B');
  58. });
  59. it('duplicate should get unique refId', function() {
  60. panel.targets = [{refId: 'A'}];
  61. dashboard.duplicateDataQuery(panel, panel.targets[0]);
  62. expect(panel.targets[1].refId).to.be('B');
  63. });
  64. });
  65. describe('row and panel manipulation', function() {
  66. var dashboard;
  67. beforeEach(function() {
  68. dashboard = _dashboardSrv.create({});
  69. });
  70. it('row span should sum spans', function() {
  71. var spanLeft = dashboard.rowSpan({ panels: [{ span: 2 }, { span: 3 }] });
  72. expect(spanLeft).to.be(5);
  73. });
  74. it('adding default should split span in half', function() {
  75. dashboard.rows = [{ panels: [{ span: 12, id: 7 }] }];
  76. dashboard.add_panel({span: 4}, dashboard.rows[0]);
  77. expect(dashboard.rows[0].panels[0].span).to.be(6);
  78. expect(dashboard.rows[0].panels[1].span).to.be(6);
  79. expect(dashboard.rows[0].panels[1].id).to.be(8);
  80. });
  81. it('duplicate panel should try to add it to same row', function() {
  82. var panel = { span: 4, attr: '123', id: 10 };
  83. dashboard.rows = [{ panels: [panel] }];
  84. dashboard.duplicatePanel(panel, dashboard.rows[0]);
  85. expect(dashboard.rows[0].panels[0].span).to.be(4);
  86. expect(dashboard.rows[0].panels[1].span).to.be(4);
  87. expect(dashboard.rows[0].panels[1].attr).to.be('123');
  88. expect(dashboard.rows[0].panels[1].id).to.be(11);
  89. });
  90. it('duplicate panel should remove repeat data', function() {
  91. var panel = { span: 4, attr: '123', id: 10, repeat: 'asd', scopedVars: { test: 'asd' }};
  92. dashboard.rows = [{ panels: [panel] }];
  93. dashboard.duplicatePanel(panel, dashboard.rows[0]);
  94. expect(dashboard.rows[0].panels[1].repeat).to.be(undefined);
  95. expect(dashboard.rows[0].panels[1].scopedVars).to.be(undefined);
  96. });
  97. });
  98. describe('when creating dashboard with editable false', function() {
  99. var model;
  100. beforeEach(function() {
  101. model = _dashboardSrv.create({
  102. editable: false
  103. });
  104. });
  105. it('should set editable false', function() {
  106. expect(model.editable).to.be(false);
  107. });
  108. });
  109. describe('when creating dashboard with old schema', function() {
  110. var model;
  111. var graph;
  112. beforeEach(function() {
  113. model = _dashboardSrv.create({
  114. services: { filter: { time: { from: 'now-1d', to: 'now'}, list: [{}] }},
  115. pulldowns: [
  116. {type: 'filtering', enable: true},
  117. {type: 'annotations', enable: true, annotations: [{name: 'old'}]}
  118. ],
  119. rows: [
  120. {
  121. panels: [
  122. {type: 'graphite', legend: true, aliasYAxis: { test: 2 }, grid: { min: 1, max: 10 }}
  123. ]
  124. }
  125. ]
  126. });
  127. graph = model.rows[0].panels[0];
  128. });
  129. it('should have title', function() {
  130. expect(model.title).to.be('No Title');
  131. });
  132. it('should have panel id', function() {
  133. expect(graph.id).to.be(1);
  134. });
  135. it('should move time and filtering list', function() {
  136. expect(model.time.from).to.be('now-1d');
  137. expect(model.templating.list[0].allFormat).to.be('glob');
  138. });
  139. it('graphite panel should change name too graph', function() {
  140. expect(graph.type).to.be('graph');
  141. });
  142. it('update legend setting', function() {
  143. expect(graph.legend.show).to.be(true);
  144. });
  145. it('update grid options', function() {
  146. expect(graph.grid.leftMin).to.be(1);
  147. expect(graph.grid.leftMax).to.be(10);
  148. });
  149. it('move aliasYAxis to series override', function() {
  150. expect(graph.seriesOverrides[0].alias).to.be("test");
  151. expect(graph.seriesOverrides[0].yaxis).to.be(2);
  152. });
  153. it('should move pulldowns to new schema', function() {
  154. expect(model.annotations.list[0].name).to.be('old');
  155. });
  156. it('dashboard schema version should be set to latest', function() {
  157. expect(model.schemaVersion).to.be(6);
  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. });
  194. });