repeat.jest.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import {DashboardModel} from '../dashboard_model';
  2. jest.mock('app/core/services/context_srv', () => ({
  3. }));
  4. describe('given dashboard with panel repeat in horizontal direction', function() {
  5. var dashboard;
  6. beforeEach(function() {
  7. dashboard = new DashboardModel({
  8. panels: [{id: 2, repeat: 'apps', repeatDirection: 'h', gridPos: {x: 0, y: 0, h: 2, w: 24}}],
  9. templating: {
  10. list: [{
  11. name: 'apps',
  12. current: {
  13. text: 'se1, se2, se3',
  14. value: ['se1', 'se2', 'se3']
  15. },
  16. options: [
  17. {text: 'se1', value: 'se1', selected: true},
  18. {text: 'se2', value: 'se2', selected: true},
  19. {text: 'se3', value: 'se3', selected: true},
  20. {text: 'se4', value: 'se4', selected: false}
  21. ]
  22. }]
  23. }
  24. });
  25. dashboard.processRepeats();
  26. });
  27. it('should repeat panel 3 times', function() {
  28. expect(dashboard.panels.length).toBe(3);
  29. });
  30. it('should mark panel repeated', function() {
  31. expect(dashboard.panels[0].repeat).toBe('apps');
  32. expect(dashboard.panels[1].repeatPanelId).toBe(2);
  33. });
  34. it('should set scopedVars on panels', function() {
  35. expect(dashboard.panels[0].scopedVars.apps.value).toBe('se1');
  36. expect(dashboard.panels[1].scopedVars.apps.value).toBe('se2');
  37. expect(dashboard.panels[2].scopedVars.apps.value).toBe('se3');
  38. });
  39. it('should place on first row and adjust width so all fit', function() {
  40. expect(dashboard.panels[0].gridPos).toMatchObject({x: 0, y: 0, h: 2, w: 8});
  41. expect(dashboard.panels[1].gridPos).toMatchObject({x: 8, y: 0, h: 2, w: 8});
  42. expect(dashboard.panels[2].gridPos).toMatchObject({x: 16, y: 0, h: 2, w: 8});
  43. });
  44. describe('After a second iteration', function() {
  45. var repeatedPanelAfterIteration1;
  46. beforeEach(function() {
  47. repeatedPanelAfterIteration1 = dashboard.panels[1];
  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.skip('given dashboard with row repeat', function() {
  127. var dashboard;
  128. beforeEach(function() {
  129. dashboard = new DashboardModel({
  130. panels: [
  131. {id: 1, type: 'row', repeat: 'apps', gridPos: {x: 0, y: 0, h: 1 , w: 24}},
  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.processRepeats();
  153. });
  154. it('should not repeat only row', function() {
  155. expect(dashboard.panels[1].type).toBe('graph');
  156. });
  157. //
  158. // it('should set scopedVars on panels', function() {
  159. // expect(dashboard.panels[1].scopedVars).toMatchObject({apps: {text: 'se1', value: 'se1'}})
  160. // });
  161. //
  162. // it.skip('should repeat row and panels below two times', function() {
  163. // expect(dashboard.panels).toMatchObject([
  164. // // first (original row)
  165. // {id: 1, type: 'row', repeat: 'apps', gridPos: {x: 0, y: 0, h: 1 , w: 24}},
  166. // {id: 2, type: 'graph', gridPos: {x: 0, y: 1, h: 1 , w: 6}},
  167. // {id: 3, type: 'graph', gridPos: {x: 6, y: 1, h: 1 , w: 6}},
  168. // // repeated row
  169. // {id: 1, type: 'row', repeatPanelId: 1, gridPos: {x: 0, y: 0, h: 1 , w: 24}},
  170. // {id: 2, type: 'graph', repeatPanelId: 1, gridPos: {x: 0, y: 1, h: 1 , w: 6}},
  171. // {id: 3, type: 'graph', repeatPanelId: 1, gridPos: {x: 6, y: 1, h: 1 , w: 6}},
  172. // // row below dont touch
  173. // {id: 4, type: 'row', gridPos: {x: 0, y: 2, h: 1 , w: 24}},
  174. // {id: 5, type: 'graph', gridPos: {x: 0, y: 3, h: 1 , w: 12}},
  175. // ]);
  176. // });
  177. });