repeat.jest.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. import _ from 'lodash';
  2. import {DashboardModel} from '../dashboard_model';
  3. import { expect } from 'test/lib/common';
  4. jest.mock('app/core/services/context_srv', () => ({
  5. }));
  6. describe('given dashboard with panel repeat in horizontal direction', function() {
  7. var dashboard;
  8. beforeEach(function() {
  9. dashboard = new DashboardModel({
  10. panels: [{id: 2, repeat: 'apps', repeatDirection: 'h', gridPos: {x: 0, y: 0, h: 2, w: 24}}],
  11. templating: {
  12. list: [{
  13. name: 'apps',
  14. current: {
  15. text: 'se1, se2, se3',
  16. value: ['se1', 'se2', 'se3']
  17. },
  18. options: [
  19. {text: 'se1', value: 'se1', selected: true},
  20. {text: 'se2', value: 'se2', selected: true},
  21. {text: 'se3', value: 'se3', selected: true},
  22. {text: 'se4', value: 'se4', selected: false}
  23. ]
  24. }]
  25. }
  26. });
  27. dashboard.processRepeats();
  28. });
  29. it('should repeat panel 3 times', function() {
  30. expect(dashboard.panels.length).toBe(3);
  31. });
  32. it('should mark panel repeated', function() {
  33. expect(dashboard.panels[0].repeat).toBe('apps');
  34. expect(dashboard.panels[1].repeatPanelId).toBe(2);
  35. });
  36. it('should set scopedVars on panels', function() {
  37. expect(dashboard.panels[0].scopedVars.apps.value).toBe('se1');
  38. expect(dashboard.panels[1].scopedVars.apps.value).toBe('se2');
  39. expect(dashboard.panels[2].scopedVars.apps.value).toBe('se3');
  40. });
  41. it('should place on first row and adjust width so all fit', function() {
  42. expect(dashboard.panels[0].gridPos).toMatchObject({x: 0, y: 0, h: 2, w: 8});
  43. expect(dashboard.panels[1].gridPos).toMatchObject({x: 8, y: 0, h: 2, w: 8});
  44. expect(dashboard.panels[2].gridPos).toMatchObject({x: 16, y: 0, h: 2, w: 8});
  45. });
  46. describe('After a second iteration', function() {
  47. beforeEach(function() {
  48. dashboard.panels[0].fill = 10;
  49. dashboard.processRepeats();
  50. });
  51. it('reused panel should copy properties from source', function() {
  52. expect(dashboard.panels[1].fill).toBe(10);
  53. });
  54. it('should have same panel count', function() {
  55. expect(dashboard.panels.length).toBe(3);
  56. });
  57. });
  58. describe('After a second iteration with different variable', function() {
  59. beforeEach(function() {
  60. dashboard.templating.list.push({
  61. name: 'server',
  62. current: { text: 'se1, se2, se3', value: ['se1']},
  63. options: [{text: 'se1', value: 'se1', selected: true}]
  64. });
  65. dashboard.panels[0].repeat = "server";
  66. dashboard.processRepeats();
  67. });
  68. it('should remove scopedVars value for last variable', function() {
  69. expect(dashboard.panels[0].scopedVars.apps).toBe(undefined);
  70. });
  71. it('should have new variable value in scopedVars', function() {
  72. expect(dashboard.panels[0].scopedVars.server.value).toBe("se1");
  73. });
  74. });
  75. describe('After a second iteration and selected values reduced', function() {
  76. beforeEach(function() {
  77. dashboard.templating.list[0].options[1].selected = false;
  78. dashboard.processRepeats();
  79. });
  80. it('should clean up repeated panel', function() {
  81. expect(dashboard.panels.length).toBe(2);
  82. });
  83. });
  84. describe('After a second iteration and panel repeat is turned off', function() {
  85. beforeEach(function() {
  86. dashboard.panels[0].repeat = null;
  87. dashboard.processRepeats();
  88. });
  89. it('should clean up repeated panel', function() {
  90. expect(dashboard.panels.length).toBe(1);
  91. });
  92. it('should remove scoped vars from reused panel', function() {
  93. expect(dashboard.panels[0].scopedVars).toBe(undefined);
  94. });
  95. });
  96. });
  97. describe('given dashboard with panel repeat in vertical direction', function() {
  98. var dashboard;
  99. beforeEach(function() {
  100. dashboard = new DashboardModel({
  101. panels: [{id: 2, repeat: 'apps', repeatDirection: 'v', gridPos: {x: 5, y: 0, h: 2, w: 8}}],
  102. templating: {
  103. list: [{
  104. name: 'apps',
  105. current: {
  106. text: 'se1, se2, se3',
  107. value: ['se1', 'se2', 'se3']
  108. },
  109. options: [
  110. {text: 'se1', value: 'se1', selected: true},
  111. {text: 'se2', value: 'se2', selected: true},
  112. {text: 'se3', value: 'se3', selected: true},
  113. {text: 'se4', value: 'se4', selected: false}
  114. ]
  115. }]
  116. }
  117. });
  118. dashboard.processRepeats();
  119. });
  120. it('should place on items on top of each other and keep witdh', function() {
  121. expect(dashboard.panels[0].gridPos).toMatchObject({x: 5, y: 0, h: 2, w: 8});
  122. expect(dashboard.panels[1].gridPos).toMatchObject({x: 5, y: 2, h: 2, w: 8});
  123. expect(dashboard.panels[2].gridPos).toMatchObject({x: 5, y: 4, h: 2, w: 8});
  124. });
  125. });
  126. describe('given dashboard with row repeat', function() {
  127. let dashboard, dashboardJSON;
  128. beforeEach(function() {
  129. dashboardJSON = {
  130. panels: [
  131. {id: 1, type: 'row', gridPos: {x: 0, y: 0, h: 1 , w: 24}, repeat: 'apps'},
  132. {id: 2, type: 'graph', gridPos: {x: 0, y: 1, h: 1 , w: 6}},
  133. {id: 3, type: 'graph', gridPos: {x: 6, y: 1, h: 1 , w: 6}},
  134. {id: 4, type: 'row', gridPos: {x: 0, y: 2, h: 1 , w: 24}},
  135. {id: 5, type: 'graph', gridPos: {x: 0, y: 3, h: 1 , w: 12}},
  136. ],
  137. templating: {
  138. list: [{
  139. name: 'apps',
  140. current: {
  141. text: 'se1, se2',
  142. value: ['se1', 'se2']
  143. },
  144. options: [
  145. {text: 'se1', value: 'se1', selected: true},
  146. {text: 'se2', value: 'se2', selected: true},
  147. {text: 'se3', value: 'se3', selected: false}
  148. ]
  149. }]
  150. }
  151. };
  152. dashboard = new DashboardModel(dashboardJSON);
  153. dashboard.processRepeats();
  154. });
  155. it('should not repeat only row', function() {
  156. const panel_types = _.map(dashboard.panels, 'type');
  157. expect(panel_types).toEqual([
  158. 'row', 'graph', 'graph',
  159. 'row', 'graph', 'graph',
  160. 'row', 'graph'
  161. ]);
  162. });
  163. it('should set scopedVars for each panel', function() {
  164. dashboardJSON.templating.list[0].options[2].selected = true;
  165. dashboard = new DashboardModel(dashboardJSON);
  166. dashboard.processRepeats();
  167. expect(dashboard.panels[1].scopedVars).toMatchObject({apps: {text: 'se1', value: 'se1'}});
  168. expect(dashboard.panels[4].scopedVars).toMatchObject({apps: {text: 'se2', value: 'se2'}});
  169. const scopedVars = _.compact(_.map(dashboard.panels, (panel) => {
  170. return panel.scopedVars ? panel.scopedVars.apps.value : null;
  171. }));
  172. expect(scopedVars).toEqual([
  173. 'se1', 'se1', 'se1',
  174. 'se2', 'se2', 'se2',
  175. 'se3', 'se3', 'se3',
  176. ]);
  177. });
  178. it('should repeat only configured row', function() {
  179. expect(dashboard.panels[6].id).toBe(4);
  180. expect(dashboard.panels[7].id).toBe(5);
  181. });
  182. it('should repeat only row if it is collapsed', function() {
  183. dashboardJSON.panels = [
  184. {
  185. id: 1, type: 'row', collapsed: true, repeat: 'apps', gridPos: {x: 0, y: 0, h: 1 , w: 24},
  186. panels: [
  187. {id: 2, type: 'graph', gridPos: {x: 0, y: 1, h: 1 , w: 6}},
  188. {id: 3, type: 'graph', gridPos: {x: 6, y: 1, h: 1 , w: 6}},
  189. ]
  190. },
  191. {id: 4, type: 'row', gridPos: {x: 0, y: 1, h: 1 , w: 24}},
  192. {id: 5, type: 'graph', gridPos: {x: 0, y: 2, h: 1 , w: 12}},
  193. ];
  194. dashboard = new DashboardModel(dashboardJSON);
  195. dashboard.processRepeats();
  196. const panel_types = _.map(dashboard.panels, 'type');
  197. expect(panel_types).toEqual([
  198. 'row', 'row', 'row', 'graph'
  199. ]);
  200. expect(dashboard.panels[0].panels).toHaveLength(2);
  201. expect(dashboard.panels[1].panels).toHaveLength(2);
  202. });
  203. it('should properly repeat multiple rows', function() {
  204. dashboardJSON.panels = [
  205. {id: 1, type: 'row', gridPos: {x: 0, y: 0, h: 1 , w: 24}, repeat: 'apps'}, // repeat
  206. {id: 2, type: 'graph', gridPos: {x: 0, y: 1, h: 1 , w: 6}},
  207. {id: 3, type: 'graph', gridPos: {x: 6, y: 1, h: 1 , w: 6}},
  208. {id: 4, type: 'row', gridPos: {x: 0, y: 2, h: 1 , w: 24}}, // don't touch
  209. {id: 5, type: 'graph', gridPos: {x: 0, y: 3, h: 1 , w: 12}},
  210. {id: 6, type: 'row', gridPos: {x: 0, y: 4, h: 1 , w: 24}, repeat: 'hosts'}, // repeat
  211. {id: 7, type: 'graph', gridPos: {x: 0, y: 5, h: 1 , w: 6}},
  212. {id: 8, type: 'graph', gridPos: {x: 6, y: 5, h: 1 , w: 6}}
  213. ];
  214. dashboardJSON.templating.list.push({
  215. name: 'hosts',
  216. current: {
  217. text: 'backend01, backend02',
  218. value: ['backend01', 'backend02']
  219. },
  220. options: [
  221. {text: 'backend01', value: 'backend01', selected: true},
  222. {text: 'backend02', value: 'backend02', selected: true},
  223. {text: 'backend03', value: 'backend03', selected: false}
  224. ]
  225. });
  226. dashboard = new DashboardModel(dashboardJSON);
  227. dashboard.processRepeats();
  228. const panel_types = _.map(dashboard.panels, 'type');
  229. expect(panel_types).toEqual([
  230. 'row', 'graph', 'graph',
  231. 'row', 'graph', 'graph',
  232. 'row', 'graph',
  233. 'row', 'graph', 'graph',
  234. 'row', 'graph', 'graph',
  235. ]);
  236. expect(dashboard.panels[0].scopedVars['apps'].value).toBe('se1');
  237. expect(dashboard.panels[1].scopedVars['apps'].value).toBe('se1');
  238. expect(dashboard.panels[3].scopedVars['apps'].value).toBe('se2');
  239. expect(dashboard.panels[4].scopedVars['apps'].value).toBe('se2');
  240. expect(dashboard.panels[8].scopedVars['hosts'].value).toBe('backend01');
  241. expect(dashboard.panels[9].scopedVars['hosts'].value).toBe('backend01');
  242. expect(dashboard.panels[11].scopedVars['hosts'].value).toBe('backend02');
  243. expect(dashboard.panels[12].scopedVars['hosts'].value).toBe('backend02');
  244. });
  245. it('should assign unique ids for repeated panels', function() {
  246. dashboardJSON.panels = [
  247. {
  248. id: 1, type: 'row', collapsed: true, repeat: 'apps', gridPos: {x: 0, y: 0, h: 1 , w: 24},
  249. panels: [
  250. {id: 2, type: 'graph', gridPos: {x: 0, y: 1, h: 1 , w: 6}},
  251. {id: 3, type: 'graph', gridPos: {x: 6, y: 1, h: 1 , w: 6}},
  252. ]
  253. },
  254. {id: 4, type: 'row', gridPos: {x: 0, y: 1, h: 1 , w: 24}},
  255. {id: 5, type: 'graph', gridPos: {x: 0, y: 2, h: 1 , w: 12}},
  256. ];
  257. dashboard = new DashboardModel(dashboardJSON);
  258. dashboard.processRepeats();
  259. const panel_ids = _.flattenDeep(_.map(dashboard.panels, (panel) => {
  260. let ids = [];
  261. if (panel.panels && panel.panels.length) {
  262. ids = _.map(panel.panels, 'id');
  263. }
  264. ids.push(panel.id);
  265. return ids;
  266. }));
  267. expect(panel_ids.length).toEqual(_.uniq(panel_ids).length);
  268. });
  269. });