AlignElementsSpec.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import {
  2. bootstrapDiagram,
  3. inject
  4. } from 'test/TestHelper';
  5. import alignElementsModule from 'lib/features/align-elements';
  6. import modelingModule from 'lib/features/modeling';
  7. describe('features/align-elements', function() {
  8. beforeEach(bootstrapDiagram({
  9. modules: [ alignElementsModule, modelingModule ]
  10. }));
  11. describe('methods', function() {
  12. var elements = [
  13. { x: 50, y: 100, width: 100, height: 100 },
  14. { x: 200, y: 250, width: 100, height: 100 },
  15. { x: 400, y: 450, width: 300, height: 300 }
  16. ];
  17. describe('#_alignmentPosition', function() {
  18. function expectAlignmentPosition(type, result) {
  19. return function(alignElements) {
  20. // when
  21. var position = alignElements._alignmentPosition(type, elements);
  22. expect(position).to.eql(result);
  23. };
  24. }
  25. it('should get "left" position', inject(expectAlignmentPosition('left', { left: 50 })));
  26. it('should get "right" position', inject(expectAlignmentPosition('right', { right: 700 })));
  27. it('should get "center" position', inject(expectAlignmentPosition('center', { center: 375 })));
  28. it('should get "top" position', inject(expectAlignmentPosition('top', { top: 100 })));
  29. it('should get "bottom" position', inject(expectAlignmentPosition('bottom', { bottom: 750 })));
  30. it('should get "middle" position', inject(expectAlignmentPosition('middle', { middle: 425 })));
  31. });
  32. });
  33. describe('integration', function() {
  34. var rootShape, shape1, shape2, shape3, shapeBig, elements;
  35. beforeEach(inject(function(elementFactory, canvas) {
  36. rootShape = elementFactory.createRoot({
  37. id: 'root'
  38. });
  39. canvas.setRootElement(rootShape);
  40. shape1 = elementFactory.createShape({
  41. id: 's1',
  42. x: 50, y: 100, width: 100, height: 100
  43. });
  44. canvas.addShape(shape1, rootShape);
  45. shape2 = elementFactory.createShape({
  46. id: 's2',
  47. x: 200, y: 250, width: 100, height: 100
  48. });
  49. canvas.addShape(shape2, rootShape);
  50. shape3 = elementFactory.createShape({
  51. id: 's3',
  52. x: 800, y: 550, width: 100, height: 100
  53. });
  54. canvas.addShape(shape3, rootShape);
  55. shapeBig = elementFactory.createShape({
  56. id: 'sBig',
  57. x: 400, y: 450, width: 300, height: 300
  58. });
  59. canvas.addShape(shapeBig, rootShape);
  60. elements = [ shape1, shape2, shapeBig ];
  61. }));
  62. it('should align to the "left"', inject(function(alignElements) {
  63. // when
  64. alignElements.trigger(elements, 'left');
  65. // then
  66. expect(shape1.x).to.equal(50);
  67. expect(shape2.x).to.equal(50);
  68. expect(shapeBig.x).to.equal(50);
  69. }));
  70. it('should align to the "right"', inject(function(alignElements) {
  71. // when
  72. alignElements.trigger(elements, 'right');
  73. // then
  74. expect(shape1.x).to.equal(600);
  75. expect(shape2.x).to.equal(600);
  76. expect(shapeBig.x).to.equal(400);
  77. }));
  78. it('should align to the "center"', inject(function(alignElements) {
  79. // when
  80. alignElements.trigger(elements, 'center');
  81. // then
  82. expect(shape1.x).to.equal(325);
  83. expect(shape2.x).to.equal(325);
  84. expect(shapeBig.x).to.equal(225);
  85. }));
  86. it('should align to the "top"', inject(function(alignElements) {
  87. // when
  88. alignElements.trigger(elements, 'top');
  89. // then
  90. expect(shape1.y).to.equal(100);
  91. expect(shape2.y).to.equal(100);
  92. expect(shapeBig.y).to.equal(100);
  93. }));
  94. it('should align to the "bottom"', inject(function(alignElements) {
  95. // when
  96. alignElements.trigger(elements, 'bottom');
  97. // then
  98. expect(shape1.y).to.equal(650);
  99. expect(shape2.y).to.equal(650);
  100. expect(shapeBig.y).to.equal(450);
  101. }));
  102. it('should align to the "middle"', inject(function(alignElements) {
  103. // when
  104. alignElements.trigger(elements.concat(shape3), 'middle');
  105. // then
  106. expect(shape1.y).to.equal(550);
  107. expect(shape2.y).to.equal(550);
  108. expect(shape3.y).to.equal(550);
  109. expect(shapeBig.y).to.equal(450);
  110. }));
  111. });
  112. });