1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and affiliates.
    
  3.  *
    
  4.  * This source code is licensed under the MIT license found in the
    
  5.  * LICENSE file in the root directory of this source tree.
    
  6.  *
    
  7.  * @flow
    
  8.  */
    
  9. 
    
  10. import {
    
  11.   pointEqualToPoint,
    
  12.   sizeEqualToSize,
    
  13.   rectEqualToRect,
    
  14.   sizeIsValid,
    
  15.   sizeIsEmpty,
    
  16.   rectIntersectsRect,
    
  17.   intersectionOfRects,
    
  18.   rectContainsPoint,
    
  19.   unionOfRects,
    
  20. } from '../geometry';
    
  21. 
    
  22. describe(pointEqualToPoint, () => {
    
  23.   it('should return true when 2 points have the same values', () => {
    
  24.     expect(pointEqualToPoint({x: 1, y: 1}, {x: 1, y: 1})).toBe(true);
    
  25.     expect(pointEqualToPoint({x: -1, y: 2}, {x: -1, y: 2})).toBe(true);
    
  26.     expect(
    
  27.       pointEqualToPoint({x: 3.14159, y: 0.26535}, {x: 3.14159, y: 0.26535}),
    
  28.     ).toBe(true);
    
  29.   });
    
  30. 
    
  31.   it('should return false when 2 points have different values', () => {
    
  32.     expect(pointEqualToPoint({x: 1, y: 1}, {x: 1, y: 0})).toBe(false);
    
  33.     expect(pointEqualToPoint({x: -1, y: 2}, {x: 0, y: 1})).toBe(false);
    
  34.     expect(
    
  35.       pointEqualToPoint({x: 3.1416, y: 0.26534}, {x: 3.14159, y: 0.26535}),
    
  36.     ).toBe(false);
    
  37.   });
    
  38. });
    
  39. 
    
  40. describe(sizeEqualToSize, () => {
    
  41.   it('should return true when 2 sizes have the same values', () => {
    
  42.     expect(sizeEqualToSize({width: 1, height: 1}, {width: 1, height: 1})).toBe(
    
  43.       true,
    
  44.     );
    
  45.     expect(
    
  46.       sizeEqualToSize({width: -1, height: 2}, {width: -1, height: 2}),
    
  47.     ).toBe(true);
    
  48.     expect(
    
  49.       sizeEqualToSize(
    
  50.         {width: 3.14159, height: 0.26535},
    
  51.         {width: 3.14159, height: 0.26535},
    
  52.       ),
    
  53.     ).toBe(true);
    
  54.   });
    
  55. 
    
  56.   it('should return false when 2 sizes have different values', () => {
    
  57.     expect(sizeEqualToSize({width: 1, height: 1}, {width: 1, height: 0})).toBe(
    
  58.       false,
    
  59.     );
    
  60.     expect(sizeEqualToSize({width: -1, height: 2}, {width: 0, height: 1})).toBe(
    
  61.       false,
    
  62.     );
    
  63.     expect(
    
  64.       sizeEqualToSize(
    
  65.         {width: 3.1416, height: 0.26534},
    
  66.         {width: 3.14159, height: 0.26535},
    
  67.       ),
    
  68.     ).toBe(false);
    
  69.   });
    
  70. });
    
  71. 
    
  72. describe(rectEqualToRect, () => {
    
  73.   it('should return true when 2 rects have the same values', () => {
    
  74.     expect(
    
  75.       rectEqualToRect(
    
  76.         {origin: {x: 1, y: 1}, size: {width: 1, height: 1}},
    
  77.         {origin: {x: 1, y: 1}, size: {width: 1, height: 1}},
    
  78.       ),
    
  79.     ).toBe(true);
    
  80.     expect(
    
  81.       rectEqualToRect(
    
  82.         {origin: {x: 1, y: 2}, size: {width: 3.14, height: 4}},
    
  83.         {origin: {x: 1, y: 2}, size: {width: 3.14, height: 4}},
    
  84.       ),
    
  85.     ).toBe(true);
    
  86.   });
    
  87. 
    
  88.   it('should return false when 2 rects have different values', () => {
    
  89.     expect(
    
  90.       rectEqualToRect(
    
  91.         {origin: {x: 1, y: 1}, size: {width: 1, height: 1}},
    
  92.         {origin: {x: 0, y: 1}, size: {width: 1, height: 1}},
    
  93.       ),
    
  94.     ).toBe(false);
    
  95.     expect(
    
  96.       rectEqualToRect(
    
  97.         {origin: {x: 1, y: 2}, size: {width: 3.14, height: 4}},
    
  98.         {origin: {x: 1, y: 2}, size: {width: 3.15, height: 4}},
    
  99.       ),
    
  100.     ).toBe(false);
    
  101.   });
    
  102. });
    
  103. 
    
  104. describe(sizeIsValid, () => {
    
  105.   it('should return true when the size has non-negative width and height', () => {
    
  106.     expect(sizeIsValid({width: 1, height: 1})).toBe(true);
    
  107.     expect(sizeIsValid({width: 0, height: 0})).toBe(true);
    
  108.   });
    
  109. 
    
  110.   it('should return false when the size has negative width or height', () => {
    
  111.     expect(sizeIsValid({width: 0, height: -1})).toBe(false);
    
  112.     expect(sizeIsValid({width: -1, height: 0})).toBe(false);
    
  113.     expect(sizeIsValid({width: -1, height: -1})).toBe(false);
    
  114.   });
    
  115. });
    
  116. 
    
  117. describe(sizeIsEmpty, () => {
    
  118.   it('should return true when the size has negative area', () => {
    
  119.     expect(sizeIsEmpty({width: 1, height: -1})).toBe(true);
    
  120.     expect(sizeIsEmpty({width: -1, height: -1})).toBe(true);
    
  121.   });
    
  122. 
    
  123.   it('should return true when the size has zero area', () => {
    
  124.     expect(sizeIsEmpty({width: 0, height: 0})).toBe(true);
    
  125.     expect(sizeIsEmpty({width: 0, height: 1})).toBe(true);
    
  126.     expect(sizeIsEmpty({width: 1, height: 0})).toBe(true);
    
  127.   });
    
  128. 
    
  129.   it('should return false when the size has positive area', () => {
    
  130.     expect(sizeIsEmpty({width: 1, height: 1})).toBe(false);
    
  131.     expect(sizeIsEmpty({width: 2, height: 1})).toBe(false);
    
  132.   });
    
  133. });
    
  134. 
    
  135. describe(rectIntersectsRect, () => {
    
  136.   it('should return true when 2 rects intersect', () => {
    
  137.     // Rects touch
    
  138.     expect(
    
  139.       rectIntersectsRect(
    
  140.         {origin: {x: 0, y: 0}, size: {width: 1, height: 1}},
    
  141.         {origin: {x: 1, y: 1}, size: {width: 1, height: 1}},
    
  142.       ),
    
  143.     ).toEqual(true);
    
  144. 
    
  145.     // Rects overlap
    
  146.     expect(
    
  147.       rectIntersectsRect(
    
  148.         {origin: {x: 0, y: 0}, size: {width: 2, height: 1}},
    
  149.         {origin: {x: 1, y: -2}, size: {width: 0.5, height: 5}},
    
  150.       ),
    
  151.     ).toEqual(true);
    
  152. 
    
  153.     // Rects are equal
    
  154.     expect(
    
  155.       rectIntersectsRect(
    
  156.         {origin: {x: 1, y: 2}, size: {width: 3.14, height: 4}},
    
  157.         {origin: {x: 1, y: 2}, size: {width: 3.14, height: 4}},
    
  158.       ),
    
  159.     ).toEqual(true);
    
  160.   });
    
  161. 
    
  162.   it('should return false when 2 rects do not intersect', () => {
    
  163.     expect(
    
  164.       rectIntersectsRect(
    
  165.         {origin: {x: 0, y: 1}, size: {width: 1, height: 1}},
    
  166.         {origin: {x: 0, y: 10}, size: {width: 1, height: 1}},
    
  167.       ),
    
  168.     ).toBe(false);
    
  169.     expect(
    
  170.       rectIntersectsRect(
    
  171.         {origin: {x: 1, y: 2}, size: {width: 3.14, height: 4}},
    
  172.         {origin: {x: -4, y: 2}, size: {width: 3.15, height: 4}},
    
  173.       ),
    
  174.     ).toBe(false);
    
  175.   });
    
  176. });
    
  177. 
    
  178. describe(intersectionOfRects, () => {
    
  179.   // NOTE: Undefined behavior if rects do not intersect
    
  180. 
    
  181.   it('should return intersection when 2 rects intersect', () => {
    
  182.     // Rects touch
    
  183.     expect(
    
  184.       intersectionOfRects(
    
  185.         {origin: {x: 0, y: 0}, size: {width: 1, height: 1}},
    
  186.         {origin: {x: 1, y: 1}, size: {width: 1, height: 1}},
    
  187.       ),
    
  188.     ).toEqual({origin: {x: 1, y: 1}, size: {width: 0, height: 0}});
    
  189. 
    
  190.     // Rects overlap
    
  191.     expect(
    
  192.       intersectionOfRects(
    
  193.         {origin: {x: 0, y: 0}, size: {width: 2, height: 1}},
    
  194.         {origin: {x: 1, y: -2}, size: {width: 0.5, height: 5}},
    
  195.       ),
    
  196.     ).toEqual({origin: {x: 1, y: 0}, size: {width: 0.5, height: 1}});
    
  197. 
    
  198.     // Rects are equal
    
  199.     expect(
    
  200.       intersectionOfRects(
    
  201.         {origin: {x: 1, y: 2}, size: {width: 9.24, height: 4}},
    
  202.         {origin: {x: 1, y: 2}, size: {width: 9.24, height: 4}},
    
  203.       ),
    
  204.     ).toEqual({origin: {x: 1, y: 2}, size: {width: 9.24, height: 4}});
    
  205.   });
    
  206. });
    
  207. 
    
  208. describe(rectContainsPoint, () => {
    
  209.   it("should return true if point is on the rect's edge", () => {
    
  210.     expect(
    
  211.       rectContainsPoint(
    
  212.         {x: 0, y: 0},
    
  213.         {origin: {x: 0, y: 0}, size: {width: 1, height: 1}},
    
  214.       ),
    
  215.     ).toBe(true);
    
  216.     expect(
    
  217.       rectContainsPoint(
    
  218.         {x: 5, y: 0},
    
  219.         {origin: {x: 0, y: 0}, size: {width: 10, height: 1}},
    
  220.       ),
    
  221.     ).toBe(true);
    
  222.     expect(
    
  223.       rectContainsPoint(
    
  224.         {x: 1, y: 1},
    
  225.         {origin: {x: 0, y: 0}, size: {width: 1, height: 1}},
    
  226.       ),
    
  227.     ).toBe(true);
    
  228.   });
    
  229. 
    
  230.   it('should return true if point is in rect', () => {
    
  231.     expect(
    
  232.       rectContainsPoint(
    
  233.         {x: 5, y: 50},
    
  234.         {origin: {x: 0, y: 0}, size: {width: 10, height: 100}},
    
  235.       ),
    
  236.     ).toBe(true);
    
  237.   });
    
  238. 
    
  239.   it('should return false if point is not in rect', () => {
    
  240.     expect(
    
  241.       rectContainsPoint(
    
  242.         {x: -1, y: 0},
    
  243.         {origin: {x: 0, y: 0}, size: {width: 1, height: 1}},
    
  244.       ),
    
  245.     ).toBe(false);
    
  246.   });
    
  247. });
    
  248. 
    
  249. describe(unionOfRects, () => {
    
  250.   it('should return zero rect if no rects are provided', () => {
    
  251.     expect(unionOfRects()).toEqual({
    
  252.       origin: {x: 0, y: 0},
    
  253.       size: {width: 0, height: 0},
    
  254.     });
    
  255.   });
    
  256. 
    
  257.   it('should return rect if 1 rect is provided', () => {
    
  258.     expect(
    
  259.       unionOfRects({origin: {x: 1, y: 2}, size: {width: 3, height: 4}}),
    
  260.     ).toEqual({origin: {x: 1, y: 2}, size: {width: 3, height: 4}});
    
  261.   });
    
  262. 
    
  263.   it('should return union of rects if more than one rect is provided', () => {
    
  264.     expect(
    
  265.       unionOfRects(
    
  266.         {origin: {x: 1, y: 2}, size: {width: 3, height: 4}},
    
  267.         {origin: {x: 100, y: 200}, size: {width: 3, height: 4}},
    
  268.         {origin: {x: -10, y: -20}, size: {width: 50, height: 60}},
    
  269.       ),
    
  270.     ).toEqual({origin: {x: -10, y: -20}, size: {width: 113, height: 224}});
    
  271.   });
    
  272. });