GlobalConnectSpec.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 globalConnectModule from 'lib/features/global-connect';
  9. import rulesModule from './rules';
  10. describe('features/global-connect-tool', function() {
  11. beforeEach(bootstrapDiagram({
  12. modules: [
  13. modelingModule,
  14. globalConnectModule,
  15. rulesModule
  16. ]
  17. }));
  18. var rootShape, shapeAbleToStartConnection, shapeUnableToStartConnection;
  19. beforeEach(inject(function(elementFactory, canvas) {
  20. rootShape = elementFactory.createRoot({
  21. id: 'root'
  22. });
  23. canvas.setRootElement(rootShape);
  24. shapeAbleToStartConnection = elementFactory.createShape({
  25. id: 's1',
  26. x: 100, y: 100, width: 300, height: 300,
  27. canStartConnection: true
  28. });
  29. canvas.addShape(shapeAbleToStartConnection, rootShape);
  30. shapeUnableToStartConnection = elementFactory.createShape({
  31. id: 's2',
  32. x: 500, y: 100, width: 100, height: 100
  33. });
  34. canvas.addShape(shapeUnableToStartConnection, rootShape);
  35. }));
  36. it('should start connect if allowed', inject(function(eventBus, globalConnect, dragging) {
  37. // given
  38. var shape = shapeAbleToStartConnection;
  39. var connectSpy = sinon.spy(function(event) {
  40. expect(event.context).to.eql({
  41. source: shape,
  42. sourcePosition: { x: 150, y: 130 }
  43. });
  44. });
  45. eventBus.once('connect.init', connectSpy);
  46. // when
  47. globalConnect.start(canvasEvent({ x: 0, y: 0 }));
  48. dragging.move(canvasEvent({ x: 150, y: 130 }));
  49. dragging.hover(canvasEvent({ x: 150, y: 130 }, { element: shape }));
  50. dragging.end(canvasEvent({ x: 0, y: 0 }));
  51. eventBus.fire('element.out', canvasEvent({ x: 99, y: 99 }, { element: shape }));
  52. // then
  53. expect(connectSpy).to.have.been.called;
  54. }));
  55. it('should NOT start connect if rejected', inject(function(eventBus, globalConnect, dragging) {
  56. // given
  57. var shape = shapeUnableToStartConnection;
  58. var connectSpy = sinon.spy();
  59. eventBus.once('connect.init', connectSpy);
  60. // when
  61. globalConnect.start(canvasEvent({ x: 0, y: 0 }));
  62. dragging.hover(canvasEvent({ x: 150, y: 150 }, { element: shape }));
  63. dragging.end(canvasEvent({ x: 0, y: 0 }));
  64. eventBus.fire('element.out', canvasEvent({ x: 99, y: 99 }, { element: shape }));
  65. // then
  66. expect(connectSpy).to.not.have.been.called;
  67. }));
  68. });