dynamic_dashboard_srv_specs.ts 8.3 KB

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