ElementsSpec.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import {
  2. selfAndDirectChildren,
  3. selfAndAllChildren,
  4. getBBox,
  5. getClosure
  6. } from 'lib/util/Elements';
  7. describe('util/Elements', function() {
  8. var shapeA = {
  9. id: 'a',
  10. children: [
  11. { id: 'a.0', children: [] },
  12. { id: 'a.1', children: [
  13. { id: 'a.1.0' },
  14. { id: 'a.1.1' }
  15. ] },
  16. { id: 'a.2', children: [
  17. { id: 'a.2.0' },
  18. { id: 'a.2.1', children: [
  19. { id: 'a.2.1.0' }
  20. ] }
  21. ] }
  22. ]
  23. };
  24. var shapeB = {
  25. id: 'b'
  26. };
  27. var shapeC = {
  28. id: 'c',
  29. children: [
  30. { id: 'c.0' },
  31. { id: 'c.1' }
  32. ],
  33. attachers: []
  34. };
  35. var attacher = {
  36. id: 'attacher',
  37. host: shapeC
  38. };
  39. shapeC.attachers.push(attacher);
  40. var shapeD = {
  41. id: 'd',
  42. children: [
  43. shapeA,
  44. shapeB,
  45. shapeC
  46. ]
  47. };
  48. var shapeE = {
  49. id: 'e',
  50. children: [
  51. shapeC
  52. ]
  53. };
  54. var connection1 = {
  55. id: 'connection1',
  56. source: shapeE,
  57. target: shapeB
  58. };
  59. shapeE.outgoing = [ connection1 ];
  60. shapeB.incoming = [ connection1 ];
  61. function ids(array) {
  62. return array.map(function(e) {
  63. return e.id;
  64. });
  65. }
  66. describe('selfAndDirectChildren', function() {
  67. it('should return self + children', function() {
  68. // given
  69. var a = shapeA;
  70. // when
  71. var result = selfAndDirectChildren([shapeA]);
  72. expect(result.length).to.equal(4);
  73. expect(result).to.eql([ a ].concat(a.children));
  74. });
  75. it('should return self (no children)', function() {
  76. // when
  77. var result = selfAndDirectChildren([shapeB]);
  78. expect(result.length).to.equal(1);
  79. expect(result).to.eql([ shapeB ]);
  80. });
  81. it('should return unique elements', function() {
  82. // given
  83. var a = shapeA,
  84. child = shapeA.children[1];
  85. // when
  86. var result = selfAndDirectChildren([ a, child ]);
  87. expect(result.length).to.equal(6);
  88. expect(ids(result)).to.eql(ids([ a ].concat(a.children).concat(child.children)));
  89. });
  90. });
  91. describe('selfAndAllChildren', function() {
  92. it('should return self + ALL children', function() {
  93. // given
  94. var a = shapeA,
  95. d = shapeD; // parent of A
  96. // when
  97. var result = selfAndAllChildren([ a, d ]);
  98. expect(result.length).to.equal(14);
  99. });
  100. });
  101. describe('getClosure', function() {
  102. it('should work', function() {
  103. var closure = getClosure([ shapeB, shapeE ]);
  104. expect(closure.allShapes).to.have.keys('b', 'e', 'c', 'c.0', 'c.1');
  105. expect(closure.allConnections).to.have.keys('connection1');
  106. expect(closure.enclosedElements).to.have.keys('b', 'e', 'connection1', 'c', 'c.0', 'c.1');
  107. expect(closure.topLevel).to.have.keys('e', 'b', 'connection1');
  108. expect(closure.topLevel['connection1']).to.eql([ connection1 ]);
  109. });
  110. it('should be extensible', function() {
  111. var closure = getClosure([ shapeB, shapeE ], false, {
  112. topLevel: {
  113. 'foo': []
  114. }
  115. });
  116. expect(closure.allShapes).to.have.keys('b', 'e', 'c', 'c.0', 'c.1');
  117. expect(closure.allConnections).to.have.keys('connection1');
  118. expect(closure.enclosedElements).to.have.keys('b', 'e', 'connection1', 'c', 'c.0', 'c.1');
  119. expect(closure.topLevel).to.have.keys('foo');
  120. });
  121. });
  122. describe('#getBBox', function() {
  123. var shape1 = {
  124. id: 'shape1',
  125. x: 100,
  126. y: 100,
  127. height: 10,
  128. width: 10
  129. };
  130. var shape2 = {
  131. id: 'shape2',
  132. x: 120,
  133. y: 100,
  134. height: 30,
  135. width: 40
  136. };
  137. var shape3 = {
  138. id: 'shape3',
  139. x: -10,
  140. y: -10,
  141. height: 20,
  142. width: 20
  143. };
  144. var connection1 = {
  145. id: 'connection1',
  146. waypoints: [ { x: 110, y: 105 }, { x: 120, y: 115 } ]
  147. };
  148. it('should return bbox for element', function() {
  149. // given
  150. var elements = shape2;
  151. var bbox = getBBox(elements);
  152. expect(bbox).to.eql({
  153. x: 120,
  154. y: 100,
  155. height: 30,
  156. width: 40
  157. });
  158. });
  159. it('should return bbox for connection', function() {
  160. // given
  161. var elements = connection1;
  162. var bbox = getBBox(elements);
  163. expect(bbox).to.eql({
  164. x: 110,
  165. y: 105,
  166. height: 10,
  167. width: 10
  168. });
  169. });
  170. it('should return bbox for elements', function() {
  171. // given
  172. var elements = [shape1, shape2, connection1];
  173. var bbox = getBBox(elements);
  174. expect(bbox).to.eql({
  175. x: 100,
  176. y: 100,
  177. height: 30,
  178. width: 60
  179. });
  180. });
  181. it('should return bbox for elements at negative x,y', function() {
  182. // given
  183. var elements = [shape1, shape2, shape3, connection1];
  184. var bbox = getBBox(elements);
  185. expect(bbox).to.eql({
  186. x: -10,
  187. y: -10,
  188. height: 140,
  189. width: 170
  190. });
  191. });
  192. it('should return bbox for elements with a connection at the bbox border', function() {
  193. // given
  194. var elements = [shape1, connection1];
  195. var bbox = getBBox(elements);
  196. expect(bbox).to.eql({
  197. x: 100,
  198. y: 100,
  199. height: 15,
  200. width: 20
  201. });
  202. });
  203. });
  204. });