CreateSpec.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import {
  2. bootstrapDiagram,
  3. inject
  4. } from 'test/TestHelper';
  5. import {
  6. createCanvasEvent as canvasEvent
  7. } from '../../../util/MockEvents';
  8. import modelingModule from 'lib/features/modeling';
  9. import moveModule from 'lib/features/move';
  10. import dragModule from 'lib/features/dragging';
  11. import createModule from 'lib/features/create';
  12. import attachSupportModule from 'lib/features/attach-support';
  13. import rulesModule from './rules';
  14. import {
  15. classes as svgClasses
  16. } from 'tiny-svg';
  17. describe('features/create - Create', function() {
  18. beforeEach(bootstrapDiagram({
  19. modules: [
  20. createModule,
  21. rulesModule,
  22. attachSupportModule,
  23. modelingModule,
  24. moveModule,
  25. dragModule
  26. ]
  27. }));
  28. beforeEach(inject(function(dragging, elementRegistry) {
  29. dragging.setOptions({ manual: true });
  30. }));
  31. var rootShape,
  32. parentShape,
  33. hostShape,
  34. childShape,
  35. newShape;
  36. beforeEach(inject(function(elementFactory, canvas) {
  37. rootShape = elementFactory.createRoot({
  38. id: 'root'
  39. });
  40. canvas.setRootElement(rootShape);
  41. parentShape = elementFactory.createShape({
  42. id: 'parent',
  43. x: 100, y: 100, width: 200, height: 200
  44. });
  45. canvas.addShape(parentShape, rootShape);
  46. hostShape = elementFactory.createShape({
  47. id: 'hostShape',
  48. x: 400, y: 200,
  49. width: 100, height: 100
  50. });
  51. canvas.addShape(hostShape, rootShape);
  52. childShape = elementFactory.createShape({
  53. id: 'childShape',
  54. x: 150, y: 350, width: 100, height: 100
  55. });
  56. canvas.addShape(childShape, rootShape);
  57. newShape = elementFactory.createShape({
  58. id: 'newShape',
  59. x: 0, y: 0, width: 50, height: 50
  60. });
  61. }));
  62. describe('basics', function() {
  63. it('should create', inject(function(create, elementRegistry, elementFactory, dragging) {
  64. // given
  65. var parentGfx = elementRegistry.getGraphics('parentShape');
  66. // when
  67. create.start(canvasEvent({ x: 0, y: 0 }), newShape);
  68. dragging.move(canvasEvent({ x: 125, y: 125 }));
  69. dragging.hover({ element: parentShape, gfx: parentGfx });
  70. dragging.move(canvasEvent({ x: 175, y: 175 }));
  71. dragging.end();
  72. var createdShape = elementRegistry.get('newShape');
  73. // then
  74. expect(createdShape).to.exist;
  75. expect(createdShape).to.eql(newShape);
  76. expect(createdShape.parent).to.equal(parentShape);
  77. }));
  78. it('should append', inject(function(create, elementRegistry, elementFactory, dragging) {
  79. // given
  80. var parentGfx = elementRegistry.getGraphics('parentShape');
  81. // when
  82. create.start(canvasEvent({ x: 0, y: 0 }), newShape, childShape);
  83. dragging.move(canvasEvent({ x: 175, y: 175 }));
  84. dragging.hover({ element: parentShape, gfx: parentGfx });
  85. dragging.move(canvasEvent({ x: 400, y: 200 }));
  86. dragging.end();
  87. var createdShape = elementRegistry.get('newShape');
  88. // then
  89. expect(createdShape).to.exist;
  90. expect(createdShape).to.eql(newShape);
  91. expect(createdShape.incoming).to.have.length(1);
  92. expect(createdShape.incoming).to.eql(childShape.outgoing);
  93. }));
  94. it('should attach', inject(function(create, elementRegistry, elementFactory, dragging) {
  95. // given
  96. var hostShapeGfx = elementRegistry.getGraphics('hostShape');
  97. // when
  98. create.start(canvasEvent({ x: 0, y: 0 }), newShape);
  99. dragging.move(canvasEvent({ x: 150, y: 350 }));
  100. dragging.hover({ element: hostShape, gfx: hostShapeGfx });
  101. dragging.move(canvasEvent({ x: 200, y: 350 }));
  102. dragging.end();
  103. // then
  104. expect(newShape.host).to.equal(hostShape);
  105. expect(hostShape.attachers).to.include(newShape);
  106. }));
  107. it('should append + attach', inject(function(create, elementRegistry, elementFactory, dragging) {
  108. // given
  109. var hostShapeGfx = elementRegistry.getGraphics('hostShape');
  110. // when
  111. create.start(canvasEvent({ x: 0, y: 0 }), newShape, parentShape);
  112. dragging.move(canvasEvent({ x: 150, y: 350 }));
  113. dragging.hover({ element: hostShape, gfx: hostShapeGfx });
  114. dragging.move(canvasEvent({ x: 200, y: 350 }));
  115. dragging.end();
  116. // then
  117. expect(newShape.host).to.equal(hostShape);
  118. expect(hostShape.attachers).to.include(newShape);
  119. // both source and new target are connected
  120. expect(newShape.incoming).to.eql(parentShape.outgoing);
  121. expect(newShape.incoming).to.have.length(1);
  122. }));
  123. });
  124. describe('visuals', function() {
  125. it('should add visuals', inject(function(create, elementRegistry, dragging) {
  126. // when
  127. create.start(canvasEvent({ x: 50, y: 50 }), newShape);
  128. dragging.move(canvasEvent({ x: 50, y: 50 }));
  129. var ctx = dragging.context();
  130. // then
  131. expect(svgClasses(ctx.data.context.visual).has('djs-drag-group')).to.be.true;
  132. }));
  133. it('should remove visuals', inject(function(create, elementRegistry, dragging, eventBus) {
  134. var parentGfx = elementRegistry.getGraphics('parentShape');
  135. // when
  136. create.start(canvasEvent({ x: 50, y: 50 }), newShape);
  137. dragging.move(canvasEvent({ x: 100, y: 100 }));
  138. dragging.hover({ element: parentShape, gfx: parentGfx });
  139. dragging.move(canvasEvent({ x: 150, y: 150 }));
  140. var ctx = dragging.context();
  141. dragging.end();
  142. // then
  143. expect(ctx.data.context.visual.parentNode).not.to.exist;
  144. }));
  145. });
  146. describe('rules', function() {
  147. it('should not allow shape create', inject(function(canvas, create, elementRegistry, dragging) {
  148. // given
  149. var targetGfx = elementRegistry.getGraphics('rootShape');
  150. // when
  151. create.start(canvasEvent({ x: 0, y: 0 }), newShape);
  152. dragging.move(canvasEvent({ x: 50, y: 25 }));
  153. dragging.hover({ element: rootShape, gfx: targetGfx });
  154. dragging.move(canvasEvent({ x: 50, y: 50 }));
  155. dragging.end();
  156. expect(elementRegistry.getGraphics('attacher')).not.to.exist;
  157. }));
  158. it('should add "new-parent" marker', inject(function(canvas, create, elementRegistry, dragging) {
  159. // given
  160. var targetGfx = elementRegistry.getGraphics('parentShape');
  161. // when
  162. create.start(canvasEvent({ x: 0, y: 0 }), newShape);
  163. dragging.move(canvasEvent({ x: 200, y: 50 }));
  164. dragging.hover({ element: parentShape, gfx: targetGfx });
  165. dragging.move(canvasEvent({ x: 200, y: 225 }));
  166. expect(canvas.hasMarker(parentShape, 'new-parent')).to.be.true;
  167. }));
  168. it('should add "drop-not-ok" marker', inject(function(canvas, create, elementRegistry, dragging) {
  169. // given
  170. var targetGfx = elementRegistry.getGraphics('childShape');
  171. // when
  172. create.start(canvasEvent({ x: 0, y: 0 }), newShape);
  173. dragging.move(canvasEvent({ x: 50, y: 25 }));
  174. dragging.hover({ element: childShape, gfx: targetGfx });
  175. dragging.move(canvasEvent({ x: 50, y: 50 }));
  176. expect(canvas.hasMarker(childShape, 'drop-not-ok')).to.be.true;
  177. }));
  178. it('should add "attach-ok" marker', inject(function(canvas, create, elementRegistry, dragging) {
  179. // given
  180. var hostGfx = elementRegistry.getGraphics('hostShape');
  181. // when
  182. create.start(canvasEvent({ x: 0, y: 0 }), newShape);
  183. dragging.move(canvasEvent({ x: 150, y: 350 }));
  184. dragging.hover({ element: hostShape, gfx: hostGfx });
  185. dragging.move(canvasEvent({ x: 200, y: 350 }));
  186. expect(canvas.hasMarker(hostShape, 'attach-ok')).to.be.true;
  187. }));
  188. it('should remove markers', inject(function(canvas, create, elementRegistry, dragging) {
  189. // given
  190. var targetGfx = elementRegistry.getGraphics('parentShape');
  191. // when
  192. create.start(canvasEvent({ x: 0, y: 0 }), newShape);
  193. dragging.move(canvasEvent({ x: 200, y: 50 }));
  194. dragging.hover({ element: parentShape, gfx: targetGfx });
  195. dragging.move(canvasEvent({ x: 200, y: 225 }));
  196. var hasMarker = canvas.hasMarker(parentShape, 'new-parent');
  197. dragging.end();
  198. expect(canvas.hasMarker(parentShape, 'new-parent')).to.be.false;
  199. expect(canvas.hasMarker(parentShape, 'new-parent')).not.to.eql(hasMarker);
  200. }));
  201. });
  202. });