OverlaysIntegrationSpec.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. import {
  2. bootstrapDiagram,
  3. inject
  4. } from 'test/TestHelper';
  5. import {
  6. createCanvasEvent as canvasEvent
  7. } from '../../../util/MockEvents';
  8. import {
  9. classes as domClasses,
  10. query as domQuery
  11. } from 'min-dom';
  12. import $ from 'jquery';
  13. import overlayModule from 'lib/features/overlays';
  14. import selectionModule from 'lib/features/selection';
  15. import modelingModule from 'lib/features/modeling';
  16. import resizeModule from 'lib/features/resize';
  17. import moveModule from 'lib/features/move';
  18. import {
  19. resizeBounds
  20. } from 'lib/features/resize/ResizeUtil';
  21. describe('features/overlay - integration', function() {
  22. beforeEach(bootstrapDiagram({
  23. modules: [
  24. overlayModule,
  25. selectionModule,
  26. modelingModule,
  27. moveModule,
  28. resizeModule
  29. ],
  30. canvas: { deferUpdate: false }
  31. }));
  32. beforeEach(inject(function(dragging) {
  33. dragging.setOptions({ manual: true });
  34. }));
  35. describe('modeling integration', function() {
  36. it('should update on shape.move', inject(function(modeling, canvas, overlays) {
  37. // given
  38. var shape = canvas.addShape({
  39. id: 'test',
  40. x: 50,
  41. y: 50,
  42. width: 100,
  43. height: 100
  44. });
  45. // add overlay to a single shape (or connection)
  46. overlays.add(shape, {
  47. html: '<div style="width: 40px; height: 40px">TEST<br/>TEST</div>',
  48. position: {
  49. top: 0,
  50. left: 0
  51. }
  52. });
  53. // when
  54. modeling.moveShape(shape, { x: -20, y: +20 });
  55. // then
  56. var html = domQuery('[data-container-id=test]', canvas.getContainer());
  57. expect(parseInt(html.style.top)).to.equal(70);
  58. expect(parseInt(html.style.left)).to.equal(30);
  59. }));
  60. it('should update on shape.move undo', inject(function(modeling, canvas, commandStack, overlays) {
  61. // given
  62. var shape = canvas.addShape({
  63. id: 'test',
  64. x: 50,
  65. y: 50,
  66. width: 100,
  67. height: 100
  68. });
  69. // add overlay to a single shape (or connection)
  70. overlays.add(shape, {
  71. html: '<div style="width: 40px; height: 40px">TEST<br/>TEST</div>',
  72. position: {
  73. top: 0,
  74. left: 0
  75. }
  76. });
  77. // when
  78. modeling.moveShape(shape, { x: -20, y: +20 });
  79. commandStack.undo();
  80. // then
  81. var html = domQuery('[data-container-id=test]', canvas.getContainer());
  82. expect(parseInt(html.style.top)).to.equal(50);
  83. expect(parseInt(html.style.left)).to.equal(50);
  84. }));
  85. it('should update on shape.resize', inject(function(modeling, canvas, overlays) {
  86. // given
  87. var shape = canvas.addShape({
  88. id: 'test',
  89. x: 50,
  90. y: 50,
  91. width: 100,
  92. height: 100
  93. });
  94. // add overlay to a single shape (or connection)
  95. overlays.add(shape, {
  96. html: '<div style="width: 40px; height: 40px;">TEST<br/>TEST</div>',
  97. position: {
  98. top: 10,
  99. left: 10
  100. }
  101. });
  102. // when
  103. modeling.resizeShape(shape, resizeBounds(shape, 'nw', { x: 5, y: -15 }));
  104. // then
  105. var html = domQuery('[data-container-id=test]', canvas.getContainer());
  106. expect(parseInt(html.style.top)).to.equal(35);
  107. expect(parseInt(html.style.left)).to.equal(55);
  108. }));
  109. it('should update on shape.resize undo', inject(function(modeling, canvas, overlays, commandStack, resize) {
  110. // given
  111. var shape = canvas.addShape({
  112. id: 'test',
  113. x: 50,
  114. y: 50,
  115. width: 100,
  116. height: 100
  117. });
  118. // add overlay to a single shape (or connection)
  119. overlays.add(shape, {
  120. html: '<div style="width: 40px; height: 40px;">TEST<br/>TEST</div>',
  121. position: {
  122. top: 10,
  123. left: 10
  124. }
  125. });
  126. modeling.resizeShape(shape, resizeBounds(shape, 'nw', { x: 5, y: -15 }));
  127. // when
  128. commandStack.undo();
  129. // then
  130. var html = domQuery('[data-container-id=test]', canvas.getContainer());
  131. expect(parseInt(html.style.top)).to.equal(50);
  132. expect(parseInt(html.style.left)).to.equal(50);
  133. }));
  134. });
  135. describe('selection/hover integration', function() {
  136. it('should add selection/hover markers', inject(function(selection, canvas, overlays) {
  137. // given
  138. var shape = canvas.addShape({
  139. id: 'shape',
  140. x: 50,
  141. y: 50,
  142. width: 100,
  143. height: 100
  144. });
  145. var overlayContainer = overlays._getOverlayContainer(shape);
  146. // when
  147. selection.select(shape);
  148. // then
  149. expect(domClasses(overlayContainer.html).has('selected')).to.be.true;
  150. }));
  151. it('should remove selection/hover markers', inject(function(selection, canvas, overlays) {
  152. // given
  153. var shape = canvas.addShape({
  154. id: 'shape',
  155. x: 50,
  156. y: 50,
  157. width: 100,
  158. height: 100
  159. });
  160. var overlayContainer = overlays._getOverlayContainer(shape);
  161. // when
  162. selection.select(shape);
  163. selection.select(null);
  164. // then
  165. expect(domClasses(overlayContainer.html).has('selected')).to.be.false;
  166. }));
  167. });
  168. describe('drag integration', function() {
  169. it('should add <djs-dragging> marker class', inject(function(canvas, move, dragging, overlays) {
  170. // given
  171. var parent = canvas.addShape({
  172. id: 'parent',
  173. x: 50,
  174. y: 50,
  175. width: 100,
  176. height: 100,
  177. children: []
  178. });
  179. var child = canvas.addShape({
  180. id: 'child',
  181. x: 50,
  182. y: 50,
  183. width: 100,
  184. height: 100
  185. }, parent);
  186. var parentOverlayContainer = overlays._getOverlayContainer(parent);
  187. var childOverlayContainer = overlays._getOverlayContainer(child);
  188. // when
  189. move.start(canvasEvent({ x: 10, y: 10 }), parent);
  190. dragging.move(canvasEvent({ x: 20, y: 30 }));
  191. // then
  192. expect(domClasses(parentOverlayContainer.html).has('djs-dragging')).to.be.true;
  193. expect(domClasses(childOverlayContainer.html).has('djs-dragging')).to.be.true;
  194. }));
  195. it('should remove <djs-dragging> marker class on end', inject(function(canvas, move, dragging, overlays) {
  196. // given
  197. var parent = canvas.addShape({
  198. id: 'parent',
  199. x: 50,
  200. y: 50,
  201. width: 100,
  202. height: 100,
  203. children: []
  204. });
  205. var child = canvas.addShape({
  206. id: 'child',
  207. x: 50,
  208. y: 50,
  209. width: 100,
  210. height: 100
  211. }, parent);
  212. var parentOverlayContainer = overlays._getOverlayContainer(parent);
  213. var childOverlayContainer = overlays._getOverlayContainer(child);
  214. // when
  215. move.start(canvasEvent({ x: 10, y: 10 }), parent);
  216. dragging.move(canvasEvent({ x: 30, y: 30 }));
  217. dragging.end();
  218. // then
  219. expect(domClasses(parentOverlayContainer.html).has('djs-dragging')).to.be.false;
  220. expect(domClasses(childOverlayContainer.html).has('djs-dragging')).to.be.false;
  221. }));
  222. });
  223. describe('jquery support', function() {
  224. it('should allow to pass jquery element as overlay', inject(function(canvas, overlays) {
  225. // given
  226. window.jQuery = $;
  227. var shape = canvas.addShape({
  228. id: 'test',
  229. x: 50,
  230. y: 50,
  231. width: 100,
  232. height: 100,
  233. children: []
  234. });
  235. var $element = $('<div id="FOO">');
  236. // add overlay to a single shape (or connection)
  237. overlays.add(shape, {
  238. html: $element,
  239. position: {
  240. top: 10,
  241. left: 10
  242. }
  243. });
  244. // then
  245. expect($element.parent().length).to.equal(1);
  246. }));
  247. });
  248. });