MoveSpec.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import {
  2. bootstrapDiagram,
  3. inject
  4. } from 'test/TestHelper';
  5. import {
  6. createCanvasEvent as canvasEvent
  7. } from '../../../util/MockEvents';
  8. import {
  9. assign,
  10. pick
  11. } from 'min-dash';
  12. import modelingModule from 'lib/features/modeling';
  13. import moveModule from 'lib/features/move';
  14. describe('features/move - Move', function() {
  15. beforeEach(bootstrapDiagram({ modules: [ moveModule, modelingModule ] }));
  16. beforeEach(inject(function(canvas, dragging) {
  17. dragging.setOptions({ manual: true });
  18. }));
  19. var rootShape, parentShape, childShape, childShape2, connection;
  20. beforeEach(inject(function(elementFactory, canvas) {
  21. rootShape = elementFactory.createRoot({
  22. id: 'root'
  23. });
  24. canvas.setRootElement(rootShape);
  25. parentShape = elementFactory.createShape({
  26. id: 'parent',
  27. x: 100, y: 100, width: 300, height: 300
  28. });
  29. canvas.addShape(parentShape, rootShape);
  30. childShape = elementFactory.createShape({
  31. id: 'child',
  32. x: 110, y: 110, width: 100, height: 100
  33. });
  34. canvas.addShape(childShape, parentShape);
  35. childShape2 = elementFactory.createShape({
  36. id: 'child2',
  37. x: 200, y: 110, width: 100, height: 100
  38. });
  39. canvas.addShape(childShape2, parentShape);
  40. connection = elementFactory.createConnection({
  41. id: 'connection',
  42. waypoints: [ { x: 150, y: 150 }, { x: 150, y: 200 }, { x: 350, y: 150 } ],
  43. source: childShape,
  44. target: childShape2
  45. });
  46. canvas.addConnection(connection, parentShape);
  47. }));
  48. describe('event centering', function() {
  49. it('should emit events relative to shape center', inject(function(eventBus, move, dragging) {
  50. // given
  51. function recordEvents(prefix) {
  52. var events = [];
  53. [ 'start', 'move', 'end', 'hover', 'out', 'cancel', 'cleanup', 'init' ].forEach(function(type) {
  54. eventBus.on(prefix + '.' + type, function(e) {
  55. events.push(assign({}, e));
  56. });
  57. });
  58. return events;
  59. }
  60. function position(e) {
  61. return pick(e, [ 'x', 'y', 'dx', 'dy' ]);
  62. }
  63. var events = recordEvents('shape.move');
  64. // when
  65. move.start(canvasEvent({ x: 0, y: 0 }), childShape);
  66. dragging.move(canvasEvent({ x: 20, y: 20 }));
  67. // then
  68. expect(events.map(position)).to.eql([
  69. { },
  70. { x: 160, y: 160, dx: 0, dy: 0 },
  71. { x: 180, y: 180, dx: 20, dy: 20 }
  72. ]);
  73. }));
  74. });
  75. describe('modeling', function() {
  76. it('should round movement to pixels', inject(function(move, dragging, elementRegistry) {
  77. // given
  78. move.start(canvasEvent({ x: 0, y: 0 }), childShape);
  79. // when
  80. dragging.move(canvasEvent({ x: 20, y: 20 }));
  81. dragging.hover({
  82. element: parentShape,
  83. gfx: elementRegistry.getGraphics(parentShape)
  84. });
  85. dragging.move(canvasEvent({ x: 30.4, y: 99.7 }));
  86. dragging.end();
  87. // then
  88. expect(childShape.x).to.eql(140);
  89. expect(childShape.y).to.eql(210);
  90. }));
  91. });
  92. });