repeat.jest.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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([
  212. 'row',
  213. 'graph',
  214. 'graph',
  215. 'row',
  216. 'graph',
  217. 'graph',
  218. 'row',
  219. 'graph',
  220. ]);
  221. });
  222. it('should set scopedVars for each panel', function() {
  223. dashboardJSON.templating.list[0].options[2].selected = true;
  224. dashboard = new DashboardModel(dashboardJSON);
  225. dashboard.processRepeats();
  226. expect(dashboard.panels[1].scopedVars).toMatchObject({
  227. apps: { text: 'se1', value: 'se1' },
  228. });
  229. expect(dashboard.panels[4].scopedVars).toMatchObject({
  230. apps: { text: 'se2', value: 'se2' },
  231. });
  232. const scopedVars = _.compact(
  233. _.map(dashboard.panels, panel => {
  234. return panel.scopedVars ? panel.scopedVars.apps.value : null;
  235. })
  236. );
  237. expect(scopedVars).toEqual([
  238. 'se1',
  239. 'se1',
  240. 'se1',
  241. 'se2',
  242. 'se2',
  243. 'se2',
  244. 'se3',
  245. 'se3',
  246. 'se3',
  247. ]);
  248. });
  249. it('should repeat only configured row', function() {
  250. expect(dashboard.panels[6].id).toBe(4);
  251. expect(dashboard.panels[7].id).toBe(5);
  252. });
  253. it('should repeat only row if it is collapsed', function() {
  254. dashboardJSON.panels = [
  255. {
  256. id: 1,
  257. type: 'row',
  258. collapsed: true,
  259. repeat: 'apps',
  260. gridPos: { x: 0, y: 0, h: 1, w: 24 },
  261. panels: [
  262. { id: 2, type: 'graph', gridPos: { x: 0, y: 1, h: 1, w: 6 } },
  263. { id: 3, type: 'graph', gridPos: { x: 6, y: 1, h: 1, w: 6 } },
  264. ],
  265. },
  266. { id: 4, type: 'row', gridPos: { x: 0, y: 1, h: 1, w: 24 } },
  267. { id: 5, type: 'graph', gridPos: { x: 0, y: 2, h: 1, w: 12 } },
  268. ];
  269. dashboard = new DashboardModel(dashboardJSON);
  270. dashboard.processRepeats();
  271. const panel_types = _.map(dashboard.panels, 'type');
  272. expect(panel_types).toEqual(['row', 'row', 'row', 'graph']);
  273. expect(dashboard.panels[0].panels).toHaveLength(2);
  274. expect(dashboard.panels[1].panels).toHaveLength(2);
  275. });
  276. it('should properly repeat multiple rows', function() {
  277. dashboardJSON.panels = [
  278. {
  279. id: 1,
  280. type: 'row',
  281. gridPos: { x: 0, y: 0, h: 1, w: 24 },
  282. repeat: 'apps',
  283. }, // repeat
  284. { id: 2, type: 'graph', gridPos: { x: 0, y: 1, h: 1, w: 6 } },
  285. { id: 3, type: 'graph', gridPos: { x: 6, y: 1, h: 1, w: 6 } },
  286. { id: 4, type: 'row', gridPos: { x: 0, y: 2, h: 1, w: 24 } }, // don't touch
  287. { id: 5, type: 'graph', gridPos: { x: 0, y: 3, h: 1, w: 12 } },
  288. {
  289. id: 6,
  290. type: 'row',
  291. gridPos: { x: 0, y: 4, h: 1, w: 24 },
  292. repeat: 'hosts',
  293. }, // repeat
  294. { id: 7, type: 'graph', gridPos: { x: 0, y: 5, h: 1, w: 6 } },
  295. { id: 8, type: 'graph', gridPos: { x: 6, y: 5, h: 1, w: 6 } },
  296. ];
  297. dashboardJSON.templating.list.push({
  298. name: 'hosts',
  299. current: {
  300. text: 'backend01, backend02',
  301. value: ['backend01', 'backend02'],
  302. },
  303. options: [
  304. { text: 'backend01', value: 'backend01', selected: true },
  305. { text: 'backend02', value: 'backend02', selected: true },
  306. { text: 'backend03', value: 'backend03', selected: false },
  307. ],
  308. });
  309. dashboard = new DashboardModel(dashboardJSON);
  310. dashboard.processRepeats();
  311. const panel_types = _.map(dashboard.panels, 'type');
  312. expect(panel_types).toEqual([
  313. 'row',
  314. 'graph',
  315. 'graph',
  316. 'row',
  317. 'graph',
  318. 'graph',
  319. 'row',
  320. 'graph',
  321. 'row',
  322. 'graph',
  323. 'graph',
  324. 'row',
  325. 'graph',
  326. 'graph',
  327. ]);
  328. expect(dashboard.panels[0].scopedVars['apps'].value).toBe('se1');
  329. expect(dashboard.panels[1].scopedVars['apps'].value).toBe('se1');
  330. expect(dashboard.panels[3].scopedVars['apps'].value).toBe('se2');
  331. expect(dashboard.panels[4].scopedVars['apps'].value).toBe('se2');
  332. expect(dashboard.panels[8].scopedVars['hosts'].value).toBe('backend01');
  333. expect(dashboard.panels[9].scopedVars['hosts'].value).toBe('backend01');
  334. expect(dashboard.panels[11].scopedVars['hosts'].value).toBe('backend02');
  335. expect(dashboard.panels[12].scopedVars['hosts'].value).toBe('backend02');
  336. });
  337. it('should assign unique ids for repeated panels', function() {
  338. dashboardJSON.panels = [
  339. {
  340. id: 1,
  341. type: 'row',
  342. collapsed: true,
  343. repeat: 'apps',
  344. gridPos: { x: 0, y: 0, h: 1, w: 24 },
  345. panels: [
  346. { id: 2, type: 'graph', gridPos: { x: 0, y: 1, h: 1, w: 6 } },
  347. { id: 3, type: 'graph', gridPos: { x: 6, y: 1, h: 1, w: 6 } },
  348. ],
  349. },
  350. { id: 4, type: 'row', gridPos: { x: 0, y: 1, h: 1, w: 24 } },
  351. { id: 5, type: 'graph', gridPos: { x: 0, y: 2, h: 1, w: 12 } },
  352. ];
  353. dashboard = new DashboardModel(dashboardJSON);
  354. dashboard.processRepeats();
  355. const panel_ids = _.flattenDeep(
  356. _.map(dashboard.panels, panel => {
  357. let ids = [];
  358. if (panel.panels && panel.panels.length) {
  359. ids = _.map(panel.panels, 'id');
  360. }
  361. ids.push(panel.id);
  362. return ids;
  363. })
  364. );
  365. expect(panel_ids.length).toEqual(_.uniq(panel_ids).length);
  366. });
  367. });