dynamic_dashboard_srv_specs.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. var model = {
  18. rows: [],
  19. templating: { list: [] }
  20. };
  21. setupFunc(model);
  22. ctx.dash = ctx.dashboardSrv.create(model);
  23. ctx.dynamicDashboardSrv = new DynamicDashboardSrv();
  24. ctx.dynamicDashboardSrv.init(ctx.dash);
  25. ctx.dynamicDashboardSrv.process();
  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.process();
  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 with different variable', function() {
  81. beforeEach(function() {
  82. ctx.dash.templating.list.push({
  83. name: 'server',
  84. current: { text: 'se1, se2, se3', value: ['se1']},
  85. options: [{text: 'se1', value: 'se1', selected: true}]
  86. });
  87. ctx.rows[0].panels[0].repeat = "server";
  88. ctx.dynamicDashboardSrv.process();
  89. });
  90. it('should remove scopedVars value for last variable', function() {
  91. expect(ctx.rows[0].panels[0].scopedVars.apps).to.be(undefined);
  92. });
  93. it('should have new variable value in scopedVars', function() {
  94. expect(ctx.rows[0].panels[0].scopedVars.server.value).to.be("se1");
  95. });
  96. });
  97. describe('After a second iteration and selected values reduced', function() {
  98. beforeEach(function() {
  99. ctx.dash.templating.list[0].options[1].selected = false;
  100. ctx.dynamicDashboardSrv.process();
  101. });
  102. it('should clean up repeated panel', function() {
  103. expect(ctx.rows[0].panels.length).to.be(2);
  104. });
  105. });
  106. describe('After a second iteration and panel repeat is turned off', function() {
  107. beforeEach(function() {
  108. ctx.rows[0].panels[0].repeat = null;
  109. ctx.dynamicDashboardSrv.process();
  110. });
  111. it('should clean up repeated panel', function() {
  112. expect(ctx.rows[0].panels.length).to.be(1);
  113. });
  114. it('should remove scoped vars from reused panel', function() {
  115. expect(ctx.rows[0].panels[0].scopedVars).to.be(undefined);
  116. });
  117. });
  118. });
  119. dynamicDashScenario('given dashboard with row repeat', function(ctx) {
  120. ctx.setup(function(dash) {
  121. dash.rows.push({
  122. repeat: 'servers',
  123. panels: [{id: 2}]
  124. });
  125. dash.rows.push({panels: []});
  126. dash.templating.list.push({
  127. name: 'servers',
  128. current: {
  129. text: 'se1, se2',
  130. value: ['se1', 'se2']
  131. },
  132. options: [
  133. {text: 'se1', value: 'se1', selected: true},
  134. {text: 'se2', value: 'se2', selected: true},
  135. ]
  136. });
  137. });
  138. it('should repeat row one time', function() {
  139. expect(ctx.rows.length).to.be(3);
  140. });
  141. it('should keep panel ids on first row', function() {
  142. expect(ctx.rows[0].panels[0].id).to.be(2);
  143. });
  144. it('should keep first row as repeat', function() {
  145. expect(ctx.rows[0].repeat).to.be('servers');
  146. });
  147. it('should clear repeat field on repeated row', function() {
  148. expect(ctx.rows[1].repeat).to.be(null);
  149. });
  150. it('should add scopedVars to rows', function() {
  151. expect(ctx.rows[0].scopedVars.servers.value).to.be('se1');
  152. expect(ctx.rows[1].scopedVars.servers.value).to.be('se2');
  153. });
  154. it('should generate a repeartRowId based on repeat row index', function() {
  155. expect(ctx.rows[1].repeatRowId).to.be(1);
  156. expect(ctx.rows[1].repeatIteration).to.be(ctx.dynamicDashboardSrv.iteration);
  157. });
  158. it('should set scopedVars on row panels', function() {
  159. expect(ctx.rows[0].panels[0].scopedVars.servers.value).to.be('se1');
  160. expect(ctx.rows[1].panels[0].scopedVars.servers.value).to.be('se2');
  161. });
  162. describe('After a second iteration', function() {
  163. var repeatedRowAfterFirstIteration;
  164. beforeEach(function() {
  165. repeatedRowAfterFirstIteration = ctx.rows[1];
  166. ctx.rows[0].height = 500;
  167. ctx.dynamicDashboardSrv.process();
  168. });
  169. it('should still only have 2 rows', function() {
  170. expect(ctx.rows.length).to.be(3);
  171. });
  172. it.skip('should have updated props from source', function() {
  173. expect(ctx.rows[1].height).to.be(500);
  174. });
  175. it('should reuse row instance', function() {
  176. expect(ctx.rows[1]).to.be(repeatedRowAfterFirstIteration);
  177. });
  178. });
  179. describe('After a second iteration and selected values reduced', function() {
  180. beforeEach(function() {
  181. ctx.dash.templating.list[0].options[1].selected = false;
  182. ctx.dynamicDashboardSrv.process();
  183. });
  184. it('should remove repeated second row', function() {
  185. expect(ctx.rows.length).to.be(2);
  186. });
  187. });
  188. });
  189. dynamicDashScenario('given dashboard with row repeat and panel repeat', function(ctx) {
  190. ctx.setup(function(dash) {
  191. dash.rows.push({
  192. repeat: 'servers',
  193. panels: [{id: 2, repeat: 'metric'}]
  194. });
  195. dash.templating.list.push({
  196. name: 'servers',
  197. current: { text: 'se1, se2', value: ['se1', 'se2'] },
  198. options: [
  199. {text: 'se1', value: 'se1', selected: true},
  200. {text: 'se2', value: 'se2', selected: true},
  201. ]
  202. });
  203. dash.templating.list.push({
  204. name: 'metric',
  205. current: { text: 'm1, m2', value: ['m1', 'm2'] },
  206. options: [
  207. {text: 'm1', value: 'm1', selected: true},
  208. {text: 'm2', value: 'm2', selected: true},
  209. ]
  210. });
  211. });
  212. it('should repeat row one time', function() {
  213. expect(ctx.rows.length).to.be(2);
  214. });
  215. it('should repeat panel on both rows', function() {
  216. expect(ctx.rows[0].panels.length).to.be(2);
  217. expect(ctx.rows[1].panels.length).to.be(2);
  218. });
  219. it('should keep panel ids on first row', function() {
  220. expect(ctx.rows[0].panels[0].id).to.be(2);
  221. });
  222. it('should mark second row as repeated', function() {
  223. expect(ctx.rows[0].repeat).to.be('servers');
  224. });
  225. it('should clear repeat field on repeated row', function() {
  226. expect(ctx.rows[1].repeat).to.be(null);
  227. });
  228. it('should generate a repeartRowId based on repeat row index', function() {
  229. expect(ctx.rows[1].repeatRowId).to.be(1);
  230. });
  231. it('should set scopedVars on row panels', function() {
  232. expect(ctx.rows[0].panels[0].scopedVars.servers.value).to.be('se1');
  233. expect(ctx.rows[1].panels[0].scopedVars.servers.value).to.be('se2');
  234. });
  235. });