dynamic_dashboard_srv_specs.ts 8.3 KB

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