SelectionSpec.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* global sinon */
  2. import {
  3. bootstrapDiagram,
  4. inject
  5. } from 'test/TestHelper';
  6. import coreModule from 'lib/core';
  7. import draggingModule from 'lib/features/dragging';
  8. import modelingModule from 'lib/features/modeling';
  9. import moveModule from 'lib/features/move';
  10. import selectionModule from 'lib/features/selection';
  11. import {
  12. createCanvasEvent as canvasEvent
  13. } from '../../../util/MockEvents';
  14. describe('features/selection/Selections', function() {
  15. beforeEach(bootstrapDiagram({
  16. modules: [
  17. coreModule,
  18. draggingModule,
  19. modelingModule,
  20. moveModule,
  21. selectionModule
  22. ]
  23. }));
  24. var shape1, shape2, connection1;
  25. beforeEach(inject(function(canvas) {
  26. // given
  27. shape1 = canvas.addShape({
  28. id: 'shape1',
  29. x: 10,
  30. y: 10,
  31. width: 100,
  32. height: 100
  33. });
  34. shape2 = canvas.addShape({
  35. id: 'shape2',
  36. x: 150,
  37. y: 10,
  38. width: 100,
  39. height: 100
  40. });
  41. connection1 = canvas.addConnection({
  42. id: 'connection1',
  43. source: 'shape1',
  44. target: 'shape2',
  45. waypoints: [ { x: 110, y: 60 }, { x: 150, y: 60 } ]
  46. });
  47. }));
  48. describe('bootstrap', function() {
  49. it('should bootstrap diagram with component', inject(function(selection) {
  50. expect(selection).to.exist;
  51. }));
  52. });
  53. describe('#select', function() {
  54. it('should add shape to selection', inject(function(selection) {
  55. // when
  56. selection.select(shape1);
  57. // then
  58. var selectedElements = selection.get();
  59. expect(selectedElements[0]).to.equal(shape1);
  60. }));
  61. it('should add connection to selection', inject(function(selection) {
  62. // when
  63. selection.select(connection1);
  64. // then
  65. var selectedElements = selection.get();
  66. expect(selectedElements[0]).to.equal(connection1);
  67. }));
  68. it('should add multiple elements to selection', inject(function(selection) {
  69. // when
  70. selection.select(shape2);
  71. selection.select(connection1, true);
  72. // then
  73. var selectedElements = selection.get();
  74. expect(selectedElements[0]).to.equal(shape2);
  75. expect(selectedElements[1]).to.equal(connection1);
  76. }));
  77. it('should select moved element if previously not in selection',
  78. inject(function(dragging, elementRegistry, modeling, move, selection) {
  79. // given
  80. selection.select(shape1);
  81. // when
  82. move.start(canvasEvent({
  83. x: shape2.x + 10 + shape2.width / 2,
  84. y: shape2.y + 30 + shape2.height/2
  85. }), shape2);
  86. dragging.hover({
  87. element: shape2,
  88. gfx: elementRegistry.getGraphics(shape2)
  89. });
  90. dragging.move(canvasEvent({ x: 300, y: 300 }));
  91. dragging.end();
  92. // then
  93. var selectedElements = selection.get();
  94. expect(selectedElements[0]).to.equal(shape2);
  95. expect(selectedElements.length).to.equal(1);
  96. })
  97. );
  98. });
  99. describe('#deselect', function() {
  100. it('should remove shape from selection', inject(function(selection) {
  101. selection.select(shape2);
  102. selection.select(connection1, true);
  103. selection.deselect(shape2);
  104. // then
  105. var selectedElements = selection.get();
  106. expect(selectedElements[0]).to.equal(connection1);
  107. expect(selectedElements.length).to.equal(1);
  108. }));
  109. it('should remove all elements from selection', inject(function(selection) {
  110. selection.select(shape2);
  111. selection.select(connection1, true);
  112. selection.select();
  113. // then
  114. var selectedElements = selection.get();
  115. expect(selectedElements.length).to.equal(0);
  116. }));
  117. it('should not fail on empty selection', inject(function(selection) {
  118. selection.select();
  119. var selectedElements = selection.get();
  120. // then
  121. expect(selectedElements.length).to.equal(0);
  122. }));
  123. });
  124. describe('clear', function() {
  125. it('should remove selection', inject(function(selection, eventBus) {
  126. // given
  127. selection.select(shape1);
  128. var changedSpy = sinon.spy(function() {});
  129. eventBus.on('selection.changed', changedSpy);
  130. // when
  131. eventBus.fire('diagram.clear');
  132. // then
  133. expect(selection._selectedElements).to.be.empty;
  134. expect(changedSpy).to.have.been.called;
  135. }));
  136. });
  137. });