ReplaceSpec.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. import {
  2. bootstrapDiagram,
  3. inject
  4. } from 'test/TestHelper';
  5. import modelingModule from 'lib/features/modeling';
  6. import replaceModule from 'lib/features/replace';
  7. import {
  8. query as domQuery
  9. } from 'min-dom';
  10. describe('features/replace', function() {
  11. beforeEach(bootstrapDiagram({
  12. modules: [
  13. modelingModule,
  14. replaceModule
  15. ]
  16. }));
  17. var rootShape, parentShape, originalShape;
  18. beforeEach(inject(function(elementFactory, canvas) {
  19. rootShape = elementFactory.createRoot({
  20. id: 'root'
  21. });
  22. canvas.setRootElement(rootShape);
  23. parentShape = elementFactory.createShape({
  24. id: 'parent',
  25. x: 100, y: 100, width: 300, height: 300
  26. });
  27. canvas.addShape(parentShape, rootShape);
  28. originalShape = elementFactory.createShape({
  29. id: 'originalShape',
  30. x: 110, y: 110, width: 100, height: 100
  31. });
  32. canvas.addShape(originalShape, parentShape);
  33. }));
  34. describe('#replaceElement', function() {
  35. it('should add new shape', inject(function(elementRegistry, replace) {
  36. // given
  37. var replacement = {
  38. id: 'replacement',
  39. width: 200,
  40. height: 200
  41. };
  42. // when
  43. var newShape = replace.replaceElement(originalShape, replacement);
  44. // then
  45. expect(newShape).to.exist;
  46. // expect added
  47. expect(elementRegistry.get('replacement')).to.equal(newShape);
  48. }));
  49. it('should define custom attributes on new shape', inject(function(replace) {
  50. // given
  51. var replacement = {
  52. id: 'replacement',
  53. width: 200,
  54. height: 200,
  55. customArray: ['FOO', 'BAR'],
  56. customString: 'foobar'
  57. };
  58. // when
  59. var newShape = replace.replaceElement(originalShape, replacement);
  60. // then
  61. expect(newShape.customArray).to.equal(replacement.customArray);
  62. expect(newShape.customString).to.equal(replacement.customString);
  63. }));
  64. it('should delete old shape', inject(function(elementFactory, replace, elementRegistry) {
  65. // given
  66. var replacement = {
  67. id: 'replacement',
  68. width: 200,
  69. height: 200
  70. };
  71. // shape replacement
  72. replace.replaceElement(originalShape, replacement);
  73. // then
  74. expect(originalShape.parent).to.be.null;
  75. }));
  76. it('should return new shape', inject(function(elementRegistry, replace) {
  77. // given
  78. var replacement = {
  79. id: 'replacement',
  80. width: 200,
  81. height: 200
  82. };
  83. // shape replacement
  84. var newShape = replace.replaceElement(originalShape, replacement);
  85. // then
  86. expect(newShape).to.exist;
  87. expect(newShape.id).to.equal('replacement');
  88. }));
  89. it('should add correct attributes to new shape', inject(function(elementFactory, replace, elementRegistry) {
  90. // given
  91. var replacement = {
  92. id: 'replacement',
  93. width: 200,
  94. height: 200
  95. };
  96. // shape replacement
  97. replace.replaceElement(originalShape, replacement);
  98. // then
  99. var replacementShape = elementRegistry.get('replacement');
  100. expect(replacementShape.x).to.equal(110);
  101. expect(replacementShape.y).to.equal(110);
  102. expect(replacementShape.width).to.equal(200);
  103. expect(replacementShape.height).to.equal(200);
  104. }));
  105. it('should retain position when setting odd height', inject(function(elementFactory, replace, elementRegistry) {
  106. // given
  107. var replacement = {
  108. id: 'replacement',
  109. width: 200,
  110. height: 201
  111. };
  112. // shape replacement
  113. replace.replaceElement(originalShape, replacement);
  114. // then
  115. var replacementShape = elementRegistry.get('replacement');
  116. expect(replacementShape.x).to.equal(110);
  117. expect(replacementShape.y).to.equal(110);
  118. expect(replacementShape.width).to.equal(200);
  119. expect(replacementShape.height).to.equal(201);
  120. }));
  121. it('should retain position when setting odd width', inject(function(elementFactory, replace, elementRegistry) {
  122. // given
  123. var replacement = {
  124. id: 'replacement',
  125. width: 201,
  126. height: 200
  127. };
  128. // shape replacement
  129. replace.replaceElement(originalShape, replacement);
  130. // then
  131. var replacementShape = elementRegistry.get('replacement');
  132. expect(replacementShape.x).to.equal(110);
  133. expect(replacementShape.y).to.equal(110);
  134. expect(replacementShape.width).to.equal(201);
  135. expect(replacementShape.height).to.equal(200);
  136. }));
  137. it('should retain position when setting odd width and height', inject(function(elementFactory, replace, elementRegistry) {
  138. // given
  139. var replacement = {
  140. id: 'replacement',
  141. width: 201,
  142. height: 201
  143. };
  144. // shape replacement
  145. replace.replaceElement(originalShape, replacement);
  146. // then
  147. var replacementShape = elementRegistry.get('replacement');
  148. expect(replacementShape.x).to.equal(110);
  149. expect(replacementShape.y).to.equal(110);
  150. expect(replacementShape.width).to.equal(201);
  151. expect(replacementShape.height).to.equal(201);
  152. }));
  153. });
  154. describe('reconnect', function() {
  155. var sourceShape,
  156. targetShape,
  157. connection;
  158. beforeEach(inject(function(elementFactory, canvas, modeling) {
  159. sourceShape = originalShape;
  160. targetShape = elementFactory.createShape({
  161. id: 'targetShape',
  162. x: 290, y: 110, width: 100, height: 100
  163. });
  164. canvas.addShape(targetShape, parentShape);
  165. connection = modeling.createConnection(sourceShape, targetShape, {
  166. id: 'connection',
  167. waypoints: [ { x: 210, y: 160 }, { x: 290, y: 160 } ]
  168. }, parentShape);
  169. // canvas.addConnection(connection);
  170. }));
  171. it('should reconnect start', inject(function(elementFactory, replace, elementRegistry) {
  172. // given
  173. var replacement = {
  174. id: 'replacement',
  175. width: 120,
  176. height: 120
  177. };
  178. // when
  179. var replacedShape = replace.replaceElement(sourceShape, replacement);
  180. // then
  181. expect(replacedShape.outgoing[0]).to.exist;
  182. }));
  183. it('should reconnect end', inject(function(elementFactory, replace, elementRegistry) {
  184. // given
  185. var replacement = {
  186. id: 'replacement',
  187. width: 80,
  188. height: 80
  189. };
  190. // when
  191. var replacedShape = replace.replaceElement(targetShape, replacement);
  192. // then
  193. expect(replacedShape.incoming[0]).to.exist;
  194. }));
  195. it('should adopt children', inject(function(elementFactory, replace, elementRegistry, eventBus) {
  196. // given
  197. var replacement = {
  198. id: 'replacement',
  199. width: 300,
  200. height: 300
  201. };
  202. // when
  203. var newShape = replace.replaceElement(parentShape, replacement);
  204. // then
  205. expect(newShape.children).to.contain(originalShape);
  206. expect(newShape.children).to.contain(connection);
  207. expect(newShape.children).to.contain(targetShape);
  208. expect(originalShape.parent).to.eql(newShape);
  209. expect(connection.parent).to.eql(newShape);
  210. expect(targetShape.parent).to.eql(newShape);
  211. expect(originalShape.outgoing).to.contain(connection);
  212. expect(targetShape.incoming).to.contain(connection);
  213. }));
  214. it('should adopt children and show them in the DOM',
  215. inject(function(canvas, elementFactory, replace, elementRegistry) {
  216. // given
  217. var replacement = {
  218. id: 'replacement',
  219. width: 300,
  220. height: 300
  221. };
  222. // when
  223. replace.replaceElement(parentShape, replacement);
  224. var newShapeContainer = domQuery('[data-element-id="replacement"]', canvas.getContainer());
  225. // then
  226. expect(domQuery('[data-element-id="originalShape"]', newShapeContainer.parentNode)).to.exist;
  227. expect(domQuery('[data-element-id="targetShape"]', newShapeContainer.parentNode)).to.exist;
  228. })
  229. );
  230. it('should retain moved children in command context', inject(function(replace, eventBus) {
  231. // given
  232. var replacement = {
  233. id: 'replacement',
  234. width: 300,
  235. height: 300
  236. };
  237. eventBus.on('commandStack.elements.move.postExecuted', function(event) {
  238. // then
  239. var shapes = event.context.shapes;
  240. expect(shapes).not.to.be.empty;
  241. expect(shapes).to.have.length(3);
  242. });
  243. // when
  244. replace.replaceElement(parentShape, replacement);
  245. }));
  246. });
  247. describe('undo/redo support', function() {
  248. it('should undo replace', inject(function(elementFactory, replace, elementRegistry, commandStack) {
  249. // given
  250. var replacement = {
  251. id: 'replacement',
  252. width: 200,
  253. height: 200
  254. };
  255. replace.replaceElement(originalShape, replacement);
  256. // when
  257. commandStack.undo();
  258. // then
  259. var shape = elementRegistry.get('originalShape');
  260. expect(shape.width).to.equal(100);
  261. }));
  262. it('should redo', inject(function(elementFactory, replace, elementRegistry, commandStack) {
  263. // given
  264. var replacement = {
  265. id: 'replacement',
  266. width: 200,
  267. height: 200
  268. };
  269. replace.replaceElement(originalShape, replacement);
  270. var replacementShape = elementRegistry.get('replacement');
  271. var replacement2 = {
  272. id: 'replacement2',
  273. width: 280,
  274. height: 280
  275. };
  276. replace.replaceElement(replacementShape, replacement2);
  277. // when
  278. commandStack.undo();
  279. commandStack.undo();
  280. commandStack.redo();
  281. commandStack.redo();
  282. // then
  283. var redoShape = elementRegistry.get('replacement2');
  284. expect(redoShape.width).to.equal(280);
  285. }));
  286. });
  287. });