LayoutUtilSpec.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import {
  2. getOrientation,
  3. getMid
  4. } from 'lib/layout/LayoutUtil';
  5. function rect(x, y, width, height) {
  6. return { x: x, y: y, width: width, height: height };
  7. }
  8. describe('layout/LayoutUtil', function() {
  9. describe('#getMid', function() {
  10. it('should return rectangle mid point', function() {
  11. // given
  12. var r = rect(100, 100, 100, 200);
  13. // then
  14. expect(getMid(r)).to.eql({ x: 150, y: 200 });
  15. });
  16. it('should return point mid point', function() {
  17. // given
  18. var point = { x: 100, y: 100 };
  19. // then
  20. expect(getMid(point)).to.eql(point);
  21. });
  22. });
  23. describe('#getOrientation', function() {
  24. // a rectangle 100,100 -> 200,200
  25. var a = rect(100, 100, 100, 100);
  26. it('should detect top', function() {
  27. // given
  28. var b = rect(100, 0, 100, 100);
  29. expect(getOrientation(b, a)).to.equal('top');
  30. });
  31. it('should detect top-right', function() {
  32. // given
  33. var b = rect(200, 0, 100, 100);
  34. expect(getOrientation(b, a)).to.equal('top-right');
  35. });
  36. it('should detect right', function() {
  37. // given
  38. var b = rect(200, 100, 100, 100);
  39. expect(getOrientation(b, a)).to.equal('right');
  40. });
  41. it('should detect bottom-right', function() {
  42. // given
  43. var b = rect(200, 200, 100, 100);
  44. expect(getOrientation(b, a)).to.equal('bottom-right');
  45. });
  46. it('should detect bottom', function() {
  47. // given
  48. var b = rect(100, 200, 100, 100);
  49. expect(getOrientation(b, a)).to.equal('bottom');
  50. });
  51. it('should detect bottom-left', function() {
  52. // given
  53. var b = rect(0, 200, 100, 100);
  54. expect(getOrientation(b, a)).to.equal('bottom-left');
  55. });
  56. it('should detect left', function() {
  57. // given
  58. var b = rect(0, 100, 100, 100);
  59. expect(getOrientation(b, a)).to.equal('left');
  60. });
  61. it('should detect top-left', function() {
  62. // given
  63. var b = rect(0, 0, 100, 100);
  64. expect(getOrientation(b, a)).to.equal('top-left');
  65. });
  66. it('should detect intersect', function() {
  67. // given
  68. var b = rect(120, 120, 100, 100);
  69. expect(getOrientation(b, a)).to.equal('intersect');
  70. });
  71. });
  72. });