dynamic_dashboard_srv_specs.ts 8.4 KB

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