ElementIntegrationSpec.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import {
  2. bootstrapDiagram,
  3. inject
  4. } from 'test/TestHelper';
  5. import {
  6. keys
  7. } from 'min-dash';
  8. import {
  9. getEnclosedElements
  10. } from 'lib/util/Elements';
  11. import modelingModule from 'lib/features/modeling';
  12. describe('util/Elements', function() {
  13. describe('#getEnclosedElements', function() {
  14. beforeEach(bootstrapDiagram({ modules: [ modelingModule ] }));
  15. var shape1, shape2, shape3, shape3a, shape3b, shape4;
  16. var connection1, connection2, connection2a, connection2b, connection3;
  17. var elements;
  18. beforeEach(inject(function(elementFactory, canvas, elementRegistry) {
  19. shape1 = elementFactory.createRoot({
  20. id: 'shape1',
  21. x: 10,
  22. y: 10,
  23. height: 50,
  24. width: 50
  25. });
  26. canvas.addShape(shape1);
  27. shape2 = elementFactory.createRoot({
  28. id: 'shape2',
  29. x: 100,
  30. y: 10,
  31. height: 50,
  32. width: 50
  33. });
  34. canvas.addShape(shape2);
  35. shape3 = elementFactory.createRoot({
  36. id: 'shape3',
  37. x: 190,
  38. y: 10,
  39. height: 50,
  40. width: 50
  41. });
  42. canvas.addShape(shape3);
  43. shape4 = elementFactory.createRoot({
  44. id: 'shape4',
  45. x: 280,
  46. y: 10,
  47. height: 50,
  48. width: 150
  49. });
  50. canvas.addShape(shape4);
  51. shape3a = elementFactory.createRoot({
  52. id: 'shape3a',
  53. x: 190,
  54. y: 100,
  55. height: 50,
  56. width: 50
  57. });
  58. canvas.addShape(shape3a);
  59. shape3b = elementFactory.createRoot({
  60. id: 'shape3b',
  61. x: 190,
  62. y: 190,
  63. height: 100,
  64. width: 50
  65. });
  66. canvas.addShape(shape3b);
  67. connection1 = elementFactory.createRoot({
  68. id: 'connection1',
  69. waypoints: [ { x: 60, y: 35 }, { x: 100, y: 35 } ]
  70. });
  71. canvas.addConnection(connection1);
  72. connection2 = elementFactory.createRoot({
  73. id: 'connection2',
  74. waypoints: [ { x: 150, y: 35 }, { x: 190, y: 35 } ]
  75. });
  76. canvas.addConnection(connection2);
  77. connection3 = elementFactory.createRoot({
  78. id: 'connection3',
  79. waypoints: [ { x: 240, y: 35 }, { x: 280, y: 35 } ]
  80. });
  81. canvas.addConnection(connection3);
  82. connection2a = elementFactory.createRoot({
  83. id: 'connection2a',
  84. waypoints: [ { x: 215, y: 60 }, { x: 215, y: 100 } ]
  85. });
  86. canvas.addConnection(connection2a);
  87. connection2b = elementFactory.createRoot({
  88. id: 'connection2b',
  89. waypoints: [ { x: 215, y: 150 }, { x: 215, y: 190 } ]
  90. });
  91. canvas.addConnection(connection2b);
  92. elements = elementRegistry.filter(function(element) {
  93. return element;
  94. });
  95. }));
  96. it('should return elements east of x', inject(function() {
  97. // given
  98. var bbox = {
  99. x: 200
  100. };
  101. // when
  102. var filteredElements = getEnclosedElements(elements, bbox);
  103. // then
  104. var ids = keys(filteredElements);
  105. expect(ids).to.eql([ 'shape4', 'connection3', 'connection2a', 'connection2b' ]);
  106. }));
  107. it('should return elements south of y', inject(function() {
  108. // given
  109. var bbox = {
  110. y: 40
  111. };
  112. // when
  113. var filteredElements = getEnclosedElements(elements, bbox);
  114. // then
  115. var ids = keys(filteredElements);
  116. expect(ids).to.eql([ 'shape3a', 'shape3b', 'connection2a', 'connection2b' ]);
  117. }));
  118. it('should return elements east of x and south of y', inject(function() {
  119. // given
  120. var bbox = {
  121. x: 0,
  122. y: 100
  123. };
  124. // when
  125. var filteredElements = getEnclosedElements(elements, bbox);
  126. // then
  127. var ids = keys(filteredElements);
  128. expect(ids).to.eql([ 'shape3b', 'connection2b' ]);
  129. }));
  130. it('should return elements within the bbox', inject(function() {
  131. // given
  132. var bbox = {
  133. x: 0,
  134. y: 0,
  135. width: 250,
  136. height: 200
  137. };
  138. // when
  139. var filteredElements = getEnclosedElements(elements, bbox);
  140. // then
  141. var ids = keys(filteredElements);
  142. expect(ids).to.eql([
  143. 'shape1',
  144. 'shape2',
  145. 'shape3',
  146. 'shape3a',
  147. 'connection1',
  148. 'connection2',
  149. 'connection2a',
  150. 'connection2b'
  151. ]);
  152. }));
  153. describe('edge cases', function() {
  154. it('should return elements within the bbox with negative source', inject(function() {
  155. // given
  156. var bbox = {
  157. x: -300,
  158. y: -300,
  159. width: 550,
  160. height: 500
  161. };
  162. // when
  163. var filteredElements = getEnclosedElements(elements, bbox);
  164. // then
  165. var ids = keys(filteredElements);
  166. expect(ids).to.eql([
  167. 'shape1',
  168. 'shape2',
  169. 'shape3',
  170. 'shape3a',
  171. 'connection1',
  172. 'connection2',
  173. 'connection2a',
  174. 'connection2b'
  175. ]);
  176. }));
  177. it('should return elements at negative positions', inject(function(elementRegistry, elementFactory, canvas) {
  178. // given
  179. var shape0 = elementFactory.createRoot({
  180. id: 'shape0',
  181. x: -100,
  182. y: -10,
  183. height: 50,
  184. width: 50
  185. });
  186. canvas.addShape(shape0);
  187. var bbox = {
  188. x: -110,
  189. y: -20,
  190. width: 180,
  191. height: 90
  192. };
  193. elements = elementRegistry.filter(function(element) {
  194. return element;
  195. });
  196. // when
  197. var filteredElements = getEnclosedElements(elements, bbox);
  198. // then
  199. var ids = keys(filteredElements);
  200. expect(ids).to.eql([
  201. 'shape1',
  202. 'shape0'
  203. ]);
  204. }));
  205. it('should not return elements that cross bbox boundaries', inject(function() {
  206. // given
  207. var bbox = {
  208. x: 190,
  209. y: 0,
  210. width: 200,
  211. height: 100
  212. };
  213. // when
  214. var filteredElements = getEnclosedElements(elements, bbox);
  215. // then
  216. var ids = keys(filteredElements);
  217. expect(ids).to.eql([ 'connection3' ]);
  218. }));
  219. });
  220. });
  221. });