ConnectSpec.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* global sinon */
  2. import {
  3. bootstrapDiagram,
  4. inject
  5. } from 'test/TestHelper';
  6. import { createCanvasEvent as canvasEvent } from '../../../util/MockEvents';
  7. import modelingModule from 'lib/features/modeling';
  8. import rulesModule from './rules';
  9. import connectModule from 'lib/features/connect';
  10. describe('features/connect', function() {
  11. beforeEach(bootstrapDiagram({
  12. modules: [
  13. modelingModule,
  14. connectModule,
  15. rulesModule
  16. ]
  17. }));
  18. beforeEach(inject(function(canvas, dragging) {
  19. dragging.setOptions({ manual: true });
  20. }));
  21. var rootShape, shape1, shape2, shape1child;
  22. beforeEach(inject(function(elementFactory, canvas) {
  23. rootShape = elementFactory.createRoot({
  24. id: 'root'
  25. });
  26. canvas.setRootElement(rootShape);
  27. shape1 = elementFactory.createShape({
  28. id: 's1',
  29. x: 100, y: 100, width: 300, height: 300
  30. });
  31. canvas.addShape(shape1, rootShape);
  32. shape2 = elementFactory.createShape({
  33. id: 's2',
  34. x: 500, y: 100, width: 100, height: 100
  35. });
  36. canvas.addShape(shape2, rootShape);
  37. shape1child = elementFactory.createShape({
  38. id: 's3',
  39. x: 150, y: 150, width: 50, height: 50
  40. });
  41. canvas.addShape(shape1child, shape1);
  42. }));
  43. describe('behavior', function() {
  44. it('should connect if allowed', inject(function(connect, dragging) {
  45. // when
  46. connect.start(canvasEvent({ x: 0, y: 0 }), shape1);
  47. dragging.move(canvasEvent({ x: 40, y: 30 }));
  48. dragging.hover(canvasEvent({ x: 10, y: 10 }, { element: shape2 }));
  49. dragging.end();
  50. var newConnection = shape1.outgoing[0];
  51. // then
  52. expect(newConnection).to.exist;
  53. expect(newConnection.target).to.equal(shape2);
  54. }));
  55. it('should not connect if rejected', inject(function(connect, rules, dragging) {
  56. // assume
  57. var context = {
  58. source: shape1child,
  59. target: shape2
  60. };
  61. expect(rules.allowed('connection.create', context)).to.be.false;
  62. // when
  63. connect.start(canvasEvent({ x: 0, y: 0 }), shape1child);
  64. dragging.move(canvasEvent({ x: 40, y: 30 }));
  65. dragging.hover(canvasEvent({ x: 10, y: 10 }, { element: shape2 }));
  66. dragging.end();
  67. // then
  68. expect(shape1child.outgoing.length).to.equal(0);
  69. expect(shape2.incoming.length).to.equal(0);
  70. }));
  71. it('should not connect with null target', inject(function(connect, rules, dragging) {
  72. // when
  73. connect.start(canvasEvent({ x: 0, y: 0 }), shape1);
  74. dragging.move(canvasEvent({ x: 40, y: 30 }));
  75. dragging.hover(canvasEvent({ x: 40, y: 30 }, { element: shape2 }));
  76. dragging.out(canvasEvent({ x: 40, y: 30 }));
  77. dragging.end();
  78. // then
  79. expect(shape1.outgoing.length).to.equal(0);
  80. expect(shape2.incoming.length).to.equal(0);
  81. }));
  82. it('should connect with start position', inject(function(connect, dragging, modeling) {
  83. // given
  84. var connectSpy = sinon.spy(modeling, 'connect');
  85. // when
  86. connect.start(canvasEvent({ x: 200, y: 0 }), shape1, { x: 200, y: 0 });
  87. dragging.move(canvasEvent({ x: 555, y: 153 }));
  88. dragging.hover(canvasEvent({ x: 555, y: 153 }, { element: shape2 }));
  89. dragging.end();
  90. // then
  91. var expectedHints = {
  92. connectionStart: { x: 200, y: 0 },
  93. connectionEnd: { x: 555, y: 153 }
  94. };
  95. expect(connectSpy).to.have.been.calledWith(
  96. shape1, shape2,
  97. { type: 'test:Connection' },
  98. expectedHints
  99. );
  100. }));
  101. it('should pass meta-data and hints to modeling', inject(function(connect, dragging, modeling) {
  102. // given
  103. var connectSpy = sinon.spy(modeling, 'connect');
  104. // assume
  105. // connect rule returns { type: 'test:Connection' }
  106. // when
  107. connect.start(canvasEvent({ x: 250, y: 250 }), shape1);
  108. dragging.move(canvasEvent({ x: 550, y: 150 }));
  109. dragging.hover(canvasEvent({ x: 550, y: 150 }, { element: shape2 }));
  110. dragging.end();
  111. // then
  112. var expectedHints = {
  113. connectionStart: { x: 250, y: 250 },
  114. connectionEnd: { x: 550, y: 150 }
  115. };
  116. expect(connectSpy).to.have.been.calledWith(
  117. shape1, shape2,
  118. { type: 'test:Connection' },
  119. expectedHints
  120. );
  121. }));
  122. });
  123. });