BendpointsMoveSpec.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /* global sinon */
  2. import {
  3. bootstrapDiagram,
  4. inject
  5. } from 'test/TestHelper';
  6. import {
  7. createCanvasEvent as canvasEvent
  8. } from '../../../util/MockEvents';
  9. import bendpointsModule from 'lib/features/bendpoints';
  10. import rulesModule from './rules';
  11. import modelingModule from 'lib/features/modeling';
  12. import selectModule from 'lib/features/selection';
  13. import {
  14. query as domQuery,
  15. queryAll as domQueryAll
  16. } from 'min-dom';
  17. describe('features/bendpoints - move', function() {
  18. beforeEach(bootstrapDiagram({
  19. modules: [
  20. bendpointsModule,
  21. rulesModule,
  22. modelingModule,
  23. selectModule
  24. ]
  25. }));
  26. beforeEach(inject(function(dragging) {
  27. dragging.setOptions({ manual: true });
  28. }));
  29. var rootShape, shape1, shape2, shape3, connection, connection2, connection3;
  30. beforeEach(inject(function(elementFactory, canvas) {
  31. rootShape = elementFactory.createRoot({
  32. id: 'root'
  33. });
  34. canvas.setRootElement(rootShape);
  35. shape1 = elementFactory.createShape({
  36. id: 'shape1',
  37. type: 'A',
  38. x: 100, y: 100, width: 300, height: 300
  39. });
  40. canvas.addShape(shape1, rootShape);
  41. shape2 = elementFactory.createShape({
  42. id: 'shape2',
  43. type: 'A',
  44. x: 500, y: 100, width: 100, height: 100
  45. });
  46. canvas.addShape(shape2, rootShape);
  47. shape3 = elementFactory.createShape({
  48. id: 'shape3',
  49. type: 'B',
  50. x: 500, y: 400, width: 100, height: 100
  51. });
  52. canvas.addShape(shape3, rootShape);
  53. connection = elementFactory.createConnection({
  54. id: 'connection',
  55. waypoints: [ { x: 250, y: 250 }, { x: 550, y: 250 }, { x: 550, y: 150 } ],
  56. source: shape1,
  57. target: shape2
  58. });
  59. canvas.addConnection(connection, rootShape);
  60. connection2 = elementFactory.createConnection({
  61. id: 'connection2',
  62. waypoints: [ { x: 250, y: 250 }, { x: 550, y: 450 } ],
  63. source: shape1,
  64. target: shape2
  65. });
  66. canvas.addConnection(connection2, rootShape);
  67. connection3 = elementFactory.createConnection({
  68. id: 'connection3',
  69. waypoints: [ { x: 575, y: 425 }, { x: 700, y: 350 }, { x: 650, y: 250 }, { x: 575, y: 175 } ],
  70. source: shape3,
  71. target: shape2
  72. });
  73. canvas.addConnection(connection3, rootShape);
  74. }));
  75. describe('dragger', function() {
  76. it('should show on activate', inject(function(canvas, bendpointMove, dragging) {
  77. // given
  78. var layer = canvas.getLayer('overlays');
  79. // when
  80. bendpointMove.start(canvasEvent({ x: 0, y: 0 }), connection, 2);
  81. dragging.move(canvasEvent({ x: 400, y: 200 }));
  82. // then
  83. var bendpoint = domQuery('.djs-bendpoint.djs-dragging', layer);
  84. expect(bendpoint).to.exist;
  85. }));
  86. it('should update during move', inject(function(canvas, bendpointMove, dragging) {
  87. // given
  88. var layer = canvas.getLayer('overlays');
  89. // when
  90. bendpointMove.start(canvasEvent({ x: 0, y: 0 }), connection, 1);
  91. dragging.move(canvasEvent({ x: 100, y: 100 }));
  92. // then
  93. var bendpoint = domQuery('.djs-bendpoint.djs-dragging', layer);
  94. expect(bendpoint).to.exist;
  95. }));
  96. it('should hide after resize', inject(function(canvas, bendpointMove, dragging) {
  97. // given
  98. var layer = canvas.getLayer('overlays');
  99. // when
  100. bendpointMove.start(canvasEvent({ x: 0, y: 0 }), connection, 2);
  101. dragging.move(canvasEvent({ x: 100, y: 100 }));
  102. dragging.hover({ element: rootShape, gfx: canvas.getGraphics(rootShape) });
  103. dragging.cancel();
  104. // then
  105. var bendpoint = domQuery('.djs-bendpoint.djs-dragging', layer);
  106. expect(bendpoint).to.be.null;
  107. }));
  108. it('should connect-hover and out', inject(function(canvas, bendpointMove, dragging) {
  109. // when
  110. bendpointMove.start(canvasEvent({ x: 500, y: 500 }), connection, 2);
  111. dragging.hover({ element: rootShape, gfx: canvas.getGraphics(rootShape) });
  112. dragging.out();
  113. dragging.hover({ element: shape2, gfx: canvas.getGraphics(shape2) });
  114. dragging.out();
  115. dragging.hover({ element: shape3, gfx: canvas.getGraphics(shape3) });
  116. dragging.out();
  117. dragging.hover({ element: rootShape, gfx: canvas.getGraphics(rootShape) });
  118. // then
  119. var hoverNodes = domQueryAll('.connect-hover, .connect-ok, .connect-not-ok', canvas._svg);
  120. // connect-hover indicator
  121. expect(hoverNodes.length).to.equal(1);
  122. }));
  123. });
  124. describe('rule integration', function() {
  125. it('should live-check hover (allowed)', inject(function(canvas, bendpointMove, dragging) {
  126. // when
  127. bendpointMove.start(canvasEvent({ x: 500, y: 500 }), connection, 2);
  128. dragging.move(canvasEvent({ x: 550, y: 150 }));
  129. dragging.hover({ element: shape2, gfx: canvas.getGraphics(shape2) });
  130. dragging.move(canvasEvent({ x: 530, y: 120 }));
  131. // then
  132. var hoverNode = domQuery('.connect-hover.connect-ok', canvas._svg);
  133. expect(hoverNode).to.exist;
  134. expect(hoverNode.getAttribute('data-element-id')).to.equal(shape2.id);
  135. }));
  136. it('should live-check hover (rejected)', inject(function(canvas, bendpointMove, dragging) {
  137. // when
  138. bendpointMove.start(canvasEvent({ x: 500, y: 500 }), connection, 2);
  139. dragging.move(canvasEvent({ x: 550, y: 450 }));
  140. dragging.hover({ element: shape3, gfx: canvas.getGraphics(shape3) });
  141. dragging.move(canvasEvent({ x: 530, y: 420 }));
  142. // then
  143. var hoverNode = domQuery('.connect-hover.connect-not-ok', canvas._svg);
  144. expect(hoverNode).to.exist;
  145. expect(hoverNode.getAttribute('data-element-id')).to.equal(shape3.id);
  146. }));
  147. });
  148. describe('hints', function() {
  149. it('should provide hints object', inject(function(canvas, bendpointMove, dragging, eventBus) {
  150. // given
  151. var spy = sinon.spy(function(e) {
  152. expect(e.context.hints).to.have.property('bendpointMove');
  153. expect(e.context.hints.bendpointMove).to.be.eql({
  154. insert: true,
  155. bendpointIndex: 2
  156. });
  157. });
  158. eventBus.once('commandStack.execute', spy);
  159. // when
  160. bendpointMove.start(canvasEvent({ x: 550, y: 200 }), connection, 2, true);
  161. dragging.hover({
  162. element: connection,
  163. gfx: canvas.getGraphics(connection)
  164. });
  165. dragging.move(canvasEvent({ x: 400, y: 100 }));
  166. dragging.end();
  167. // then
  168. expect(spy).to.have.been.called;
  169. }));
  170. });
  171. describe('modeling', function() {
  172. it('should add bendpoint', inject(function(canvas, bendpointMove, dragging) {
  173. // when
  174. bendpointMove.start(canvasEvent({ x: 550, y: 200 }), connection, 2, true);
  175. // need hover for creating new bendpoint
  176. dragging.hover({
  177. element: connection,
  178. gfx: canvas.getGraphics(connection)
  179. });
  180. dragging.move(canvasEvent({ x: 400, y: 100 }));
  181. dragging.end();
  182. // then
  183. expect(connection).to.have.waypoints([
  184. { x: 250, y: 250 },
  185. { x: 550, y: 250 },
  186. { x: 400, y: 100 },
  187. { x: 550, y: 150 }
  188. ]);
  189. }));
  190. it('should update bendpoint', inject(function(canvas, bendpointMove, dragging) {
  191. // when
  192. bendpointMove.start(canvasEvent({ x: 500, y: 500 }), connection, 1);
  193. dragging.move(canvasEvent({ x: 450, y: 430 }));
  194. dragging.hover({ element: rootShape, gfx: canvas.getGraphics(rootShape) });
  195. dragging.move(canvasEvent({ x: 530, y: 420 }));
  196. dragging.end();
  197. // then
  198. expect(connection.waypoints[1]).to.eql({ x: 530, y: 420 });
  199. }));
  200. it('should round to pixel values', inject(function(canvas, bendpointMove, dragging) {
  201. // when
  202. bendpointMove.start(canvasEvent({ x: 500, y: 500 }), connection, 1);
  203. dragging.move(canvasEvent({ x: 450, y: 430 }));
  204. dragging.hover({ element: rootShape, gfx: canvas.getGraphics(rootShape) });
  205. dragging.move(canvasEvent({ x: 530.3, y: 419.8 }));
  206. dragging.end();
  207. // then
  208. expect(connection.waypoints[1]).to.eql({ x: 530, y: 420 });
  209. }));
  210. it('should reattach target', inject(function(canvas, bendpointMove, dragging) {
  211. // given
  212. bendpointMove.start(canvasEvent({ x: 500, y: 500 }), connection, 2);
  213. dragging.hover({ element: shape2, gfx: canvas.getGraphics(shape2) });
  214. dragging.move(canvasEvent({ x: 530, y: 120 }));
  215. // when
  216. dragging.end();
  217. // then
  218. var waypoints = connection.waypoints;
  219. expect(waypoints[waypoints.length - 1]).to.eql({ x: 530, y: 120 });
  220. }));
  221. it('should reattach source', inject(function(canvas, bendpointMove, dragging) {
  222. // given
  223. bendpointMove.start(canvasEvent({ x: 500, y: 500 }), connection, 0);
  224. dragging.hover({ element: shape1, gfx: canvas.getGraphics(shape1) });
  225. dragging.move(canvasEvent({ x: 230, y: 120 }));
  226. // when
  227. dragging.end();
  228. // then
  229. expect(connection.waypoints[0]).to.eql({ x: 230, y: 120 });
  230. }));
  231. it('should keep one bendpoint, if two are overlapping', inject(function(canvas, bendpointMove, dragging) {
  232. // given
  233. bendpointMove.start(canvasEvent({ x: 650, y: 250 }), connection3, 2);
  234. // when
  235. dragging.move(canvasEvent({ x: 700, y: 350 }));
  236. dragging.end();
  237. // then
  238. expect(connection3.waypoints).to.eql([
  239. { x: 575, y: 425 },
  240. { x: 700, y: 350 },
  241. { x: 575, y: 175 }
  242. ]);
  243. }));
  244. });
  245. });