repeat.jest.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. beforeEach(function() {
  46. dashboard.panels[0].fill = 10;
  47. dashboard.processRepeats();
  48. });
  49. it('reused panel should copy properties from source', function() {
  50. expect(dashboard.panels[1].fill).toBe(10);
  51. });
  52. it('should have same panel count', function() {
  53. expect(dashboard.panels.length).toBe(3);
  54. });
  55. });
  56. describe('After a second iteration with different variable', function() {
  57. beforeEach(function() {
  58. dashboard.templating.list.push({
  59. name: 'server',
  60. current: { text: 'se1, se2, se3', value: ['se1']},
  61. options: [{text: 'se1', value: 'se1', selected: true}]
  62. });
  63. dashboard.panels[0].repeat = "server";
  64. dashboard.processRepeats();
  65. });
  66. it('should remove scopedVars value for last variable', function() {
  67. expect(dashboard.panels[0].scopedVars.apps).toBe(undefined);
  68. });
  69. it('should have new variable value in scopedVars', function() {
  70. expect(dashboard.panels[0].scopedVars.server.value).toBe("se1");
  71. });
  72. });
  73. describe('After a second iteration and selected values reduced', function() {
  74. beforeEach(function() {
  75. dashboard.templating.list[0].options[1].selected = false;
  76. dashboard.processRepeats();
  77. });
  78. it('should clean up repeated panel', function() {
  79. expect(dashboard.panels.length).toBe(2);
  80. });
  81. });
  82. describe('After a second iteration and panel repeat is turned off', function() {
  83. beforeEach(function() {
  84. dashboard.panels[0].repeat = null;
  85. dashboard.processRepeats();
  86. });
  87. it('should clean up repeated panel', function() {
  88. expect(dashboard.panels.length).toBe(1);
  89. });
  90. it('should remove scoped vars from reused panel', function() {
  91. expect(dashboard.panels[0].scopedVars).toBe(undefined);
  92. });
  93. });
  94. });
  95. describe('given dashboard with panel repeat in vertical direction', function() {
  96. var dashboard;
  97. beforeEach(function() {
  98. dashboard = new DashboardModel({
  99. panels: [{id: 2, repeat: 'apps', repeatDirection: 'v', gridPos: {x: 5, y: 0, h: 2, w: 8}}],
  100. templating: {
  101. list: [{
  102. name: 'apps',
  103. current: {
  104. text: 'se1, se2, se3',
  105. value: ['se1', 'se2', 'se3']
  106. },
  107. options: [
  108. {text: 'se1', value: 'se1', selected: true},
  109. {text: 'se2', value: 'se2', selected: true},
  110. {text: 'se3', value: 'se3', selected: true},
  111. {text: 'se4', value: 'se4', selected: false}
  112. ]
  113. }]
  114. }
  115. });
  116. dashboard.processRepeats();
  117. });
  118. it('should place on items on top of each other and keep witdh', function() {
  119. expect(dashboard.panels[0].gridPos).toMatchObject({x: 5, y: 0, h: 2, w: 8});
  120. expect(dashboard.panels[1].gridPos).toMatchObject({x: 5, y: 2, h: 2, w: 8});
  121. expect(dashboard.panels[2].gridPos).toMatchObject({x: 5, y: 4, h: 2, w: 8});
  122. });
  123. });
  124. describe.skip('given dashboard with row repeat', function() {
  125. var dashboard;
  126. beforeEach(function() {
  127. dashboard = new DashboardModel({
  128. panels: [
  129. {id: 1, type: 'row', repeat: 'apps', gridPos: {x: 0, y: 0, h: 1 , w: 24}},
  130. {id: 2, type: 'graph', gridPos: {x: 0, y: 1, h: 1 , w: 6}},
  131. {id: 3, type: 'graph', gridPos: {x: 6, y: 1, h: 1 , w: 6}},
  132. {id: 4, type: 'row', gridPos: {x: 0, y: 2, h: 1 , w: 24}},
  133. {id: 5, type: 'graph', gridPos: {x: 0, y: 3, h: 1 , w: 12}},
  134. ],
  135. templating: {
  136. list: [{
  137. name: 'apps',
  138. current: {
  139. text: 'se1, se2',
  140. value: ['se1', 'se2']
  141. },
  142. options: [
  143. {text: 'se1', value: 'se1', selected: true},
  144. {text: 'se2', value: 'se2', selected: true},
  145. {text: 'se3', value: 'se3', selected: false}
  146. ]
  147. }]
  148. }
  149. });
  150. dashboard.processRepeats();
  151. });
  152. it('should not repeat only row', function() {
  153. expect(dashboard.panels[1].type).toBe('graph');
  154. });
  155. //
  156. // it('should set scopedVars on panels', function() {
  157. // expect(dashboard.panels[1].scopedVars).toMatchObject({apps: {text: 'se1', value: 'se1'}})
  158. // });
  159. //
  160. // it.skip('should repeat row and panels below two times', function() {
  161. // expect(dashboard.panels).toMatchObject([
  162. // // first (original row)
  163. // {id: 1, type: 'row', repeat: 'apps', gridPos: {x: 0, y: 0, h: 1 , w: 24}},
  164. // {id: 2, type: 'graph', gridPos: {x: 0, y: 1, h: 1 , w: 6}},
  165. // {id: 3, type: 'graph', gridPos: {x: 6, y: 1, h: 1 , w: 6}},
  166. // // repeated row
  167. // {id: 1, type: 'row', repeatPanelId: 1, gridPos: {x: 0, y: 0, h: 1 , w: 24}},
  168. // {id: 2, type: 'graph', repeatPanelId: 1, gridPos: {x: 0, y: 1, h: 1 , w: 6}},
  169. // {id: 3, type: 'graph', repeatPanelId: 1, gridPos: {x: 6, y: 1, h: 1 , w: 6}},
  170. // // row below dont touch
  171. // {id: 4, type: 'row', gridPos: {x: 0, y: 2, h: 1 , w: 24}},
  172. // {id: 5, type: 'graph', gridPos: {x: 0, y: 3, h: 1 , w: 12}},
  173. // ]);
  174. // });
  175. });