123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- import {
- bootstrapDiagram,
- getDiagramJS,
- inject
- } from 'test/TestHelper';
- import { assign } from 'min-dash';
- import snappingModule from 'lib/features/snapping';
- import modelingModule from 'lib/features/modeling';
- import moveModule from 'lib/features/move';
- import createModule from 'lib/features/create';
- import attachSupportModule from 'lib/features/attach-support';
- import {
- createCanvasEvent as canvasEvent
- } from '../../util/MockEvents';
- import SnapContext from 'lib/features/snapping/SnapContext';
- describe('features/snapping - Snapping', function() {
- describe('basics', function() {
- beforeEach(bootstrapDiagram({
- modules: [
- modelingModule,
- snappingModule
- ]
- }));
- var rootElement, shape;
- beforeEach(inject(function(canvas, elementFactory) {
- rootElement = elementFactory.createRoot({
- id: 'root'
- });
- canvas.setRootElement(rootElement);
- shape = canvas.addShape(elementFactory.createShape({
- id: 'shape',
- x: 100, y: 100, width: 100, height: 100
- }));
- // snap target
- canvas.addShape(elementFactory.createShape({
- id: 'snap-target',
- x: 400, y: 150, width: 50, height: 50
- }));
- }));
- describe('#initSnap', function() {
- it('should init snapContext', inject(function(snapping) {
- // given
- var event = {
- x: 100,
- y: 100,
- context: {
- shape: shape
- }
- };
- // when
- var snapContext = snapping.initSnap(event);
- // then
- expect(snapContext).to.exist;
- expect(event.context.snapContext).to.equal(snapContext);
- }));
- it('should reuse existing snapContext', inject(function(snapping) {
- // given
- var originalSnapContext = new SnapContext();
- var event = {
- x: 100,
- y: 100,
- context: {
- shape: shape,
- snapContext: originalSnapContext
- }
- };
- // when
- var snapContext = snapping.initSnap(event);
- // then
- expect(snapContext).to.equal(originalSnapContext);
- }));
- });
- describe('eventBus integration', function() {
- var startEvent;
- beforeEach(inject(function(eventBus) {
- startEvent = eventBus.createEvent({
- x: 150,
- y: 150,
- context: {
- shape: shape,
- target: rootElement
- }
- });
- }));
- function moveTo(startEvent, newPosition) {
- return getDiagramJS().invoke(function(eventBus) {
- return eventBus.createEvent(assign(startEvent, {
- x: newPosition.x,
- y: newPosition.y,
- dx: newPosition.x - startEvent.x,
- dy: newPosition.y - startEvent.y
- }));
- });
- }
- it('should init on shape.move.start', inject(function(eventBus) {
- // when
- eventBus.fire('shape.move.start', startEvent);
- // then
- expect(startEvent.context.snapContext).to.exist;
- }));
- it('should init on create.start', inject(function(eventBus) {
- // when
- eventBus.fire('create.start', startEvent);
- // then
- expect(startEvent.context.snapContext).to.exist;
- }));
- it('should snap on shape.move.move / horizontally', inject(function(eventBus) {
- // given
- eventBus.fire('shape.move.start', startEvent);
- // when
- var moveEvent = moveTo(startEvent, { x: 180, y: 170 });
- eventBus.fire('shape.move.move', moveEvent);
- // then
- expect(moveEvent.x).to.eql(180);
- expect(moveEvent.y).to.eql(175); // snap at (180,175)
- }));
- it('should snap on shape.move.move / vertically', inject(function(eventBus) {
- // given
- eventBus.fire('shape.move.start', startEvent);
- // when
- var moveEvent = moveTo(startEvent, { x: 420, y: 200 });
- eventBus.fire('shape.move.move', moveEvent);
- // then
- expect(moveEvent.x).to.eql(425);
- expect(moveEvent.y).to.eql(200); // snap at (425,200)
- }));
- it('should snap on shape.move.end', inject(function(eventBus) {
- // given
- eventBus.fire('shape.move.start', startEvent);
- // when
- var endEvent = moveTo(startEvent, { x: 180, y: 170 });
- eventBus.fire('shape.move.end', endEvent);
- // then
- expect(endEvent.x).to.eql(180);
- expect(endEvent.y).to.eql(175); // snap at (180,175)
- }));
- });
- });
- // TODO(nikku): test this (!)
- describe('snap lines', function() {
- it('should show');
- it('should hide async');
- });
- describe('util', function() {
- beforeEach(bootstrapDiagram({
- modules: [
- modelingModule,
- snappingModule,
- attachSupportModule
- ]
- }));
- var rootElement, shape, sibling;
- beforeEach(inject(function(canvas, elementFactory) {
- rootElement = elementFactory.createRoot({
- id: 'root'
- });
- canvas.setRootElement(rootElement);
- shape = canvas.addShape(elementFactory.createShape({
- id: 'shape1',
- x: 100, y: 100, width: 100, height: 100
- }));
- sibling = canvas.addShape(elementFactory.createShape({
- id: 'shape2',
- x: 400, y: 150, width: 50, height: 50
- }));
- }));
- it('getSiblings', inject(function(canvas, elementFactory, modeling, snapping) {
- // given
- var attacher = elementFactory.createShape({
- id: 'attacher',
- width: 50, height: 50
- });
- modeling.createShape(attacher, { x: 100, y: 100 }, shape, true);
- var label = elementFactory.createLabel({
- id: 'label',
- width: 80, height: 40
- });
- modeling.createLabel(shape, { x: 150, y: 250 }, label, rootElement);
- var connection = modeling.connect(shape, sibling);
- // when
- var siblings = snapping.getSiblings(shape, rootElement);
- // then
- expect(siblings).to.have.length(2);
- expect(siblings).to.contain(sibling);
- expect(siblings).to.contain(connection);
- }));
- });
- describe('integration', function() {
- beforeEach(bootstrapDiagram({
- modules: [
- createModule,
- snappingModule,
- modelingModule,
- moveModule
- ]
- }));
- beforeEach(inject(function(dragging, elementRegistry) {
- dragging.setOptions({ manual: true });
- }));
- var rootElement;
- beforeEach(inject(function(canvas, elementFactory) {
- rootElement = elementFactory.createRoot({
- id: 'root'
- });
- canvas.setRootElement(rootElement);
- canvas.addShape(elementFactory.createShape({
- id: 'snap-target',
- x: 100, y: 100, width: 100, height: 100
- }));
- }));
- it('should snap horizontal on create', inject(function(create, dragging, elementFactory, elementRegistry) {
- // given
- var newShape = elementFactory.createShape({
- id: 'new-shape',
- x: 0, y: 0,
- width: 100, height: 100
- });
- // when
- create.start(canvasEvent({ x: 50, y: 50 }), newShape);
- dragging.hover({ element: rootElement });
- dragging.move(canvasEvent({ x: 100, y: 350 }));
- dragging.move(canvasEvent({ x: 145, y: 350 }));
- dragging.end();
- var createdShape = elementRegistry.get('new-shape');
- // then
- expect(createdShape).to.have.bounds({
- x: 100, // snapped to mid(100, _)
- y: 300,
- width: 100,
- height: 100
- });
- }));
- it('should snap vertical on create', inject(function(create, dragging, elementFactory, elementRegistry) {
- // given
- var newShape = elementFactory.createShape({
- id: 'new-shape',
- x: 0, y: 0,
- width: 100, height: 100
- });
- // when
- create.start(canvasEvent({ x: 50, y: 50 }), newShape);
- dragging.hover({ element: rootElement });
- dragging.move(canvasEvent({ x: 100, y: 145 }));
- dragging.move(canvasEvent({ x: 350, y: 145 }));
- dragging.end();
- var createdShape = elementRegistry.get('new-shape');
- // then
- expect(createdShape).to.have.bounds({
- x: 300, // snapped to mid(100, _)
- y: 100,
- width: 100,
- height: 100
- });
- }));
- });
- });
|