dynamicDashboardSrv-specs.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. define([
  2. 'app/features/dashboard/dynamicDashboardSrv',
  3. 'app/features/dashboard/dashboardSrv'
  4. ], function() {
  5. 'use strict';
  6. function dynamicDashScenario(desc, func) {
  7. describe(desc, function() {
  8. var ctx = {};
  9. ctx.setup = function (setupFunc) {
  10. beforeEach(module('grafana.services'));
  11. beforeEach(inject(function(dynamicDashboardSrv, dashboardSrv) {
  12. ctx.dynamicDashboardSrv = dynamicDashboardSrv;
  13. ctx.dashboardSrv = dashboardSrv;
  14. var model = {
  15. rows: [],
  16. templating: { list: [] }
  17. };
  18. setupFunc(model);
  19. ctx.dash = ctx.dashboardSrv.create(model);
  20. ctx.dynamicDashboardSrv.init(ctx.dash);
  21. ctx.rows = ctx.dash.rows;
  22. }));
  23. };
  24. func(ctx);
  25. });
  26. }
  27. dynamicDashScenario('given dashboard with panel repeat', function(ctx) {
  28. ctx.setup(function(dash) {
  29. dash.rows.push({
  30. panels: [{id: 2, repeat: 'apps'}]
  31. });
  32. dash.templating.list.push({
  33. name: 'apps',
  34. current: {
  35. text: 'se1, se2, se3',
  36. value: ['se1', 'se2', 'se3']
  37. },
  38. options: [
  39. {text: 'se1', value: 'se1', selected: true},
  40. {text: 'se2', value: 'se2', selected: true},
  41. {text: 'se3', value: 'se3', selected: true},
  42. {text: 'se4', value: 'se4', selected: false}
  43. ]
  44. });
  45. });
  46. it('should repeat panel one time', function() {
  47. expect(ctx.rows[0].panels.length).to.be(3);
  48. });
  49. it('should mark panel repeated', function() {
  50. expect(ctx.rows[0].panels[0].repeat).to.be('apps');
  51. expect(ctx.rows[0].panels[1].repeatPanelId).to.be(2);
  52. });
  53. it('should set scopedVars on panels', function() {
  54. expect(ctx.rows[0].panels[0].scopedVars.apps.value).to.be('se1');
  55. expect(ctx.rows[0].panels[1].scopedVars.apps.value).to.be('se2');
  56. expect(ctx.rows[0].panels[2].scopedVars.apps.value).to.be('se3');
  57. });
  58. describe('After a second iteration', function() {
  59. var repeatedPanelAfterIteration1;
  60. beforeEach(function() {
  61. repeatedPanelAfterIteration1 = ctx.rows[0].panels[1];
  62. ctx.rows[0].panels[0].fill = 10;
  63. ctx.dynamicDashboardSrv.update(ctx.dash);
  64. });
  65. it('should have reused same panel instances', function() {
  66. expect(ctx.rows[0].panels[1]).to.be(repeatedPanelAfterIteration1);
  67. });
  68. it('reused panel should copy properties from source', function() {
  69. expect(ctx.rows[0].panels[1].fill).to.be(10);
  70. });
  71. it('should have same panel count', function() {
  72. expect(ctx.rows[0].panels.length).to.be(3);
  73. });
  74. });
  75. describe('After a second iteration and selected values reduced', function() {
  76. beforeEach(function() {
  77. ctx.dash.templating.list[0].options[1].selected = false;
  78. ctx.dynamicDashboardSrv.update(ctx.dash);
  79. });
  80. it('should clean up repeated panel', function() {
  81. expect(ctx.rows[0].panels.length).to.be(2);
  82. });
  83. });
  84. describe('After a second iteration and panel repeat is turned off', function() {
  85. beforeEach(function() {
  86. ctx.rows[0].panels[0].repeat = null;
  87. ctx.dynamicDashboardSrv.update(ctx.dash);
  88. });
  89. it('should clean up repeated panel', function() {
  90. expect(ctx.rows[0].panels.length).to.be(1);
  91. });
  92. it('should remove scoped vars from reused panel', function() {
  93. expect(ctx.rows[0].panels[0].scopedVars).to.be.empty();
  94. });
  95. });
  96. });
  97. dynamicDashScenario('given dashboard with row repeat', function(ctx) {
  98. ctx.setup(function(dash) {
  99. dash.rows.push({
  100. repeat: 'servers',
  101. panels: [{id: 2}]
  102. });
  103. dash.rows.push({panels: []});
  104. dash.templating.list.push({
  105. name: 'servers',
  106. current: {
  107. text: 'se1, se2',
  108. value: ['se1', 'se2']
  109. },
  110. options: [
  111. {text: 'se1', value: 'se1', selected: true},
  112. {text: 'se2', value: 'se2', selected: true},
  113. ]
  114. });
  115. });
  116. it('should repeat row one time', function() {
  117. expect(ctx.rows.length).to.be(3);
  118. });
  119. it('should keep panel ids on first row', function() {
  120. expect(ctx.rows[0].panels[0].id).to.be(2);
  121. });
  122. it('should keep first row as repeat', function() {
  123. expect(ctx.rows[0].repeat).to.be('servers');
  124. });
  125. it('should clear repeat field on repeated row', function() {
  126. expect(ctx.rows[1].repeat).to.be(null);
  127. });
  128. it('should add scopedVars to rows', function() {
  129. expect(ctx.rows[0].scopedVars.servers.value).to.be('se1');
  130. expect(ctx.rows[1].scopedVars.servers.value).to.be('se2');
  131. });
  132. it('should generate a repeartRowId based on repeat row index', function() {
  133. expect(ctx.rows[1].repeatRowId).to.be(1);
  134. });
  135. it('should set scopedVars on row panels', function() {
  136. expect(ctx.rows[0].panels[0].scopedVars.servers.value).to.be('se1');
  137. expect(ctx.rows[1].panels[0].scopedVars.servers.value).to.be('se2');
  138. });
  139. describe('After a second iteration', function() {
  140. var repeatedRowAfterFirstIteration;
  141. beforeEach(function() {
  142. repeatedRowAfterFirstIteration = ctx.rows[1];
  143. ctx.rows[0].height = 500;
  144. ctx.dynamicDashboardSrv.update(ctx.dash);
  145. });
  146. it('should still only have 2 rows', function() {
  147. expect(ctx.rows.length).to.be(3);
  148. });
  149. it.skip('should have updated props from source', function() {
  150. expect(ctx.rows[1].height).to.be(500);
  151. });
  152. it('should reuse row instance', function() {
  153. expect(ctx.rows[1]).to.be(repeatedRowAfterFirstIteration);
  154. });
  155. });
  156. describe('After a second iteration and selected values reduced', function() {
  157. beforeEach(function() {
  158. ctx.dash.templating.list[0].options[1].selected = false;
  159. ctx.dynamicDashboardSrv.update(ctx.dash);
  160. });
  161. it('should remove repeated second row', function() {
  162. expect(ctx.rows.length).to.be(2);
  163. });
  164. });
  165. });
  166. dynamicDashScenario('given dashboard with row repeat and panel repeat', function(ctx) {
  167. ctx.setup(function(dash) {
  168. dash.rows.push({
  169. repeat: 'servers',
  170. panels: [{id: 2, repeat: 'metric'}]
  171. });
  172. dash.templating.list.push({
  173. name: 'servers',
  174. current: { text: 'se1, se2', value: ['se1', 'se2'] },
  175. options: [
  176. {text: 'se1', value: 'se1', selected: true},
  177. {text: 'se2', value: 'se2', selected: true},
  178. ]
  179. });
  180. dash.templating.list.push({
  181. name: 'metric',
  182. current: { text: 'm1, m2', value: ['m1', 'm2'] },
  183. options: [
  184. {text: 'm1', value: 'm1', selected: true},
  185. {text: 'm2', value: 'm2', selected: true},
  186. ]
  187. });
  188. });
  189. it('should repeat row one time', function() {
  190. expect(ctx.rows.length).to.be(2);
  191. });
  192. it('should repeat panel on both rows', function() {
  193. expect(ctx.rows[0].panels.length).to.be(2);
  194. expect(ctx.rows[1].panels.length).to.be(2);
  195. });
  196. it('should keep panel ids on first row', function() {
  197. expect(ctx.rows[0].panels[0].id).to.be(2);
  198. });
  199. it('should mark second row as repeated', function() {
  200. expect(ctx.rows[0].repeat).to.be('servers');
  201. });
  202. it('should clear repeat field on repeated row', function() {
  203. expect(ctx.rows[1].repeat).to.be(null);
  204. });
  205. it('should generate a repeartRowId based on repeat row index', function() {
  206. expect(ctx.rows[1].repeatRowId).to.be(1);
  207. });
  208. it('should set scopedVars on row panels', function() {
  209. expect(ctx.rows[0].panels[0].scopedVars.servers.value).to.be('se1');
  210. expect(ctx.rows[1].panels[0].scopedVars.servers.value).to.be('se2');
  211. });
  212. });
  213. });