UpdateAttachmentSpec.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import {
  2. bootstrapDiagram,
  3. inject
  4. } from 'test/TestHelper';
  5. import modelingModule from 'lib/features/modeling';
  6. describe('features/modeling - attach shape', function() {
  7. beforeEach(bootstrapDiagram({ modules: [ modelingModule ] }));
  8. var rootShape, oldHostShape, hostShape, attachedShape, detachedShape;
  9. beforeEach(inject(function(elementFactory, canvas) {
  10. rootShape = elementFactory.createRoot({
  11. id: 'root'
  12. });
  13. canvas.setRootElement(rootShape);
  14. oldHostShape = elementFactory.createShape({
  15. id: 'parent',
  16. x: 100, y: 100,
  17. width: 300, height: 300
  18. });
  19. canvas.addShape(oldHostShape, rootShape);
  20. hostShape = elementFactory.createShape({
  21. id: 'hostShape',
  22. x: 700, y: 100,
  23. width: 100, height: 100
  24. });
  25. canvas.addShape(hostShape, rootShape);
  26. attachedShape = elementFactory.createShape({
  27. id: 'attachedShape',
  28. x: 400, y: 110,
  29. width: 50, height: 50,
  30. host: oldHostShape
  31. });
  32. canvas.addShape(attachedShape, rootShape);
  33. detachedShape = elementFactory.createShape({
  34. id: 'detachedShape',
  35. x: 400, y: 400,
  36. width: 50, height: 50
  37. });
  38. canvas.addShape(detachedShape, rootShape);
  39. }));
  40. describe('initial state', function() {
  41. it('should have wired host <-> attachers', inject(function() {
  42. // assume
  43. expect(oldHostShape.attachers).to.contain(attachedShape);
  44. expect(oldHostShape.attachers).not.to.contain(detachedShape);
  45. expect(attachedShape.host).to.eql(oldHostShape);
  46. expect(detachedShape.host).not.to.exist;
  47. }));
  48. });
  49. describe('detach attached shape', function() {
  50. it('should execute', inject(function(modeling) {
  51. // when
  52. modeling.updateAttachment(attachedShape, null);
  53. // then
  54. expect(attachedShape.host).not.to.exist;
  55. expect(oldHostShape.attachers).not.to.include(attachedShape);
  56. }));
  57. it('should undo', inject(function(modeling, commandStack) {
  58. // given
  59. modeling.updateAttachment(attachedShape, null);
  60. // when
  61. commandStack.undo();
  62. // then
  63. expect(attachedShape.host).to.equal(oldHostShape);
  64. expect(oldHostShape.attachers).to.include(attachedShape);
  65. }));
  66. it('should redo', inject(function(modeling, commandStack) {
  67. // given
  68. modeling.updateAttachment(attachedShape, null);
  69. commandStack.undo();
  70. // when
  71. commandStack.redo();
  72. // then
  73. expect(attachedShape.host).not.to.exist;
  74. expect(oldHostShape.attachers).not.to.include(attachedShape);
  75. }));
  76. });
  77. describe('attach detached shape', function() {
  78. it('should execute', inject(function(modeling) {
  79. // when
  80. modeling.updateAttachment(detachedShape, hostShape);
  81. // then
  82. expect(detachedShape.host).to.equal(hostShape);
  83. expect(hostShape.attachers).to.include(detachedShape);
  84. }));
  85. it('should undo', inject(function(modeling, commandStack) {
  86. // given
  87. modeling.updateAttachment(detachedShape, hostShape);
  88. // when
  89. commandStack.undo();
  90. // then
  91. expect(detachedShape.host).not.to.exist;
  92. expect(hostShape.attachers).not.to.include(detachedShape);
  93. }));
  94. it('should redo', inject(function(modeling, commandStack) {
  95. // given
  96. modeling.updateAttachment(detachedShape, hostShape);
  97. commandStack.undo();
  98. // when
  99. commandStack.redo();
  100. // then
  101. expect(detachedShape.host).to.equal(hostShape);
  102. expect(hostShape.attachers).to.include(detachedShape);
  103. }));
  104. });
  105. describe('reattach attached shape', function() {
  106. it('should execute', inject(function(modeling) {
  107. // when
  108. modeling.updateAttachment(attachedShape, hostShape);
  109. // then
  110. expect(attachedShape.host).to.equal(hostShape);
  111. expect(hostShape.attachers).to.include(attachedShape);
  112. expect(oldHostShape.attachers).not.to.include(attachedShape);
  113. }));
  114. it('should undo', inject(function(modeling, commandStack) {
  115. // given
  116. modeling.updateAttachment(attachedShape, hostShape);
  117. // when
  118. commandStack.undo();
  119. // then
  120. expect(attachedShape.host).to.equal(oldHostShape);
  121. expect(oldHostShape.attachers).to.include(attachedShape);
  122. expect(hostShape.attachers).not.to.include(attachedShape);
  123. }));
  124. it('should redo', inject(function(modeling, commandStack) {
  125. // given
  126. modeling.updateAttachment(attachedShape, hostShape);
  127. commandStack.undo();
  128. // when
  129. commandStack.redo();
  130. // then
  131. expect(attachedShape.host).to.equal(hostShape);
  132. expect(hostShape.attachers).to.include(attachedShape);
  133. expect(oldHostShape.attachers).not.to.include(attachedShape);
  134. }));
  135. });
  136. describe('connection handling', function() {
  137. var connectedShape, connectionToAttached, connectionToDetached;
  138. beforeEach(inject(function(canvas, elementFactory) {
  139. connectedShape = elementFactory.createShape({
  140. id: 'connectedShape',
  141. x: 700, y: 400,
  142. width: 100, height: 100
  143. });
  144. canvas.addShape(connectedShape, rootShape);
  145. connectionToAttached = elementFactory.createConnection({
  146. id: 'connectionToAttached',
  147. waypoints: [
  148. { x: 400, y: 110 },
  149. { x: 750, y: 450 }
  150. ],
  151. source: attachedShape,
  152. target: connectedShape
  153. });
  154. canvas.addConnection(connectionToAttached, rootShape);
  155. connectionToDetached = elementFactory.createConnection({
  156. id: 'connectionToDetached',
  157. waypoints: [
  158. { x: 425, y: 425 },
  159. { x: 750, y: 450 }
  160. ],
  161. source: detachedShape,
  162. target: connectedShape
  163. });
  164. canvas.addConnection(connectionToDetached, rootShape);
  165. }));
  166. it('should keep connection when reattaching', inject(function(modeling) {
  167. // when
  168. modeling.updateAttachment(attachedShape, hostShape);
  169. // then
  170. expect(attachedShape.outgoing).to.have.lengthOf(1);
  171. expect(connectedShape.incoming).to.have.lengthOf(2);
  172. }));
  173. it('should keep connection when detaching', inject(function(modeling) {
  174. // when
  175. modeling.updateAttachment(attachedShape, null);
  176. // then
  177. expect(attachedShape.outgoing).to.have.lengthOf(1);
  178. expect(connectedShape.incoming).to.have.lengthOf(2);
  179. }));
  180. it('should keep connection when attaching', inject(function(modeling) {
  181. // when
  182. modeling.updateAttachment(detachedShape, hostShape);
  183. // then
  184. expect(detachedShape.outgoing).to.have.lengthOf(1);
  185. expect(connectedShape.incoming).to.have.lengthOf(2);
  186. }));
  187. it('should keep connection when undoing detach', inject(function(modeling, commandStack) {
  188. // given
  189. modeling.updateAttachment(attachedShape, null);
  190. // when
  191. commandStack.undo();
  192. // then
  193. expect(attachedShape.outgoing).to.have.lengthOf(1);
  194. expect(connectedShape.incoming).to.have.lengthOf(2);
  195. }));
  196. });
  197. });