dynamicDashboardSrv-specs.js 7.9 KB

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