BendpointsSpec.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* global sinon */
  2. import {
  3. bootstrapDiagram,
  4. inject
  5. } from 'test/TestHelper';
  6. import modelingModule from 'lib/features/modeling';
  7. import bendpointsModule from 'lib/features/bendpoints';
  8. import rulesModule from './rules';
  9. import interactionModule from 'lib/features/interaction-events';
  10. import {
  11. createCanvasEvent as canvasEvent
  12. } from '../../../util/MockEvents';
  13. import {
  14. query as domQuery,
  15. queryAll as domQueryAll
  16. } from 'min-dom';
  17. describe('features/bendpoints', function() {
  18. beforeEach(bootstrapDiagram({
  19. modules: [
  20. modelingModule,
  21. bendpointsModule,
  22. interactionModule,
  23. rulesModule
  24. ]
  25. }));
  26. beforeEach(inject(function(dragging) {
  27. dragging.setOptions({ manual: true });
  28. }));
  29. var rootShape, shape1, shape2, shape3, connection, connection2;
  30. beforeEach(inject(function(elementFactory, canvas) {
  31. rootShape = elementFactory.createRoot({
  32. id: 'root'
  33. });
  34. canvas.setRootElement(rootShape);
  35. shape1 = elementFactory.createShape({
  36. id: 'shape.1',
  37. type: 'A',
  38. x: 100, y: 100, width: 300, height: 300
  39. });
  40. canvas.addShape(shape1, rootShape);
  41. shape2 = elementFactory.createShape({
  42. id: 'shape2',
  43. type: 'A',
  44. x: 500, y: 100, width: 100, height: 100
  45. });
  46. canvas.addShape(shape2, rootShape);
  47. shape3 = elementFactory.createShape({
  48. id: 'shape3',
  49. type: 'B',
  50. x: 500, y: 400, width: 100, height: 100
  51. });
  52. canvas.addShape(shape3, rootShape);
  53. connection = elementFactory.createConnection({
  54. id: 'connection.1',
  55. waypoints: [ { x: 250, y: 250 }, { x: 550, y: 250 }, { x: 550, y: 150 } ],
  56. source: shape1,
  57. target: shape2
  58. });
  59. canvas.addConnection(connection, rootShape);
  60. connection2 = elementFactory.createConnection({
  61. id: 'connection2',
  62. waypoints: [ { x: 250, y: 250 }, { x: 550, y: 450 } ],
  63. source: shape1,
  64. target: shape2
  65. });
  66. canvas.addConnection(connection2, rootShape);
  67. }));
  68. describe('activation', function() {
  69. it('should show on hover', inject(function(eventBus, canvas, elementRegistry) {
  70. // given
  71. var layer = canvas.getLayer('overlays');
  72. // when
  73. eventBus.fire('element.hover', {
  74. element: connection,
  75. gfx: elementRegistry.getGraphics(connection)
  76. });
  77. // then
  78. // 3 visible + 1 invisible bendpoint are shown
  79. expect(domQueryAll('.djs-bendpoint', layer).length).to.equal(4);
  80. expect(domQueryAll('.djs-segment-dragger', layer).length).to.equal(2);
  81. }));
  82. it('should show on select', inject(function(selection, canvas, elementRegistry) {
  83. // given
  84. var layer = canvas.getLayer('overlays');
  85. // when
  86. selection.select(connection);
  87. // then
  88. // 3 visible + 1 invisible bendpoint are shown
  89. expect(domQueryAll('.djs-bendpoint', layer).length).to.equal(4);
  90. expect(domQueryAll('.djs-segment-dragger', layer).length).to.equal(2);
  91. }));
  92. it('should activate bendpoint move', inject(
  93. function(dragging, eventBus, elementRegistry, bendpoints) {
  94. // when
  95. eventBus.fire('element.hover', {
  96. element: connection,
  97. gfx: elementRegistry.getGraphics(connection)
  98. });
  99. eventBus.fire('element.mousemove', {
  100. element: connection,
  101. originalEvent: canvasEvent({ x: 500, y: 250 })
  102. });
  103. eventBus.fire('element.mousedown', {
  104. element: connection,
  105. originalEvent: canvasEvent({ x: 500, y: 250 })
  106. });
  107. var draggingContext = dragging.context();
  108. // then
  109. expect(draggingContext).to.exist;
  110. expect(draggingContext.prefix).to.eql('bendpoint.move');
  111. }
  112. ));
  113. it('should activate parallel move', inject(
  114. function(dragging, eventBus, elementRegistry, bendpoints) {
  115. // precondition
  116. var intersectionStart = connection.waypoints[0].x,
  117. intersectionEnd = connection.waypoints[1].x,
  118. intersectionMid = intersectionEnd - (intersectionEnd - intersectionStart) / 2;
  119. // when
  120. eventBus.fire('element.hover', {
  121. element: connection,
  122. gfx: elementRegistry.getGraphics(connection)
  123. });
  124. eventBus.fire('element.mousemove', {
  125. element: connection,
  126. originalEvent: canvasEvent({ x: intersectionMid, y: 250 })
  127. });
  128. eventBus.fire('element.mousedown', {
  129. element: connection,
  130. originalEvent: canvasEvent({ x: intersectionMid, y: 250 })
  131. });
  132. var draggingContext = dragging.context();
  133. // then
  134. expect(draggingContext).to.exist;
  135. expect(draggingContext.prefix).to.eql('connectionSegment.move');
  136. }
  137. ));
  138. describe('should trigger interaction events', function() {
  139. function triggerMouseEvent(type, gfx) {
  140. var event = document.createEvent('MouseEvent');
  141. event.initMouseEvent(type, true, true, window, 0, 0, 0, 80, 20, false, false, false, false, 0, null);
  142. return gfx.dispatchEvent(event);
  143. }
  144. var bendpointGfx,
  145. listenerSpy;
  146. beforeEach(inject(function(bendpoints, eventBus) {
  147. bendpoints.addHandles(connection);
  148. bendpointGfx = domQuery('.djs-bendpoint', bendpoints.getBendpointsContainer(connection));
  149. listenerSpy = sinon.spy(function(event) {
  150. expect(event.originalEvent.target).to.equal(bendpointGfx);
  151. expect(event.element).to.equal(connection);
  152. });
  153. }));
  154. it('element.click', inject(function(eventBus, bendpoints) {
  155. // given
  156. eventBus.once('element.click', listenerSpy);
  157. // when
  158. triggerMouseEvent('click', bendpointGfx);
  159. // then
  160. expect(listenerSpy).to.have.been.called;
  161. }));
  162. it('element.dblclick', inject(function(eventBus, bendpoints) {
  163. // given
  164. eventBus.once('element.dblclick', listenerSpy);
  165. // when
  166. triggerMouseEvent('dblclick', bendpointGfx);
  167. // then
  168. expect(listenerSpy).to.have.been.called;
  169. }));
  170. it('element.mousedown', inject(function(eventBus, bendpoints) {
  171. // given
  172. eventBus.once('element.mousedown', listenerSpy);
  173. // when
  174. triggerMouseEvent('mousedown', bendpointGfx);
  175. // then
  176. expect(listenerSpy).to.have.been.called;
  177. }));
  178. });
  179. });
  180. describe('updating', function() {
  181. it('should update on element updated ID', inject(
  182. function(selection, canvas, elementRegistry) {
  183. // given
  184. var layer = canvas.getLayer('overlays');
  185. selection.select(connection);
  186. // when
  187. elementRegistry.updateId(connection, 'foo');
  188. var bendpointContainer = domQuery('.djs-bendpoints', layer);
  189. // then
  190. // bendpoint container references element with updated ID
  191. expect(bendpointContainer.dataset.elementId).to.equal('foo');
  192. }
  193. ));
  194. });
  195. });