dynamicDashboardSrv-specs.js 6.8 KB

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