rectangle.test.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const __1 = require("..");
  4. describe('rectangle', () => {
  5. describe('#constructor', () => {
  6. it('should create a rectangle instance', () => {
  7. expect(new __1.Rectangle()).toBeInstanceOf(__1.Rectangle);
  8. expect(new __1.Rectangle(1)).toBeInstanceOf(__1.Rectangle);
  9. expect(new __1.Rectangle(1, 2)).toBeInstanceOf(__1.Rectangle);
  10. expect(new __1.Rectangle(1, 2).x).toEqual(1);
  11. expect(new __1.Rectangle(1, 2).y).toEqual(2);
  12. expect(new __1.Rectangle(1, 2).width).toEqual(0);
  13. expect(new __1.Rectangle(1, 2).height).toEqual(0);
  14. expect(new __1.Rectangle().equals(new __1.Rectangle(0, 0, 0, 0)));
  15. });
  16. it('should work with key points', () => {
  17. const rect = new __1.Rectangle(1, 2, 3, 4);
  18. expect(rect.origin.equals({ x: 1, y: 2 })).toBeTruthy();
  19. expect(rect.topLeft.equals({ x: 1, y: 2 })).toBeTruthy();
  20. expect(rect.topCenter.equals({ x: 2.5, y: 2 })).toBeTruthy();
  21. expect(rect.topRight.equals({ x: 4, y: 2 })).toBeTruthy();
  22. expect(rect.center.equals({ x: 2.5, y: 4 })).toBeTruthy();
  23. expect(rect.bottomLeft.equals({ x: 1, y: 6 })).toBeTruthy();
  24. expect(rect.bottomCenter.equals({ x: 2.5, y: 6 })).toBeTruthy();
  25. expect(rect.bottomRight.equals({ x: 4, y: 6 })).toBeTruthy();
  26. expect(rect.corner.equals({ x: 4, y: 6 })).toBeTruthy();
  27. expect(rect.leftMiddle.equals({ x: 1, y: 4 })).toBeTruthy();
  28. expect(rect.rightMiddle.equals({ x: 4, y: 4 })).toBeTruthy();
  29. });
  30. it('should return the key points', () => {
  31. const rect = new __1.Rectangle(1, 2, 3, 4);
  32. expect(rect.getOrigin().equals({ x: 1, y: 2 })).toBeTruthy();
  33. expect(rect.getTopLeft().equals({ x: 1, y: 2 })).toBeTruthy();
  34. expect(rect.getTopCenter().equals({ x: 2.5, y: 2 })).toBeTruthy();
  35. expect(rect.getTopRight().equals({ x: 4, y: 2 })).toBeTruthy();
  36. expect(rect.getCenter().equals({ x: 2.5, y: 4 })).toBeTruthy();
  37. expect(rect.getBottomLeft().equals({ x: 1, y: 6 })).toBeTruthy();
  38. expect(rect.getBottomCenter().equals({ x: 2.5, y: 6 })).toBeTruthy();
  39. expect(rect.getBottomRight().equals({ x: 4, y: 6 })).toBeTruthy();
  40. expect(rect.getCorner().equals({ x: 4, y: 6 })).toBeTruthy();
  41. expect(rect.getLeftMiddle().equals({ x: 1, y: 4 })).toBeTruthy();
  42. expect(rect.getRightMiddle().equals({ x: 4, y: 4 })).toBeTruthy();
  43. expect(rect.getCenterX()).toEqual(2.5);
  44. expect(rect.getCenterY()).toEqual(4);
  45. });
  46. it('should work with key lines', () => {
  47. const rect = new __1.Rectangle(1, 2, 3, 4);
  48. expect(rect.topLine.equals(new __1.Line(1, 2, 4, 2)));
  49. expect(rect.rightLine.equals(new __1.Line(4, 2, 4, 6)));
  50. expect(rect.bottomLine.equals(new __1.Line(1, 6, 4, 6)));
  51. expect(rect.leftLine.equals(new __1.Line(1, 2, 1, 6)));
  52. });
  53. it('should return the key lines', () => {
  54. const rect = new __1.Rectangle(1, 2, 3, 4);
  55. expect(rect.getTopLine().equals(new __1.Line(1, 2, 4, 2)));
  56. expect(rect.getRightLine().equals(new __1.Line(4, 2, 4, 6)));
  57. expect(rect.getBottomLine().equals(new __1.Line(1, 6, 4, 6)));
  58. expect(rect.getLeftLine().equals(new __1.Line(1, 2, 1, 6)));
  59. });
  60. });
  61. describe('#Rectangle.clone', () => {
  62. it('should clone rectangle', () => {
  63. const obj = { x: 1, y: 2, width: 3, height: 4 };
  64. expect(__1.Rectangle.clone(new __1.Rectangle(1, 2, 3, 4)).toJSON()).toEqual(obj);
  65. expect(__1.Rectangle.clone(obj).toJSON()).toEqual(obj);
  66. expect(__1.Rectangle.clone([1, 2, 3, 4]).toJSON()).toEqual(obj);
  67. });
  68. });
  69. describe('#Rectangle.isRectangleLike', () => {
  70. it('should return true if the given object is a rectangle-like object', () => {
  71. const obj = { x: 1, y: 2, width: 3, height: 4 };
  72. expect(__1.Rectangle.isRectangleLike(obj)).toBeTruthy();
  73. expect(__1.Rectangle.isRectangleLike(Object.assign(Object.assign({}, obj), { z: 10 }))).toBeTruthy();
  74. expect(__1.Rectangle.isRectangleLike(Object.assign(Object.assign({}, obj), { z: 10, s: 's' }))).toBeTruthy();
  75. });
  76. it('should return false if the given object is a rectangle-like object', () => {
  77. expect(__1.Rectangle.isRectangleLike({ x: 1 })).toBeFalsy();
  78. expect(__1.Rectangle.isRectangleLike({ y: 2 })).toBeFalsy();
  79. expect(__1.Rectangle.isRectangleLike({})).toBeFalsy();
  80. expect(__1.Rectangle.isRectangleLike(null)).toBeFalsy();
  81. expect(__1.Rectangle.isRectangleLike(false)).toBeFalsy();
  82. expect(__1.Rectangle.isRectangleLike(1)).toBeFalsy();
  83. expect(__1.Rectangle.isRectangleLike('s')).toBeFalsy();
  84. });
  85. });
  86. describe('#Rectangle.fromSize', () => {
  87. it('should create a rectangle from the given size', () => {
  88. expect(__1.Rectangle.fromSize({ width: 10, height: 8 }).toJSON()).toEqual({
  89. x: 0,
  90. y: 0,
  91. width: 10,
  92. height: 8,
  93. });
  94. });
  95. });
  96. describe('#Rectangle.fromPositionAndSize', () => {
  97. it('should create a rectangle from the given position and size', () => {
  98. expect(__1.Rectangle.fromPositionAndSize({ x: 2, y: 5 }, { width: 10, height: 8 }).toJSON()).toEqual({
  99. x: 2,
  100. y: 5,
  101. width: 10,
  102. height: 8,
  103. });
  104. });
  105. });
  106. describe('#Rectangle.fromEllipse', () => {
  107. it('should create a rectangle from the given ellipse', () => {
  108. expect(__1.Rectangle.fromEllipse(new __1.Ellipse(1, 2, 3, 4)).toJSON()).toEqual({
  109. x: -2,
  110. y: -2,
  111. width: 6,
  112. height: 8,
  113. });
  114. });
  115. });
  116. describe('#left,top,right,bottom', () => {
  117. it('should return properties of rectangle', () => {
  118. const obj = { x: 1, y: 2, width: 3, height: 4 };
  119. const rect = __1.Rectangle.create(obj);
  120. expect(rect.left).toEqual(obj.x);
  121. expect(rect.top).toEqual(obj.y);
  122. expect(rect.right).toEqual(obj.x + obj.width);
  123. expect(rect.bottom).toEqual(obj.y + obj.height);
  124. });
  125. });
  126. describe('#valueOf', () => {
  127. it('should return JSON object', () => {
  128. const obj = { x: 1, y: 2, width: 3, height: 4 };
  129. const rect = __1.Rectangle.create(obj);
  130. expect(rect.valueOf()).toEqual(obj);
  131. });
  132. });
  133. describe('#toString', () => {
  134. it('should return JSON string', () => {
  135. const obj = { x: 1, y: 2, width: 3, height: 4 };
  136. const rect = __1.Rectangle.create(obj);
  137. expect(rect.toString()).toEqual(JSON.stringify(obj));
  138. });
  139. });
  140. describe('#bbox', () => {
  141. it('should return a rectangle that is the bounding box of the rectangle.', () => {
  142. const rect = new __1.Rectangle(1, 2, 3, 4);
  143. expect(rect.bbox().equals(rect)).toBeTruthy();
  144. });
  145. it('should rotate the rectangle if angle specified.', () => {
  146. const rect = new __1.Rectangle(0, 0, 2, 4);
  147. expect(rect.bbox(90).round().equals(new __1.Rectangle(-1, 1, 4, 2))).toBeTruthy();
  148. });
  149. });
  150. describe('#add', () => {
  151. it('should add the given `rect` to me', () => {
  152. const rect = new __1.Rectangle(1, 2, 3, 4);
  153. expect(rect.add(0, 0, 0, 0)).toEqual(new __1.Rectangle(0, 0, 4, 6));
  154. });
  155. });
  156. describe('#update', () => {
  157. it('should update me with the given `rect`', () => {
  158. const rect = new __1.Rectangle(1, 2, 3, 4);
  159. expect(rect.update(0, 0, 1, 1)).toEqual(new __1.Rectangle(0, 0, 1, 1));
  160. });
  161. });
  162. describe('#inflate', () => {
  163. it('should inflate me with the given `amount`', () => {
  164. const rect = new __1.Rectangle(1, 2, 3, 4);
  165. expect(rect.inflate(2)).toEqual(new __1.Rectangle(-1, 0, 7, 8));
  166. });
  167. it('should inflate me with the given `dx` and `dy`', () => {
  168. const rect = new __1.Rectangle(1, 2, 3, 4);
  169. expect(rect.inflate(2, 1)).toEqual(new __1.Rectangle(-1, 1, 7, 6));
  170. });
  171. });
  172. describe('#snapToGrid', () => {
  173. it('should snap to grid', () => {
  174. const rect1 = new __1.Rectangle(2, 6, 33, 44);
  175. const rect2 = rect1.clone().snapToGrid(10);
  176. const rect3 = rect1.clone().snapToGrid(3, 5);
  177. expect(rect2.equals({ x: 0, y: 10, width: 40, height: 40 })).toBeTruthy();
  178. expect(rect3.equals({ x: 3, y: 5, width: 33, height: 45 })).toBeTruthy();
  179. });
  180. });
  181. describe('#translate', () => {
  182. it('should translate x and y by adding the given `dx` and `dy` values respectively', () => {
  183. const rect = new __1.Rectangle(1, 2, 3, 4);
  184. expect(rect.clone().translate(2, 3).toJSON()).toEqual({
  185. x: 3,
  186. y: 5,
  187. width: 3,
  188. height: 4,
  189. });
  190. expect(rect.clone().translate(new __1.Point(-2, 4)).toJSON()).toEqual({
  191. x: -1,
  192. y: 6,
  193. width: 3,
  194. height: 4,
  195. });
  196. });
  197. });
  198. describe('#scale', () => {
  199. it('should scale point with the given amount', () => {
  200. expect(new __1.Rectangle(1, 2, 3, 4).scale(2, 3).toJSON()).toEqual({
  201. x: 2,
  202. y: 6,
  203. width: 6,
  204. height: 12,
  205. });
  206. });
  207. it('should scale point with the given amount and center ', () => {
  208. expect(new __1.Rectangle(20, 30, 10, 20).scale(2, 3, new __1.Point(40, 45)).toJSON()).toEqual({ x: 0, y: 0, width: 20, height: 60 });
  209. });
  210. });
  211. describe('#rotate', () => {
  212. it('should rorate the rect by the given angle', () => {
  213. expect(new __1.Rectangle(1, 2, 3, 4).rotate(180).round().toJSON()).toEqual({
  214. x: 1,
  215. y: 2,
  216. width: 3,
  217. height: 4,
  218. });
  219. });
  220. it('should keep the same when the given angle is `0`', () => {
  221. expect(new __1.Rectangle(1, 2, 3, 4).rotate(0).toJSON()).toEqual({
  222. x: 1,
  223. y: 2,
  224. width: 3,
  225. height: 4,
  226. });
  227. });
  228. });
  229. describe('#rotate90', () => {
  230. it("should rorate the rect by 90deg around it's center", () => {
  231. expect(new __1.Rectangle(1, 2, 3, 4).rotate90().toJSON()).toEqual({
  232. x: 0.5,
  233. y: 2.5,
  234. width: 4,
  235. height: 3,
  236. });
  237. });
  238. });
  239. describe('#getMaxScaleToFit', () => {
  240. it('should return the scale amount', () => {
  241. const scale1 = new __1.Rectangle(1, 2, 3, 4).getMaxScaleToFit([5, 6, 7, 8]);
  242. const scale2 = new __1.Rectangle(1, 2, 3, 4).getMaxScaleToFit([0, 0, 7, 8]);
  243. expect(scale1.sx.toFixed(2)).toEqual('0.16');
  244. expect(scale1.sy.toFixed(2)).toEqual('0.20');
  245. expect(scale2.sx.toFixed(2)).toEqual('0.33');
  246. expect(scale2.sy.toFixed(2)).toEqual('0.50');
  247. });
  248. });
  249. describe('#getMaxUniformScaleToFit', () => {
  250. it('should return the scale amount', () => {
  251. const s1 = new __1.Rectangle(1, 2, 3, 4).getMaxUniformScaleToFit([5, 6, 7, 8]);
  252. const s2 = new __1.Rectangle(1, 2, 3, 4).getMaxUniformScaleToFit([0, 0, 7, 8]);
  253. expect(s1.toFixed(2)).toEqual('0.16');
  254. expect(s2.toFixed(2)).toEqual('0.33');
  255. });
  256. });
  257. describe('#moveAndExpand', () => {
  258. it('should translate and expand me by the given `rect`', () => {
  259. expect(new __1.Rectangle(1, 2, 3, 4)
  260. .moveAndExpand(new __1.Rectangle(1, 2, 3, 4))
  261. .toJSON()).toEqual({ x: 2, y: 4, width: 6, height: 8 });
  262. expect(new __1.Rectangle(1, 2, 3, 4).moveAndExpand(new __1.Rectangle()).toJSON()).toEqual({ x: 1, y: 2, width: 3, height: 4 });
  263. });
  264. });
  265. describe('#containsPoint', () => {
  266. it('should return true when rect contains the given point', () => {
  267. expect(new __1.Rectangle(50, 50, 100, 100).containsPoint(60, 60)).toBeTruthy();
  268. });
  269. });
  270. describe('#containsRect', () => {
  271. it('should return true when rect is completely inside the other rect', () => {
  272. expect(new __1.Rectangle(50, 50, 100, 100).containsRect(60, 60, 80, 80)).toBeTruthy();
  273. });
  274. it('should return true when rect is equal the other rect', () => {
  275. expect(new __1.Rectangle(50, 50, 100, 100).containsRect({
  276. x: 50,
  277. y: 50,
  278. width: 100,
  279. height: 100,
  280. })).toBeTruthy();
  281. });
  282. it('should return false when rect is not inside the other rect', () => {
  283. expect(new __1.Rectangle(50, 50, 100, 100).containsRect(20, 20, 200, 200)).toBeFalsy();
  284. expect(new __1.Rectangle(50, 50, 100, 100).containsRect(40, 40, 100, 100)).toBeFalsy();
  285. expect(new __1.Rectangle(50, 50, 100, 100).containsRect(60, 60, 100, 40)).toBeFalsy();
  286. expect(new __1.Rectangle(50, 50, 100, 100).containsRect(60, 60, 100, 100)).toBeFalsy();
  287. expect(new __1.Rectangle(50, 50, 100, 100).containsRect(60, 60, 40, 100)).toBeFalsy();
  288. });
  289. it('should return false when one of the dimensions is `0`', () => {
  290. expect(new __1.Rectangle(50, 50, 100, 100).containsRect(75, 75, 0, 0)).toBeFalsy();
  291. expect(new __1.Rectangle(50, 50, 0, 0).containsRect(50, 50, 0, 0)).toBeFalsy();
  292. });
  293. });
  294. describe('#intersectsWithRect', () => {
  295. it('should return the intersection', () => {
  296. var _a, _b, _c;
  297. // inside
  298. expect((_a = new __1.Rectangle(20, 20, 100, 100)
  299. .intersectsWithRect([40, 40, 20, 20])) === null || _a === void 0 ? void 0 : _a.toJSON()).toEqual({ x: 40, y: 40, width: 20, height: 20 });
  300. expect((_b = new __1.Rectangle(20, 20, 100, 100)
  301. .intersectsWithRect([0, 0, 100, 100])) === null || _b === void 0 ? void 0 : _b.toJSON()).toEqual({ x: 20, y: 20, width: 80, height: 80 });
  302. expect((_c = new __1.Rectangle(20, 20, 100, 100)
  303. .intersectsWithRect([40, 40, 100, 100])) === null || _c === void 0 ? void 0 : _c.toJSON()).toEqual({ x: 40, y: 40, width: 80, height: 80 });
  304. });
  305. it('should return null when no intersection', () => {
  306. expect(new __1.Rectangle(20, 20, 100, 100).intersectsWithRect([140, 140, 20, 20])).toBeNull();
  307. });
  308. });
  309. describe('#intersectsWithLine', () => {
  310. it('should return the intersection points', () => {
  311. const points1 = new __1.Rectangle(0, 0, 4, 4).intersectsWithLine(new __1.Line(2, 2, 2, 8));
  312. const points2 = new __1.Rectangle(0, 0, 4, 4).intersectsWithLine(new __1.Line(2, -2, 2, 8));
  313. expect(__1.Point.equalPoints(points1, [{ x: 2, y: 4 }]));
  314. expect(__1.Point.equalPoints(points2, [
  315. { x: 2, y: 0 },
  316. { x: 2, y: 4 },
  317. ]));
  318. });
  319. it('should return null when no intersection exists', () => {
  320. expect(new __1.Rectangle(0, 0, 4, 4).intersectsWithLine(new __1.Line(-2, -2, -2, -8))).toBeNull();
  321. });
  322. });
  323. describe('#intersectsWithLineFromCenterToPoint', () => {
  324. it('should return the intersection point', () => {
  325. var _a, _b;
  326. expect((_a = new __1.Rectangle(0, 0, 4, 4)
  327. .intersectsWithLineFromCenterToPoint([2, 8])) === null || _a === void 0 ? void 0 : _a.equals([2, 4])).toBeTruthy();
  328. expect((_b = new __1.Rectangle(0, 0, 4, 4)
  329. .intersectsWithLineFromCenterToPoint([2, 8], 90)) === null || _b === void 0 ? void 0 : _b.round().equals([2, 4])).toBeTruthy();
  330. });
  331. it('should return null when no intersection exists', () => {
  332. expect(new __1.Rectangle(0, 0, 4, 4).intersectsWithLineFromCenterToPoint([3, 3])).toBeNull();
  333. });
  334. });
  335. describe('#normalize', () => {
  336. it('should keep the same when width and height is positive', () => {
  337. expect(new __1.Rectangle(1, 2, 3, 4).normalize().toJSON()).toEqual({
  338. x: 1,
  339. y: 2,
  340. width: 3,
  341. height: 4,
  342. });
  343. });
  344. it('should make the width positive', () => {
  345. expect(new __1.Rectangle(1, 2, -3, 4).normalize().toJSON()).toEqual({
  346. x: -2,
  347. y: 2,
  348. width: 3,
  349. height: 4,
  350. });
  351. });
  352. it('should make the height positive', () => {
  353. expect(new __1.Rectangle(1, 2, 3, -4).normalize().toJSON()).toEqual({
  354. x: 1,
  355. y: -2,
  356. width: 3,
  357. height: 4,
  358. });
  359. });
  360. });
  361. describe('#union', () => {
  362. it('should return the unioned rectangle', () => {
  363. const rect1 = new __1.Rectangle(-1, -1, 3, 3);
  364. const rect2 = new __1.Rectangle(0, 0, 4, 4);
  365. const rect = rect1.union(rect2);
  366. expect(rect.toJSON()).toEqual({
  367. x: -1,
  368. y: -1,
  369. width: 5,
  370. height: 5,
  371. });
  372. });
  373. });
  374. describe('#getNearestSideToPoint', () => {
  375. it('should return the nearest side to point when point is on the side', () => {
  376. const rect = new __1.Rectangle(0, 0, 4, 4);
  377. expect(rect.getNearestSideToPoint({ x: 0, y: 0 })).toEqual('left');
  378. expect(rect.getNearestSideToPoint({ x: 4, y: 4 })).toEqual('right');
  379. expect(rect.getNearestSideToPoint({ x: 0, y: 4 })).toEqual('left');
  380. expect(rect.getNearestSideToPoint({ x: 4, y: 0 })).toEqual('right');
  381. expect(rect.getNearestSideToPoint({ x: 2, y: 0 })).toEqual('top');
  382. expect(rect.getNearestSideToPoint({ x: 0, y: 2 })).toEqual('left');
  383. expect(rect.getNearestSideToPoint({ x: 4, y: 2 })).toEqual('right');
  384. expect(rect.getNearestSideToPoint({ x: 2, y: 4 })).toEqual('bottom');
  385. });
  386. it('should return the nearest side to point when point is outside', () => {
  387. const rect = new __1.Rectangle(0, 0, 4, 4);
  388. expect(rect.getNearestSideToPoint({ x: 5, y: 5 })).toEqual('right');
  389. expect(rect.getNearestSideToPoint({ x: 5, y: 4 })).toEqual('right');
  390. expect(rect.getNearestSideToPoint({ x: 5, y: 2 })).toEqual('right');
  391. expect(rect.getNearestSideToPoint({ x: 5, y: 0 })).toEqual('right');
  392. expect(rect.getNearestSideToPoint({ x: 5, y: -1 })).toEqual('right');
  393. expect(rect.getNearestSideToPoint({ x: -1, y: 5 })).toEqual('left');
  394. expect(rect.getNearestSideToPoint({ x: -1, y: 4 })).toEqual('left');
  395. expect(rect.getNearestSideToPoint({ x: -1, y: 2 })).toEqual('left');
  396. expect(rect.getNearestSideToPoint({ x: -1, y: 0 })).toEqual('left');
  397. expect(rect.getNearestSideToPoint({ x: -1, y: -1 })).toEqual('left');
  398. expect(rect.getNearestSideToPoint({ x: 0, y: 5 })).toEqual('bottom');
  399. expect(rect.getNearestSideToPoint({ x: 2, y: 5 })).toEqual('bottom');
  400. expect(rect.getNearestSideToPoint({ x: 4, y: 5 })).toEqual('bottom');
  401. expect(rect.getNearestSideToPoint({ x: 0, y: -1 })).toEqual('top');
  402. expect(rect.getNearestSideToPoint({ x: 2, y: -1 })).toEqual('top');
  403. expect(rect.getNearestSideToPoint({ x: 4, y: -1 })).toEqual('top');
  404. });
  405. it('should return the nearest side to point when point is inside', () => {
  406. const rect = new __1.Rectangle(0, 0, 4, 4);
  407. expect(rect.getNearestSideToPoint({ x: 2, y: 1 })).toEqual('top');
  408. expect(rect.getNearestSideToPoint({ x: 3, y: 2 })).toEqual('right');
  409. expect(rect.getNearestSideToPoint({ x: 2, y: 3 })).toEqual('bottom');
  410. expect(rect.getNearestSideToPoint({ x: 1, y: 2 })).toEqual('left');
  411. });
  412. });
  413. describe('#getNearestPointToPoint', () => {
  414. it('should return the nearest point to point when point is inside the rect', () => {
  415. const rect = new __1.Rectangle(0, 0, 4, 4);
  416. // left
  417. expect(rect.getNearestPointToPoint({ x: 1, y: 2 }).toJSON()).toEqual({
  418. x: 0,
  419. y: 2,
  420. });
  421. // right
  422. expect(rect.getNearestPointToPoint({ x: 3, y: 2 }).toJSON()).toEqual({
  423. x: 4,
  424. y: 2,
  425. });
  426. // top
  427. expect(rect.getNearestPointToPoint({ x: 2, y: 1 }).toJSON()).toEqual({
  428. x: 2,
  429. y: 0,
  430. });
  431. // bottom
  432. expect(rect.getNearestPointToPoint({ x: 2, y: 3 }).toJSON()).toEqual({
  433. x: 2,
  434. y: 4,
  435. });
  436. });
  437. it('should return the nearest point to point when point is outside the rect', () => {
  438. const rect = new __1.Rectangle(0, 0, 4, 4);
  439. expect(rect.getNearestPointToPoint({ x: 5, y: 5 }).toJSON()).toEqual({
  440. x: 4,
  441. y: 4,
  442. });
  443. expect(rect.getNearestPointToPoint({ x: -1, y: -1 }).toJSON()).toEqual({
  444. x: 0,
  445. y: 0,
  446. });
  447. });
  448. });
  449. describe('#serialize', () => {
  450. it('should return the serialized string', () => {
  451. expect(new __1.Rectangle(1, 2, 3, 4).serialize()).toEqual('1 2 3 4');
  452. });
  453. });
  454. });
  455. //# sourceMappingURL=rectangle.test.js.map