repeat.jest.ts 11 KB

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