dynamicDashboardSrv-specs.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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.templating.list.push({
  89. name: 'servers',
  90. current: {
  91. text: 'se1, se2',
  92. value: ['se1', 'se2']
  93. },
  94. options: [
  95. {text: 'se1', value: 'se1', selected: true},
  96. {text: 'se2', value: 'se2', selected: true},
  97. ]
  98. });
  99. });
  100. it('should repeat row one time', function() {
  101. expect(ctx.rows.length).to.be(2);
  102. });
  103. it('should keep panel ids on first row', function() {
  104. expect(ctx.rows[0].panels[0].id).to.be(2);
  105. });
  106. it('should mark second row as repeated', function() {
  107. expect(ctx.rows[0].repeat).to.be('servers');
  108. });
  109. it('should clear repeat field on repeated row', function() {
  110. expect(ctx.rows[1].repeat).to.be(null);
  111. });
  112. it('should add scopedVars to rows', function() {
  113. expect(ctx.rows[0].scopedVars.servers.value).to.be('se1');
  114. expect(ctx.rows[1].scopedVars.servers.value).to.be('se2');
  115. });
  116. it('should generate a repeartRowId based on repeat row index', function() {
  117. expect(ctx.rows[1].repeatRowId).to.be(1);
  118. });
  119. it('should set scopedVars on row panels', function() {
  120. expect(ctx.rows[0].panels[0].scopedVars.servers.value).to.be('se1');
  121. expect(ctx.rows[1].panels[0].scopedVars.servers.value).to.be('se2');
  122. });
  123. describe('After a second iteration', function() {
  124. var repeatedRowAfterFirstIteration;
  125. beforeEach(function() {
  126. repeatedRowAfterFirstIteration = ctx.rows[1];
  127. ctx.rows[0].height = 500;
  128. ctx.dynamicDashboardSrv.update(ctx.dash);
  129. });
  130. it('should still only have 2 rows', function() {
  131. expect(ctx.rows.length).to.be(2);
  132. });
  133. it.skip('should have updated props from source', function() {
  134. expect(ctx.rows[1].height).to.be(500);
  135. });
  136. it('should reuse row instance', function() {
  137. expect(ctx.rows[1]).to.be(repeatedRowAfterFirstIteration);
  138. });
  139. });
  140. describe('After a second iteration and selected values reduced', function() {
  141. beforeEach(function() {
  142. ctx.dash.templating.list[0].options[1].selected = false;
  143. ctx.dynamicDashboardSrv.update(ctx.dash);
  144. });
  145. it('should remove repeated second row', function() {
  146. expect(ctx.rows.length).to.be(1);
  147. });
  148. });
  149. });
  150. dynamicDashScenario('given dashboard with row repeat and panel repeat', function(ctx) {
  151. ctx.setup(function(dash) {
  152. dash.rows.push({
  153. repeat: 'servers',
  154. panels: [{id: 2, repeat: 'metric'}]
  155. });
  156. dash.templating.list.push({
  157. name: 'servers',
  158. current: { text: 'se1, se2', value: ['se1', 'se2'] },
  159. options: [
  160. {text: 'se1', value: 'se1', selected: true},
  161. {text: 'se2', value: 'se2', selected: true},
  162. ]
  163. });
  164. dash.templating.list.push({
  165. name: 'metric',
  166. current: { text: 'm1, m2', value: ['m1', 'm2'] },
  167. options: [
  168. {text: 'm1', value: 'm1', selected: true},
  169. {text: 'm2', value: 'm2', selected: true},
  170. ]
  171. });
  172. });
  173. it('should repeat row one time', function() {
  174. expect(ctx.rows.length).to.be(2);
  175. });
  176. it('should repeat panel on both rows', function() {
  177. expect(ctx.rows[0].panels.length).to.be(2);
  178. expect(ctx.rows[1].panels.length).to.be(2);
  179. });
  180. it('should keep panel ids on first row', function() {
  181. expect(ctx.rows[0].panels[0].id).to.be(2);
  182. });
  183. it('should mark second row as repeated', function() {
  184. expect(ctx.rows[0].repeat).to.be('servers');
  185. });
  186. it('should clear repeat field on repeated row', function() {
  187. expect(ctx.rows[1].repeat).to.be(null);
  188. });
  189. it('should generate a repeartRowId based on repeat row index', function() {
  190. expect(ctx.rows[1].repeatRowId).to.be(1);
  191. });
  192. it('should set scopedVars on row panels', function() {
  193. expect(ctx.rows[0].panels[0].scopedVars.servers.value).to.be('se1');
  194. expect(ctx.rows[1].panels[0].scopedVars.servers.value).to.be('se2');
  195. });
  196. });
  197. });