DistributeElementsSpec.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import {
  2. bootstrapDiagram,
  3. inject
  4. } from 'test/TestHelper';
  5. import { forEach } from 'min-dash';
  6. import distributeElementsModule from 'lib/features/distribute-elements';
  7. import modelingModule from 'lib/features/modeling';
  8. function expectRanges(rangeGroups, expectedRanges) {
  9. expect(rangeGroups).to.have.length(expectedRanges.length);
  10. forEach(rangeGroups, function(group, idx) {
  11. expect(group.range).to.eql({
  12. min: expectedRanges[idx].min,
  13. max: expectedRanges[idx].max
  14. });
  15. expect(group.elements).to.eql(expectedRanges[idx].elements);
  16. });
  17. }
  18. describe('features/distribute-elements', function() {
  19. beforeEach(bootstrapDiagram({
  20. modules: [ distributeElementsModule, modelingModule ]
  21. }));
  22. describe('methods', function() {
  23. var s1 = {
  24. id: 's1',
  25. x: -200,
  26. y: -200,
  27. width: 100,
  28. height: 100
  29. },
  30. s2 = {
  31. id: 's2',
  32. x: -150,
  33. y: -100,
  34. width: 100,
  35. height: 100
  36. },
  37. s3 = {
  38. id: 's3',
  39. x: 325,
  40. y: 250,
  41. width: 100,
  42. height: 100
  43. },
  44. s4 = {
  45. id: 's4',
  46. x: 600,
  47. y: 350,
  48. width: 100,
  49. height: 100
  50. },
  51. s5 = {
  52. id: 's5',
  53. x: 650,
  54. y: 451,
  55. width: 100,
  56. height: 100
  57. };
  58. var elements = [ s1, s2, s3, s4, s5 ];
  59. describe('#createGroups', function() {
  60. it('should group elements horizontally', inject(function(distributeElements) {
  61. // given
  62. distributeElements._setOrientation('horizontal');
  63. // when
  64. var rangeGroups = distributeElements._createGroups(elements);
  65. // then
  66. expectRanges(rangeGroups, [
  67. { min: -195, max: -105, elements: [ s1, s2 ] },
  68. { min: 330, max: 420, elements: [ s3 ] },
  69. { min: 605, max: 695, elements: [ s4, s5 ] }
  70. ]);
  71. }));
  72. it('should group elements vertically', inject(function(distributeElements) {
  73. // given
  74. distributeElements._setOrientation('vertical');
  75. // when
  76. var rangeGroups = distributeElements._createGroups(elements);
  77. // then
  78. expectRanges(rangeGroups, [
  79. { min: -195, max: -105, elements: [ s1] },
  80. { min: -95, max: -5, elements: [ s2 ] },
  81. { min: 255, max: 345, elements: [ s3 ] },
  82. { min: 355, max: 445, elements: [ s4 ] },
  83. { min: 456, max: 546, elements: [ s5 ] }
  84. ]);
  85. }));
  86. });
  87. describe('hasIntersection', function() {
  88. it('should return true for intersecting ranges', inject(function(distributeElements) {
  89. // when
  90. var result = distributeElements._hasIntersection({ min: 100, max: 200 }, { min: 150, max: 200 });
  91. // then
  92. expect(result).to.be.true;
  93. }));
  94. it('should return false for NON intersecting ranges', inject(function(distributeElements) {
  95. // when
  96. var result = distributeElements._hasIntersection({ min: 100, max: 200 }, { min: 250, max: 300 });
  97. // then
  98. expect(result).to.be.false;
  99. }));
  100. it('should return true for negative intersecting ranges', inject(function(distributeElements) {
  101. // when
  102. var result = distributeElements._hasIntersection({ min: -200, max: -100 }, { min: -150, max: -50 });
  103. // then
  104. expect(result).to.be.true;
  105. }));
  106. });
  107. });
  108. describe('integration', function() {
  109. var rootShape, shape1, shape2, shape3, shape4, shape5, shapeBig;
  110. beforeEach(inject(function(elementFactory, canvas) {
  111. rootShape = elementFactory.createRoot({
  112. id: 'root'
  113. });
  114. canvas.setRootElement(rootShape);
  115. shape1 = elementFactory.createShape({
  116. id: 's1',
  117. x: 100, y: 100, width: 100, height: 100
  118. });
  119. canvas.addShape(shape1, rootShape);
  120. shape2 = elementFactory.createShape({
  121. id: 's2',
  122. x: 250, y: 100, width: 100, height: 100
  123. });
  124. canvas.addShape(shape2, rootShape);
  125. shape3 = elementFactory.createShape({
  126. id: 's3',
  127. x: 325, y: 250, width: 100, height: 100
  128. });
  129. canvas.addShape(shape3, rootShape);
  130. shape4 = elementFactory.createShape({
  131. id: 's4',
  132. x: 600, y: 350, width: 100, height: 100
  133. });
  134. canvas.addShape(shape4, rootShape);
  135. shape5 = elementFactory.createShape({
  136. id: 's5',
  137. x: 650, y: 451, width: 100, height: 100
  138. });
  139. canvas.addShape(shape5, rootShape);
  140. shapeBig = elementFactory.createShape({
  141. id: 'sBig',
  142. x: 150, y: 400, width: 200, height: 200
  143. });
  144. canvas.addShape(shapeBig, rootShape);
  145. }));
  146. it('should align shapes of same size horizontally', inject(function(distributeElements) {
  147. // given
  148. var elements = [ shape5, shape3, shape1, shape4, shape2, shapeBig ];
  149. // when
  150. distributeElements.trigger(elements, 'horizontal');
  151. expect(shape1.x).to.equal(100);
  152. expect(shape1.y).to.equal(100);
  153. expect(shape2.x).to.equal(373);
  154. expect(shape2.y).to.equal(100);
  155. expect(shape3.x).to.equal(373);
  156. expect(shape3.y).to.equal(250);
  157. expect(shape4.x).to.equal(650);
  158. expect(shape4.y).to.equal(350);
  159. expect(shape5.x).to.equal(650);
  160. expect(shape5.y).to.equal(451);
  161. expect(shapeBig.x).to.equal(150);
  162. expect(shapeBig.y).to.equal(400);
  163. }));
  164. it('should align shapes of same size vertically', inject(function(distributeElements) {
  165. // given
  166. var elements = [ shape5, shape3, shape1, shape4, shape2, shapeBig ];
  167. // when
  168. distributeElements.trigger(elements, 'vertical');
  169. expect(shape1.x).to.equal(100);
  170. expect(shape1.y).to.equal(100);
  171. expect(shape2.x).to.equal(250);
  172. expect(shape2.y).to.equal(100);
  173. expect(shape3.x).to.equal(325);
  174. expect(shape3.y).to.equal(219);
  175. expect(shape4.x).to.equal(600);
  176. expect(shape4.y).to.equal(338);
  177. expect(shape5.x).to.equal(650);
  178. expect(shape5.y).to.equal(451);
  179. expect(shapeBig.x).to.equal(150);
  180. expect(shapeBig.y).to.equal(400);
  181. }));
  182. });
  183. });