dynamicDashboardSrv-specs.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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',
  36. value: ['se1', 'se2']
  37. },
  38. options: [
  39. {text: 'se1', value: 'se1', selected: true},
  40. {text: 'se2', value: 'se2', selected: true},
  41. ]
  42. });
  43. });
  44. it('should repeat panel one time', function() {
  45. expect(ctx.rows[0].panels.length).to.be(2);
  46. });
  47. it('should mark panel repeated', function() {
  48. expect(ctx.rows[0].panels[0].repeat).to.be('apps');
  49. expect(ctx.rows[0].panels[1].repeatPanelId).to.be(2);
  50. });
  51. it('should set scopedVars on panels', function() {
  52. expect(ctx.rows[0].panels[0].scopedVars.apps.value).to.be('se1');
  53. expect(ctx.rows[0].panels[1].scopedVars.apps.value).to.be('se2');
  54. });
  55. describe('After a second iteration', function() {
  56. var repeatedPanelAfterIteration1;
  57. beforeEach(function() {
  58. repeatedPanelAfterIteration1 = ctx.rows[0].panels[1];
  59. ctx.rows[0].panels[0].fill = 10;
  60. ctx.dynamicDashboardSrv.update(ctx.dash);
  61. });
  62. it('should have reused same panel instances', function() {
  63. expect(ctx.rows[0].panels[1]).to.be(repeatedPanelAfterIteration1);
  64. });
  65. it('reused panel should copy properties from source', function() {
  66. expect(ctx.rows[0].panels[1].fill).to.be(10);
  67. });
  68. it('should have same panel count', function() {
  69. expect(ctx.rows[0].panels.length).to.be(2);
  70. });
  71. });
  72. describe('After a second iteration and selected values reduced', function() {
  73. beforeEach(function() {
  74. ctx.dash.templating.list[0].options[1].selected = false;
  75. ctx.dynamicDashboardSrv.update(ctx.dash);
  76. });
  77. it('should clean up repeated panel', function() {
  78. expect(ctx.rows[0].panels.length).to.be(1);
  79. });
  80. });
  81. });
  82. dynamicDashScenario('given dashboard with row repeat', function(ctx) {
  83. ctx.setup(function(dash) {
  84. dash.rows.push({
  85. repeat: 'servers',
  86. panels: [{id: 2}]
  87. });
  88. dash.rows.push({panels: []});
  89. dash.templating.list.push({
  90. name: 'servers',
  91. current: {
  92. text: 'se1, se2',
  93. value: ['se1', 'se2']
  94. },
  95. options: [
  96. {text: 'se1', value: 'se1', selected: true},
  97. {text: 'se2', value: 'se2', selected: true},
  98. ]
  99. });
  100. });
  101. it('should repeat row one time', function() {
  102. expect(ctx.rows.length).to.be(3);
  103. });
  104. it('should keep panel ids on first row', function() {
  105. expect(ctx.rows[0].panels[0].id).to.be(2);
  106. });
  107. it('should keep first row as repeat', function() {
  108. expect(ctx.rows[0].repeat).to.be('servers');
  109. });
  110. it('should clear repeat field on repeated row', function() {
  111. expect(ctx.rows[1].repeat).to.be(null);
  112. });
  113. it('should add scopedVars to rows', function() {
  114. expect(ctx.rows[0].scopedVars.servers.value).to.be('se1');
  115. expect(ctx.rows[1].scopedVars.servers.value).to.be('se2');
  116. });
  117. it('should generate a repeartRowId based on repeat row index', function() {
  118. expect(ctx.rows[1].repeatRowId).to.be(1);
  119. });
  120. it('should set scopedVars on row panels', function() {
  121. expect(ctx.rows[0].panels[0].scopedVars.servers.value).to.be('se1');
  122. expect(ctx.rows[1].panels[0].scopedVars.servers.value).to.be('se2');
  123. });
  124. describe('After a second iteration', function() {
  125. var repeatedRowAfterFirstIteration;
  126. beforeEach(function() {
  127. repeatedRowAfterFirstIteration = ctx.rows[1];
  128. ctx.rows[0].height = 500;
  129. ctx.dynamicDashboardSrv.update(ctx.dash);
  130. });
  131. it('should still only have 2 rows', function() {
  132. expect(ctx.rows.length).to.be(3);
  133. });
  134. it.skip('should have updated props from source', function() {
  135. expect(ctx.rows[1].height).to.be(500);
  136. });
  137. it('should reuse row instance', function() {
  138. expect(ctx.rows[1]).to.be(repeatedRowAfterFirstIteration);
  139. });
  140. });
  141. describe('After a second iteration and selected values reduced', function() {
  142. beforeEach(function() {
  143. ctx.dash.templating.list[0].options[1].selected = false;
  144. ctx.dynamicDashboardSrv.update(ctx.dash);
  145. });
  146. it('should remove repeated second row', function() {
  147. expect(ctx.rows.length).to.be(2);
  148. });
  149. });
  150. });
  151. dynamicDashScenario('given dashboard with row repeat and panel repeat', function(ctx) {
  152. ctx.setup(function(dash) {
  153. dash.rows.push({
  154. repeat: 'servers',
  155. panels: [{id: 2, repeat: 'metric'}]
  156. });
  157. dash.templating.list.push({
  158. name: 'servers',
  159. current: { text: 'se1, se2', value: ['se1', 'se2'] },
  160. options: [
  161. {text: 'se1', value: 'se1', selected: true},
  162. {text: 'se2', value: 'se2', selected: true},
  163. ]
  164. });
  165. dash.templating.list.push({
  166. name: 'metric',
  167. current: { text: 'm1, m2', value: ['m1', 'm2'] },
  168. options: [
  169. {text: 'm1', value: 'm1', selected: true},
  170. {text: 'm2', value: 'm2', selected: true},
  171. ]
  172. });
  173. });
  174. it('should repeat row one time', function() {
  175. expect(ctx.rows.length).to.be(2);
  176. });
  177. it('should repeat panel on both rows', function() {
  178. expect(ctx.rows[0].panels.length).to.be(2);
  179. expect(ctx.rows[1].panels.length).to.be(2);
  180. });
  181. it('should keep panel ids on first row', function() {
  182. expect(ctx.rows[0].panels[0].id).to.be(2);
  183. });
  184. it('should mark second row as repeated', function() {
  185. expect(ctx.rows[0].repeat).to.be('servers');
  186. });
  187. it('should clear repeat field on repeated row', function() {
  188. expect(ctx.rows[1].repeat).to.be(null);
  189. });
  190. it('should generate a repeartRowId based on repeat row index', function() {
  191. expect(ctx.rows[1].repeatRowId).to.be(1);
  192. });
  193. it('should set scopedVars on row panels', function() {
  194. expect(ctx.rows[0].panels[0].scopedVars.servers.value).to.be('se1');
  195. expect(ctx.rows[1].panels[0].scopedVars.servers.value).to.be('se2');
  196. });
  197. });
  198. });