MoveShapeSpec.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* global sinon */
  2. import {
  3. bootstrapDiagram,
  4. inject
  5. } from 'test/TestHelper';
  6. import modelingModule from 'lib/features/modeling';
  7. describe('features/modeling - move shape', function() {
  8. beforeEach(bootstrapDiagram({
  9. modules: [
  10. modelingModule
  11. ]
  12. }));
  13. var rootShape, parentShape, childShape, childShape2, connection;
  14. beforeEach(inject(function(elementFactory, canvas) {
  15. rootShape = elementFactory.createRoot({
  16. id: 'root'
  17. });
  18. canvas.setRootElement(rootShape);
  19. parentShape = elementFactory.createShape({
  20. id: 'parent',
  21. x: 100, y: 100,
  22. width: 300, height: 300
  23. });
  24. canvas.addShape(parentShape, rootShape);
  25. childShape = elementFactory.createShape({
  26. id: 'child',
  27. x: 110, y: 110,
  28. width: 100, height: 100
  29. });
  30. canvas.addShape(childShape, parentShape);
  31. childShape2 = elementFactory.createShape({
  32. id: 'child2',
  33. x: 200, y: 110,
  34. width: 100, height: 100
  35. });
  36. canvas.addShape(childShape2, parentShape);
  37. connection = elementFactory.createConnection({
  38. id: 'connection',
  39. waypoints: [
  40. { x: 150, y: 150, original: { x: 150, y: 150 } },
  41. { x: 150, y: 200 },
  42. { x: 350, y: 150 }
  43. ],
  44. source: childShape,
  45. target: childShape2
  46. });
  47. canvas.addConnection(connection, parentShape);
  48. }));
  49. describe('should move according to delta', function() {
  50. it('execute', inject(function(modeling) {
  51. // when
  52. modeling.moveShape(childShape, { x: -20, y: +20 });
  53. // then
  54. expect(childShape.x).to.equal(90);
  55. expect(childShape.y).to.equal(130);
  56. // keep old parent
  57. expect(childShape.parent).to.equal(parentShape);
  58. }));
  59. it('should update parent', inject(function(modeling) {
  60. // when
  61. modeling.moveShape(childShape, { x: -20, y: +20 }, rootShape);
  62. // then
  63. // update parent
  64. expect(childShape.parent).to.equal(rootShape);
  65. }));
  66. it('undo', inject(function(modeling, commandStack) {
  67. // given
  68. modeling.moveShape(childShape, { x: -20, y: +20 });
  69. // when
  70. commandStack.undo();
  71. // then
  72. expect(childShape.x).to.equal(110);
  73. expect(childShape.y).to.equal(110);
  74. // keep old parent
  75. expect(childShape.parent).to.equal(parentShape);
  76. }));
  77. it('redo', inject(function(modeling, commandStack) {
  78. // given
  79. modeling.moveShape(childShape, { x: -20, y: +20 });
  80. // when
  81. commandStack.undo();
  82. commandStack.redo();
  83. // then
  84. expect(childShape.x).to.equal(90);
  85. expect(childShape.y).to.equal(130);
  86. // keep old parent
  87. expect(childShape.parent).to.equal(parentShape);
  88. }));
  89. });
  90. describe('should update parent', function() {
  91. it('execute', inject(function(modeling) {
  92. // when
  93. modeling.moveShape(childShape, { x: 0, y: 0 }, rootShape);
  94. // then
  95. // update parent
  96. expect(childShape.parent).to.equal(rootShape);
  97. }));
  98. it('undo', inject(function(modeling, commandStack) {
  99. // given
  100. modeling.moveShape(childShape, { x: 0, y: 0 }, rootShape);
  101. // when
  102. commandStack.undo();
  103. // then
  104. // update parent
  105. expect(childShape.parent).to.equal(parentShape);
  106. }));
  107. it('redo', inject(function(modeling, commandStack) {
  108. // given
  109. modeling.moveShape(childShape, { x: 0, y: 0 }, rootShape);
  110. // when
  111. commandStack.undo();
  112. commandStack.redo();
  113. // then
  114. // update parent
  115. expect(childShape.parent).to.equal(rootShape);
  116. }));
  117. });
  118. describe('should update parent with parentIndex', function() {
  119. it('execute', inject(function(modeling) {
  120. // when
  121. modeling.moveShape(childShape, { x: -20, y: +20 }, rootShape, 0);
  122. // then
  123. expect(rootShape.children[0]).to.equal(childShape);
  124. }));
  125. it('undo', inject(function(modeling, commandStack) {
  126. // given
  127. modeling.moveShape(childShape, { x: -20, y: +20 }, rootShape, 0);
  128. // when
  129. commandStack.undo();
  130. // then
  131. expect(rootShape.children).not.to.contain(childShape);
  132. expect(parentShape.children[0]).to.equal(childShape);
  133. }));
  134. it('execute', inject(function(modeling, commandStack) {
  135. // given
  136. modeling.moveShape(childShape, { x: -20, y: +20 }, rootShape, 0);
  137. // when
  138. commandStack.undo();
  139. commandStack.redo();
  140. // then
  141. expect(rootShape.children[0]).to.equal(childShape);
  142. }));
  143. });
  144. describe('layout connections', function() {
  145. it('should layout after move', inject(function(modeling) {
  146. // when
  147. modeling.moveShape(childShape, { x: -20, y: +20 }, parentShape);
  148. // then
  149. // update parent
  150. expect(connection).to.have.waypoints([
  151. { x: 130, y: 170 },
  152. { x: 250, y: 160 }
  153. ]);
  154. }));
  155. it('should provide original waypoints to layout', inject(function(eventBus, modeling) {
  156. // given
  157. var originalWaypoints = connection.waypoints;
  158. var preLayoutSpy = sinon.spy(function(event) {
  159. var context = event.context,
  160. connection = context.connection;
  161. expect(connection).to.have.waypoints(originalWaypoints);
  162. expect(connection).to.have.startDocking({ x: 150, y: 150 });
  163. });
  164. eventBus.on('commandStack.connection.layout.preExecute', preLayoutSpy);
  165. // when
  166. modeling.moveShape(childShape, { x: -20, y: +20 }, parentShape);
  167. // then
  168. expect(preLayoutSpy).to.have.been.called;
  169. }));
  170. });
  171. });