dynamic_dashboard_srv_specs.ts 7.5 KB

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