AppendShapeSpec.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import {
  2. bootstrapDiagram,
  3. inject
  4. } from 'test/TestHelper';
  5. import {
  6. find
  7. } from 'min-dash';
  8. import modelingModule from 'lib/features/modeling';
  9. describe('features/modeling - append shape', function() {
  10. beforeEach(bootstrapDiagram({ modules: [ modelingModule ] }));
  11. var diagramRoot, childShape;
  12. beforeEach(inject(function(elementFactory, canvas) {
  13. diagramRoot = elementFactory.createRoot({
  14. id: 'root'
  15. });
  16. canvas.setRootElement(diagramRoot);
  17. childShape = elementFactory.createShape({
  18. id: 'child',
  19. x: 110, y: 110, width: 100, height: 100
  20. });
  21. canvas.addShape(childShape, diagramRoot);
  22. }));
  23. describe('basic handling', function() {
  24. var newShape;
  25. beforeEach(inject(function(modeling) {
  26. // add new shape
  27. newShape = modeling.appendShape(
  28. childShape,
  29. { id: 'appended', width: 50, height: 50 },
  30. { x: 200, y: 200 }
  31. );
  32. }));
  33. it('should return shape', inject(function() {
  34. // when
  35. // shape added
  36. // then
  37. expect(newShape).to.exist;
  38. }));
  39. it('should position new shape at mid', inject(function() {
  40. // when
  41. // shape added
  42. // then
  43. expect(newShape).to.exist;
  44. expect(newShape.x + newShape.width / 2).to.equal(200);
  45. expect(newShape.y + newShape.height / 2).to.equal(200);
  46. }));
  47. it('should render shape', inject(function(elementRegistry) {
  48. // when
  49. // shape added
  50. // then
  51. expect(elementRegistry.getGraphics(newShape)).to.exist;
  52. }));
  53. it('should add connection', inject(function(elementRegistry) {
  54. // when
  55. // shape added
  56. var connection = find(newShape.incoming, function(c) {
  57. return c.source === childShape;
  58. });
  59. // then
  60. expect(connection).to.exist;
  61. expect(connection.parent).to.equal(newShape.parent);
  62. expect(elementRegistry.getGraphics(connection)).to.exist;
  63. }));
  64. it('should undo', inject(function(commandStack, elementRegistry) {
  65. // given
  66. // shape added
  67. // when
  68. commandStack.undo();
  69. // then
  70. expect(newShape.parent).to.be.null;
  71. expect(elementRegistry.getGraphics(newShape)).to.be.undefined;
  72. }));
  73. });
  74. describe('customization', function() {
  75. it('should pass custom attributes connection', inject(function(modeling) {
  76. // given
  77. var connectionProperties = {
  78. custom: true
  79. };
  80. var shapeProps = {
  81. id: 'appended',
  82. width: 100,
  83. height: 100
  84. };
  85. var hints = {
  86. connection: connectionProperties
  87. };
  88. // when
  89. var newShape = modeling.appendShape(childShape, shapeProps, { x: 200, y: 200 }, null, hints);
  90. var newConnection = newShape.incoming[0];
  91. // then
  92. expect(newConnection.custom).to.be.true;
  93. }));
  94. });
  95. describe('connection create behavior', function() {
  96. it('should only create if not connected already', inject(function(modeling, eventBus) {
  97. // given
  98. eventBus.on('commandStack.shape.create.postExecute', function connectListener(event) {
  99. var context = event.context;
  100. // connect childShape -> shape
  101. modeling.connect(childShape, context.shape);
  102. });
  103. // when
  104. var newShape = modeling.appendShape(
  105. childShape,
  106. { id: 'appended', width: 100, height: 100 },
  107. { x: 500, y: 200 }
  108. );
  109. var incomingConnections = newShape.incoming;
  110. // then
  111. expect(incomingConnections.length).to.eql(1);
  112. }));
  113. });
  114. });