MovePreviewSpec.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 attachSupportModule from 'lib/features/attach-support';
  11. import rulesModule from './rules';
  12. import {
  13. query as domQuery
  14. } from 'min-dom';
  15. import {
  16. classes as svgClasses
  17. } from 'tiny-svg';
  18. describe('features/move - MovePreview', function() {
  19. beforeEach(bootstrapDiagram({
  20. modules: [
  21. moveModule,
  22. attachSupportModule,
  23. rulesModule,
  24. modelingModule
  25. ]
  26. }));
  27. beforeEach(inject(function(canvas, dragging) {
  28. dragging.setOptions({ manual: true });
  29. }));
  30. var rootShape, parentShape, childShape, childShape2, connection;
  31. beforeEach(inject(function(elementFactory, canvas) {
  32. rootShape = elementFactory.createRoot({
  33. id: 'root'
  34. });
  35. canvas.setRootElement(rootShape);
  36. parentShape = elementFactory.createShape({
  37. id: 'parent',
  38. x: 100, y: 100, width: 300, height: 300
  39. });
  40. canvas.addShape(parentShape, rootShape);
  41. childShape = elementFactory.createShape({
  42. id: 'child',
  43. x: 110, y: 110, width: 100, height: 100
  44. });
  45. canvas.addShape(childShape, parentShape);
  46. childShape2 = elementFactory.createShape({
  47. id: 'child2',
  48. x: 200, y: 110, width: 100, height: 100
  49. });
  50. canvas.addShape(childShape2, parentShape);
  51. connection = elementFactory.createConnection({
  52. id: 'connection',
  53. waypoints: [ { x: 150, y: 150 }, { x: 150, y: 200 }, { x: 350, y: 150 } ],
  54. source: childShape,
  55. target: childShape2
  56. });
  57. canvas.addConnection(connection, parentShape);
  58. }));
  59. describe('style integration via <djs-dragging>', function() {
  60. it('should append class to shape + outgoing connections', inject(function(move, dragging, elementRegistry) {
  61. // given
  62. move.start(canvasEvent({ x: 10, y: 10 }), childShape);
  63. // when
  64. dragging.move(canvasEvent({ x: 20, y: 20 }));
  65. // then
  66. expect(svgClasses(elementRegistry.getGraphics(childShape)).has('djs-dragging')).to.equal(true);
  67. expect(svgClasses(elementRegistry.getGraphics(connection)).has('djs-dragging')).to.equal(true);
  68. }));
  69. it('should append class to shape + incoming connections', inject(function(move, dragging, elementRegistry) {
  70. // given
  71. move.start(canvasEvent({ x: 10, y: 10 }), childShape2);
  72. // when
  73. dragging.move(canvasEvent({ x: 20, y: 20 }));
  74. // then
  75. expect(svgClasses(elementRegistry.getGraphics(childShape2)).has('djs-dragging')).to.equal(true);
  76. expect(svgClasses(elementRegistry.getGraphics(connection)).has('djs-dragging')).to.equal(true);
  77. }));
  78. it('should remove class on drag end', inject(function(move, dragging, elementRegistry) {
  79. // given
  80. move.start(canvasEvent({ x: 10, y: 10 }), childShape2);
  81. // when
  82. dragging.move(canvasEvent({ x: 30, y: 30 }));
  83. dragging.end();
  84. // then
  85. expect(svgClasses(elementRegistry.getGraphics(childShape2)).has('djs-dragging')).to.equal(false);
  86. expect(svgClasses(elementRegistry.getGraphics(connection)).has('djs-dragging')).to.equal(false);
  87. }));
  88. });
  89. describe('drop markup', function() {
  90. it('should indicate drop allowed', inject(function(move, dragging, elementRegistry) {
  91. // given
  92. move.start(canvasEvent({ x: 10, y: 10 }), childShape);
  93. // when
  94. dragging.move(canvasEvent({ x: 20, y: 20 }));
  95. dragging.hover(canvasEvent({ x: 20, y: 20 }, {
  96. element: parentShape,
  97. gfx: elementRegistry.getGraphics(parentShape)
  98. }));
  99. dragging.move(canvasEvent({ x: 22, y: 22 }));
  100. // then
  101. var ctx = dragging.context();
  102. expect(ctx.data.context.canExecute).to.equal(true);
  103. expect(svgClasses(elementRegistry.getGraphics(parentShape)).has('drop-ok')).to.equal(true);
  104. }));
  105. it('should indicate drop not allowed', inject(function(move, dragging, elementRegistry) {
  106. // given
  107. move.start(canvasEvent({ x: 10, y: 10 }), childShape);
  108. // when
  109. dragging.move(canvasEvent({ x: 20, y: 20 }));
  110. dragging.hover(canvasEvent({ x: 20, y: 20 }, {
  111. element: childShape,
  112. gfx: elementRegistry.getGraphics(childShape)
  113. }));
  114. dragging.move(canvasEvent({ x: 22, y: 22 }));
  115. // then
  116. var ctx = dragging.context();
  117. expect(ctx.data.context.canExecute).to.equal(false);
  118. expect(svgClasses(elementRegistry.getGraphics(childShape)).has('drop-not-ok')).to.equal(true);
  119. }));
  120. });
  121. describe('drop markup on target change', function() {
  122. var differentShape;
  123. beforeEach(inject(function(elementFactory, canvas) {
  124. differentShape = elementFactory.createShape({
  125. id: 'differentShape',
  126. x: 10, y: 110, width: 50, height: 50
  127. });
  128. canvas.addShape(differentShape, rootShape);
  129. }));
  130. it('should indicate new target, if selected shapes have different parents',
  131. inject(function(move, dragging, elementRegistry, selection) {
  132. // given
  133. selection.select([ childShape, differentShape ]);
  134. move.start(canvasEvent({ x: 10, y: 10 }), differentShape);
  135. // when
  136. dragging.move(canvasEvent({ x: 200, y: 200 }));
  137. dragging.hover(canvasEvent({ x: 200, y: 200 }, {
  138. element: parentShape,
  139. gfx: elementRegistry.getGraphics(parentShape)
  140. }));
  141. dragging.move(canvasEvent({ x: 120, y: 180 }));
  142. // then
  143. var ctx = dragging.context();
  144. expect(ctx.data.context.differentParents).to.equal(true);
  145. expect(svgClasses(elementRegistry.getGraphics(parentShape)).has('new-parent')).to.equal(true);
  146. })
  147. );
  148. it('should not indicate new target, if target does not change',
  149. inject(function(move, dragging, elementRegistry, selection) {
  150. // given
  151. selection.select([ childShape, differentShape ]);
  152. move.start(canvasEvent({ x: 10, y: 10 }), childShape);
  153. // when
  154. dragging.move(canvasEvent({ x: 200, y: 200 }));
  155. dragging.hover(canvasEvent({ x: 200, y: 200 }, {
  156. element: parentShape,
  157. gfx: elementRegistry.getGraphics(parentShape)
  158. }));
  159. dragging.move(canvasEvent({ x: 120, y: 180 }));
  160. // then
  161. var ctx = dragging.context();
  162. expect(ctx.data.context.differentParents).to.equal(true);
  163. expect(svgClasses(elementRegistry.getGraphics(parentShape)).has('drop-new-target')).to.equal(false);
  164. })
  165. );
  166. it('should not indicate new target, if drop is not allowed',
  167. inject(function(move, dragging, elementRegistry, selection) {
  168. // given
  169. selection.select([ childShape, differentShape ]);
  170. move.start(canvasEvent({ x: 10, y: 10 }), differentShape);
  171. // when
  172. dragging.move(canvasEvent({ x: 200, y: 200 }));
  173. dragging.hover(canvasEvent({ x: 200, y: 200 }, {
  174. element: childShape,
  175. gfx: elementRegistry.getGraphics(childShape)
  176. }));
  177. dragging.move(canvasEvent({ x: 150, y: 15 }));
  178. // then
  179. var ctx = dragging.context();
  180. expect(ctx.data.context.differentParents).to.equal(true);
  181. var childGfx = elementRegistry.getGraphics(childShape);
  182. expect(svgClasses(childGfx).has('drop-new-target')).to.equal(false);
  183. expect(svgClasses(childGfx).has('drop-not-ok')).to.equal(true);
  184. })
  185. );
  186. });
  187. describe('connections', function() {
  188. var host, attacher, parentShape2, shape, connectionA, connectionB;
  189. beforeEach(inject(function(elementFactory, canvas, modeling) {
  190. parentShape2 = elementFactory.createShape({
  191. id: 'parentShape2',
  192. x: 450, y: 50, width: 400, height: 200
  193. });
  194. canvas.addShape(parentShape2, rootShape);
  195. host = elementFactory.createShape({
  196. id: 'host',
  197. x: 500, y: 100, width: 100, height: 100
  198. });
  199. canvas.addShape(host, parentShape2);
  200. attacher = elementFactory.createShape({
  201. id: 'attacher',
  202. x: 0, y: 0, width: 50, height: 50
  203. });
  204. modeling.createShape(attacher, { x: 600, y: 100 }, host, true);
  205. shape = elementFactory.createShape({
  206. id: 'shape',
  207. x: 700, y: 100, width: 100, height: 100
  208. });
  209. canvas.addShape(shape, parentShape2);
  210. connectionA = elementFactory.createConnection({
  211. id: 'connectionA',
  212. waypoints: [ { x: 500, y: 100 }, { x: 750, y: 150 } ],
  213. source: host,
  214. target: shape
  215. });
  216. canvas.addConnection(connectionA, parentShape2);
  217. connectionB = elementFactory.createConnection({
  218. id: 'connectionB',
  219. waypoints: [ { x: 600, y: 100 }, { x: 750, y: 150 } ],
  220. source: attacher,
  221. target: shape
  222. });
  223. canvas.addConnection(connectionB, parentShape2);
  224. }));
  225. it('should add connections to dragGroup',
  226. inject(function(canvas, elementFactory, move, dragging, elementRegistry, selection, modeling) {
  227. var rootGfx = elementRegistry.getGraphics(rootShape),
  228. dragGroup;
  229. // when
  230. selection.select([ host, shape ]);
  231. move.start(canvasEvent({ x: 0, y: 0 }), host);
  232. dragging.hover({
  233. element: rootShape,
  234. gfx: rootGfx
  235. });
  236. dragging.move(canvasEvent({ x: 150, y: 200 }));
  237. dragGroup = dragging.context().data.context.dragGroup;
  238. // then
  239. expect(domQuery('[data-element-id="connectionA"]', dragGroup)).to.exist;
  240. expect(domQuery('[data-element-id="connectionB"]', dragGroup)).to.exist;
  241. })
  242. );
  243. });
  244. });