ModelSpec.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import {
  2. create,
  3. Connection,
  4. Shape
  5. } from 'lib/model';
  6. describe('model', function() {
  7. it('should instantiate connection', function() {
  8. // given
  9. var waypoints = [ { x: 0, y: 0 }, { x: 100, y: 100 } ];
  10. // when
  11. var connection = create('connection', {
  12. waypoints: waypoints
  13. });
  14. // then
  15. expect(connection.waypoints).to.equal(waypoints);
  16. expect(connection instanceof Connection).to.equal(true);
  17. });
  18. it('should instantiate shape', function() {
  19. // given
  20. var x = 10, y = 20, width = 100, height = 100;
  21. // when
  22. var shape = create('shape', {
  23. x: x,
  24. y: y,
  25. width: width,
  26. height: height
  27. });
  28. // then
  29. expect(shape.x).to.equal(x);
  30. expect(shape.y).to.equal(y);
  31. expect(shape.width).to.equal(width);
  32. expect(shape.height).to.equal(height);
  33. expect(shape instanceof Shape).to.equal(true);
  34. });
  35. it('should wire relationships', function() {
  36. // when
  37. var parentShape = create('shape');
  38. var shape1 = create('shape', { parent: parentShape });
  39. var shape2 = create('shape', { parent: parentShape });
  40. var shape1Label = create('label', { parent: parentShape, labelTarget: shape1 });
  41. var connection = create('connection', { parent: parentShape, source: shape1, target: shape2 });
  42. var connectionLabel = create('label', { parent: parentShape, labelTarget: connection });
  43. // then
  44. // expect parent to be wired
  45. expect(parentShape.children).to.contain(shape1);
  46. expect(parentShape.children).to.contain(shape2);
  47. expect(parentShape.children).to.contain(shape1Label);
  48. expect(parentShape.children).to.contain(connection);
  49. expect(parentShape.children).to.contain(connectionLabel);
  50. // expect labels to be wired
  51. expect(shape1.label).to.equal(shape1Label);
  52. expect(connection.label).to.equal(connectionLabel);
  53. // expect outgoing / incoming to be wired
  54. expect(shape1.outgoing).to.contain(connection);
  55. expect(shape2.incoming).to.contain(connection);
  56. });
  57. describe('labels', function() {
  58. it('should set labelTarget', function() {
  59. // given
  60. var shape = create('shape');
  61. // when
  62. var label = create('label', { labelTarget: shape });
  63. // then
  64. expect(shape.label).to.equal(label);
  65. expect(shape.labels).to.eql([ label ]);
  66. });
  67. it('should set label', function() {
  68. // when
  69. var label = create('label');
  70. // when
  71. var shape = create('shape', { label: label });
  72. // then
  73. expect(shape.labels).to.eql([ label ]);
  74. expect(label.labelTarget).to.equal(shape);
  75. });
  76. it('should unset labelTarget', function() {
  77. // given
  78. var shape = create('shape');
  79. var label = create('label', { labelTarget: shape });
  80. // when
  81. label.labelTarget = null;
  82. // then
  83. expect(shape.label).not.to.exist;
  84. expect(shape.labels).to.be.empty;
  85. });
  86. it('should unset label', function() {
  87. // given
  88. var shape = create('shape');
  89. var label = create('label', { labelTarget: shape });
  90. // when
  91. shape.label = null;
  92. // then
  93. expect(shape.labels).to.eql([ ]);
  94. expect(label.labelTarget).not.to.exist;
  95. });
  96. it('should wire multi label to relationship', function() {
  97. // when
  98. var parentShape = create('shape');
  99. var shape1 = create('shape', { parent: parentShape });
  100. var shape2 = create('shape', { parent: parentShape });
  101. var connection = create('connection', {
  102. parent: parentShape,
  103. source: shape1,
  104. target: shape2
  105. });
  106. var primaryLabel = create('label', {
  107. parent: parentShape
  108. });
  109. var label2 = create('label', {
  110. parent: parentShape,
  111. labelTarget: connection
  112. });
  113. label2.labelTarget = null;
  114. var label3 = create('label', {
  115. parent: parentShape,
  116. labelTarget: connection
  117. });
  118. connection.label = primaryLabel;
  119. // then
  120. // expect labels to be wired
  121. expect(connection.labels).to.eql([
  122. primaryLabel,
  123. label3
  124. ]);
  125. });
  126. });
  127. });