SnappingSpec.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. import {
  2. bootstrapDiagram,
  3. getDiagramJS,
  4. inject
  5. } from 'test/TestHelper';
  6. import { assign } from 'min-dash';
  7. import snappingModule from 'lib/features/snapping';
  8. import modelingModule from 'lib/features/modeling';
  9. import moveModule from 'lib/features/move';
  10. import createModule from 'lib/features/create';
  11. import attachSupportModule from 'lib/features/attach-support';
  12. import {
  13. createCanvasEvent as canvasEvent
  14. } from '../../util/MockEvents';
  15. import SnapContext from 'lib/features/snapping/SnapContext';
  16. describe('features/snapping - Snapping', function() {
  17. describe('basics', function() {
  18. beforeEach(bootstrapDiagram({
  19. modules: [
  20. modelingModule,
  21. snappingModule
  22. ]
  23. }));
  24. var rootElement, shape;
  25. beforeEach(inject(function(canvas, elementFactory) {
  26. rootElement = elementFactory.createRoot({
  27. id: 'root'
  28. });
  29. canvas.setRootElement(rootElement);
  30. shape = canvas.addShape(elementFactory.createShape({
  31. id: 'shape',
  32. x: 100, y: 100, width: 100, height: 100
  33. }));
  34. // snap target
  35. canvas.addShape(elementFactory.createShape({
  36. id: 'snap-target',
  37. x: 400, y: 150, width: 50, height: 50
  38. }));
  39. }));
  40. describe('#initSnap', function() {
  41. it('should init snapContext', inject(function(snapping) {
  42. // given
  43. var event = {
  44. x: 100,
  45. y: 100,
  46. context: {
  47. shape: shape
  48. }
  49. };
  50. // when
  51. var snapContext = snapping.initSnap(event);
  52. // then
  53. expect(snapContext).to.exist;
  54. expect(event.context.snapContext).to.equal(snapContext);
  55. }));
  56. it('should reuse existing snapContext', inject(function(snapping) {
  57. // given
  58. var originalSnapContext = new SnapContext();
  59. var event = {
  60. x: 100,
  61. y: 100,
  62. context: {
  63. shape: shape,
  64. snapContext: originalSnapContext
  65. }
  66. };
  67. // when
  68. var snapContext = snapping.initSnap(event);
  69. // then
  70. expect(snapContext).to.equal(originalSnapContext);
  71. }));
  72. });
  73. describe('eventBus integration', function() {
  74. var startEvent;
  75. beforeEach(inject(function(eventBus) {
  76. startEvent = eventBus.createEvent({
  77. x: 150,
  78. y: 150,
  79. context: {
  80. shape: shape,
  81. target: rootElement
  82. }
  83. });
  84. }));
  85. function moveTo(startEvent, newPosition) {
  86. return getDiagramJS().invoke(function(eventBus) {
  87. return eventBus.createEvent(assign(startEvent, {
  88. x: newPosition.x,
  89. y: newPosition.y,
  90. dx: newPosition.x - startEvent.x,
  91. dy: newPosition.y - startEvent.y
  92. }));
  93. });
  94. }
  95. it('should init on shape.move.start', inject(function(eventBus) {
  96. // when
  97. eventBus.fire('shape.move.start', startEvent);
  98. // then
  99. expect(startEvent.context.snapContext).to.exist;
  100. }));
  101. it('should init on create.start', inject(function(eventBus) {
  102. // when
  103. eventBus.fire('create.start', startEvent);
  104. // then
  105. expect(startEvent.context.snapContext).to.exist;
  106. }));
  107. it('should snap on shape.move.move / horizontally', inject(function(eventBus) {
  108. // given
  109. eventBus.fire('shape.move.start', startEvent);
  110. // when
  111. var moveEvent = moveTo(startEvent, { x: 180, y: 170 });
  112. eventBus.fire('shape.move.move', moveEvent);
  113. // then
  114. expect(moveEvent.x).to.eql(180);
  115. expect(moveEvent.y).to.eql(175); // snap at (180,175)
  116. }));
  117. it('should snap on shape.move.move / vertically', inject(function(eventBus) {
  118. // given
  119. eventBus.fire('shape.move.start', startEvent);
  120. // when
  121. var moveEvent = moveTo(startEvent, { x: 420, y: 200 });
  122. eventBus.fire('shape.move.move', moveEvent);
  123. // then
  124. expect(moveEvent.x).to.eql(425);
  125. expect(moveEvent.y).to.eql(200); // snap at (425,200)
  126. }));
  127. it('should snap on shape.move.end', inject(function(eventBus) {
  128. // given
  129. eventBus.fire('shape.move.start', startEvent);
  130. // when
  131. var endEvent = moveTo(startEvent, { x: 180, y: 170 });
  132. eventBus.fire('shape.move.end', endEvent);
  133. // then
  134. expect(endEvent.x).to.eql(180);
  135. expect(endEvent.y).to.eql(175); // snap at (180,175)
  136. }));
  137. });
  138. });
  139. // TODO(nikku): test this (!)
  140. describe('snap lines', function() {
  141. it('should show');
  142. it('should hide async');
  143. });
  144. describe('util', function() {
  145. beforeEach(bootstrapDiagram({
  146. modules: [
  147. modelingModule,
  148. snappingModule,
  149. attachSupportModule
  150. ]
  151. }));
  152. var rootElement, shape, sibling;
  153. beforeEach(inject(function(canvas, elementFactory) {
  154. rootElement = elementFactory.createRoot({
  155. id: 'root'
  156. });
  157. canvas.setRootElement(rootElement);
  158. shape = canvas.addShape(elementFactory.createShape({
  159. id: 'shape1',
  160. x: 100, y: 100, width: 100, height: 100
  161. }));
  162. sibling = canvas.addShape(elementFactory.createShape({
  163. id: 'shape2',
  164. x: 400, y: 150, width: 50, height: 50
  165. }));
  166. }));
  167. it('getSiblings', inject(function(canvas, elementFactory, modeling, snapping) {
  168. // given
  169. var attacher = elementFactory.createShape({
  170. id: 'attacher',
  171. width: 50, height: 50
  172. });
  173. modeling.createShape(attacher, { x: 100, y: 100 }, shape, true);
  174. var label = elementFactory.createLabel({
  175. id: 'label',
  176. width: 80, height: 40
  177. });
  178. modeling.createLabel(shape, { x: 150, y: 250 }, label, rootElement);
  179. var connection = modeling.connect(shape, sibling);
  180. // when
  181. var siblings = snapping.getSiblings(shape, rootElement);
  182. // then
  183. expect(siblings).to.have.length(2);
  184. expect(siblings).to.contain(sibling);
  185. expect(siblings).to.contain(connection);
  186. }));
  187. });
  188. describe('integration', function() {
  189. beforeEach(bootstrapDiagram({
  190. modules: [
  191. createModule,
  192. snappingModule,
  193. modelingModule,
  194. moveModule
  195. ]
  196. }));
  197. beforeEach(inject(function(dragging, elementRegistry) {
  198. dragging.setOptions({ manual: true });
  199. }));
  200. var rootElement;
  201. beforeEach(inject(function(canvas, elementFactory) {
  202. rootElement = elementFactory.createRoot({
  203. id: 'root'
  204. });
  205. canvas.setRootElement(rootElement);
  206. canvas.addShape(elementFactory.createShape({
  207. id: 'snap-target',
  208. x: 100, y: 100, width: 100, height: 100
  209. }));
  210. }));
  211. it('should snap horizontal on create', inject(function(create, dragging, elementFactory, elementRegistry) {
  212. // given
  213. var newShape = elementFactory.createShape({
  214. id: 'new-shape',
  215. x: 0, y: 0,
  216. width: 100, height: 100
  217. });
  218. // when
  219. create.start(canvasEvent({ x: 50, y: 50 }), newShape);
  220. dragging.hover({ element: rootElement });
  221. dragging.move(canvasEvent({ x: 100, y: 350 }));
  222. dragging.move(canvasEvent({ x: 145, y: 350 }));
  223. dragging.end();
  224. var createdShape = elementRegistry.get('new-shape');
  225. // then
  226. expect(createdShape).to.have.bounds({
  227. x: 100, // snapped to mid(100, _)
  228. y: 300,
  229. width: 100,
  230. height: 100
  231. });
  232. }));
  233. it('should snap vertical on create', inject(function(create, dragging, elementFactory, elementRegistry) {
  234. // given
  235. var newShape = elementFactory.createShape({
  236. id: 'new-shape',
  237. x: 0, y: 0,
  238. width: 100, height: 100
  239. });
  240. // when
  241. create.start(canvasEvent({ x: 50, y: 50 }), newShape);
  242. dragging.hover({ element: rootElement });
  243. dragging.move(canvasEvent({ x: 100, y: 145 }));
  244. dragging.move(canvasEvent({ x: 350, y: 145 }));
  245. dragging.end();
  246. var createdShape = elementRegistry.get('new-shape');
  247. // then
  248. expect(createdShape).to.have.bounds({
  249. x: 300, // snapped to mid(100, _)
  250. y: 100,
  251. width: 100,
  252. height: 100
  253. });
  254. }));
  255. });
  256. });