ReconnectConnectionSpec.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. import {
  2. bootstrapDiagram,
  3. inject
  4. } from 'test/TestHelper';
  5. import modelingModule from 'lib/features/modeling';
  6. /* global sinon */
  7. describe('features/modeling - reconnect connection', function() {
  8. beforeEach(bootstrapDiagram({
  9. modules: [
  10. modelingModule,
  11. {
  12. layouter: [ 'type', NoopLayouter ]
  13. }
  14. ]
  15. }));
  16. var parentShape, childShape, childShape2, connection;
  17. beforeEach(inject(function(elementFactory, canvas) {
  18. parentShape = elementFactory.createShape({
  19. id: 'parent',
  20. x: 100, y: 100, width: 300, height: 300
  21. });
  22. canvas.addShape(parentShape);
  23. childShape = elementFactory.createShape({
  24. id: 'child',
  25. x: 110, y: 110, width: 100, height: 100
  26. });
  27. canvas.addShape(childShape, parentShape);
  28. childShape2 = elementFactory.createShape({
  29. id: 'child2',
  30. x: 200, y: 110, width: 100, height: 100
  31. });
  32. canvas.addShape(childShape2, parentShape);
  33. connection = elementFactory.createConnection({
  34. id: 'connection',
  35. waypoints: [ { x: 150, y: 150 }, { x: 150, y: 200 }, { x: 350, y: 150 } ],
  36. source: childShape,
  37. target: childShape2
  38. });
  39. canvas.addConnection(connection, parentShape);
  40. }));
  41. describe('reconnectStart', function() {
  42. describe('passing position', function() {
  43. it('should execute', inject(function(modeling) {
  44. // given
  45. var newWaypoints = [ { x: 120, y: 120 }, { x: 350, y: 150 } ];
  46. // when
  47. modeling.reconnectStart(connection, childShape, { x: 120, y: 120 });
  48. // then
  49. expect(connection).to.have.waypoints(newWaypoints);
  50. }));
  51. it('should undo', inject(function(modeling, commandStack) {
  52. // given
  53. var oldWaypoints = connection.waypoints.slice();
  54. modeling.reconnectStart(connection, childShape, { x: 120, y: 120 });
  55. // when
  56. commandStack.undo();
  57. // then
  58. expect(connection).to.have.waypoints(oldWaypoints);
  59. }));
  60. it('should redo', inject(function(modeling, commandStack) {
  61. // given
  62. var newWaypoints = [ { x: 120, y: 120 }, { x: 350, y: 150 } ];
  63. modeling.reconnectStart(connection, childShape, { x: 120, y: 120 });
  64. // when
  65. commandStack.undo();
  66. commandStack.redo();
  67. // then
  68. expect(connection).to.have.waypoints(newWaypoints);
  69. }));
  70. it('should layout connection', inject(function(modeling) {
  71. // given
  72. var layoutSpy = sinon.spy(modeling, 'layoutConnection'),
  73. docking = { x: 120, y: 120 };
  74. // when
  75. modeling.reconnectStart(connection, childShape, docking);
  76. // then
  77. expect(layoutSpy).to.have.been.calledOnce;
  78. expect(layoutSpy.getCall(0).args).to.eql([ connection, { connectionStart: docking }]);
  79. }));
  80. });
  81. describe('passing waypoints', function() {
  82. it('should execute', inject(function(modeling) {
  83. // given
  84. var newWaypoints = [ { x: 110, y: 110 }, { x: 300, y: 300 } ];
  85. // when
  86. modeling.reconnectStart(connection, childShape, newWaypoints);
  87. // then
  88. expect(connection).to.have.waypoints(newWaypoints);
  89. }));
  90. it('should undo', inject(function(modeling, commandStack) {
  91. // given
  92. var oldWaypoints = connection.waypoints.slice();
  93. modeling.reconnectStart(connection, childShape, [ { x: 110, y: 110 }, { x: 300, y: 300 } ]);
  94. // when
  95. commandStack.undo();
  96. // then
  97. expect(connection).to.have.waypoints(oldWaypoints);
  98. }));
  99. it('should redo', inject(function(modeling, commandStack) {
  100. // given
  101. var newWaypoints = [ { x: 110, y: 110 }, { x: 300, y: 300 } ];
  102. modeling.reconnectStart(connection, childShape, newWaypoints);
  103. // when
  104. commandStack.undo();
  105. commandStack.redo();
  106. // then
  107. expect(connection).to.have.waypoints(newWaypoints);
  108. }));
  109. it('should layout connection', inject(function(modeling) {
  110. // given
  111. var newWaypoints = [ { x: 110, y: 110 }, { x: 300, y: 300 } ],
  112. docking = newWaypoints[0],
  113. layoutSpy = sinon.spy(modeling, 'layoutConnection');
  114. // when
  115. modeling.reconnectStart(connection, childShape, newWaypoints);
  116. // then
  117. expect(layoutSpy).to.have.been.calledOnce;
  118. expect(layoutSpy.getCall(0).args).to.eql([ connection, { connectionStart: docking }]);
  119. }));
  120. });
  121. });
  122. describe('reconnectEnd', function() {
  123. describe('passing position', function() {
  124. it('should execute', inject(function(modeling) {
  125. // given
  126. var newWaypoints = [ { x: 150, y: 150 }, { x: 300, y: 100 } ];
  127. // when
  128. modeling.reconnectEnd(connection, childShape2, { x: 300, y: 100 });
  129. // then
  130. expect(connection).to.have.waypoints(newWaypoints);
  131. }));
  132. it('should undo', inject(function(modeling, commandStack) {
  133. // given
  134. var oldWaypoints = connection.waypoints.slice();
  135. modeling.reconnectEnd(connection, childShape2, { x: 300, y: 100 });
  136. // when
  137. commandStack.undo();
  138. // then
  139. expect(connection).to.have.waypoints(oldWaypoints);
  140. }));
  141. it('should redo', inject(function(modeling, commandStack) {
  142. // given
  143. var newWaypoints = [ { x: 150, y: 150 }, { x: 300, y: 100 } ];
  144. // when
  145. modeling.reconnectEnd(connection, childShape2, { x: 300, y: 100 });
  146. // when
  147. commandStack.undo();
  148. commandStack.redo();
  149. // then
  150. expect(connection).to.have.waypoints(newWaypoints);
  151. }));
  152. it('should layout connection', inject(function(modeling) {
  153. // given
  154. var layoutSpy = sinon.spy(modeling, 'layoutConnection'),
  155. docking = { x: 120, y: 120 };
  156. // when
  157. modeling.reconnectEnd(connection, childShape, docking);
  158. // then
  159. expect(layoutSpy).to.have.been.calledOnce;
  160. expect(layoutSpy.getCall(0).args).to.eql([ connection, { connectionEnd: docking }]);
  161. }));
  162. });
  163. describe('passing waypoints', function() {
  164. it('should execute', inject(function(modeling) {
  165. // given
  166. var newWaypoints = [ { x: 110, y: 110 }, { x: 300, y: 300 } ];
  167. // when
  168. modeling.reconnectEnd(connection, childShape2, newWaypoints);
  169. // then
  170. expect(connection).to.have.waypoints(newWaypoints);
  171. }));
  172. it('should undo', inject(function(modeling, commandStack) {
  173. // given
  174. var oldWaypoints = connection.waypoints.slice();
  175. modeling.reconnectEnd(connection, childShape2, [ { x: 110, y: 110 }, { x: 300, y: 300 } ]);
  176. // when
  177. commandStack.undo();
  178. // then
  179. expect(connection).to.have.waypoints(oldWaypoints);
  180. }));
  181. it('should redo', inject(function(modeling, commandStack) {
  182. // given
  183. var newWaypoints = [ { x: 110, y: 110 }, { x: 300, y: 300 } ];
  184. modeling.reconnectEnd(connection, childShape2, newWaypoints);
  185. // when
  186. commandStack.undo();
  187. commandStack.redo();
  188. // then
  189. expect(connection).to.have.waypoints(newWaypoints);
  190. }));
  191. it('should layout connection', inject(function(modeling) {
  192. // given
  193. var newWaypoints = [ { x: 110, y: 110 }, { x: 300, y: 300 } ],
  194. docking = newWaypoints[1],
  195. layoutSpy = sinon.spy(modeling, 'layoutConnection');
  196. // when
  197. modeling.reconnectEnd(connection, childShape, newWaypoints);
  198. // then
  199. expect(layoutSpy).to.have.been.calledOnce;
  200. expect(layoutSpy.getCall(0).args).to.eql([ connection, { connectionEnd: docking }]);
  201. }));
  202. });
  203. });
  204. });
  205. // helpers /////////////////////
  206. /**
  207. * The most simple of all layouters
  208. */
  209. function NoopLayouter() {
  210. this.layoutConnection = function(connection, hints) {
  211. hints = hints || {};
  212. return [
  213. hints.connectionStart || connection.waypoints[0],
  214. hints.connectionEnd || connection.waypoints[connection.waypoints.length - 1]
  215. ];
  216. };
  217. }