Type snapUtils test

This commit is contained in:
Jordan Eldredge 2018-09-17 09:06:27 -07:00
parent 954e9f038d
commit 926be9570b
2 changed files with 19 additions and 13 deletions

View file

@ -53,43 +53,43 @@ describe("near function", () => {
describe("overlap functions", () => {
it("overlapY detects when the boxes overlap in the Y axis", () => {
const a = { y: 10, height: 50 };
const b = { y: 40, height: 50 };
const a = { y: 10, height: 50, width: 100, x: 0 };
const b = { y: 40, height: 50, width: 100, x: 0 };
const actual = overlapY(a, b);
const expected = true;
expect(actual).toEqual(expected);
});
it("overlapY detects when the boxes are within SNAP_DISTANCE on the Y axis", () => {
const a = { y: 10, height: 50 };
const b = { y: 70, height: 50 };
const a = { y: 10, height: 50, width: 100, x: 0 };
const b = { y: 70, height: 50, width: 100, x: 0 };
const actual = overlapY(a, b);
const expected = true;
expect(actual).toEqual(expected);
});
it("overlapY detects when the boxes do not overlap in the Y axis", () => {
const a = { y: 10, height: 50 };
const b = { y: 90, height: 50 };
const a = { y: 10, height: 50, width: 100, x: 0 };
const b = { y: 90, height: 50, width: 100, x: 0 };
const actual = overlapY(a, b);
const expected = false;
expect(actual).toEqual(expected);
});
it("overlapX detects when the boxes overlap in the X axis", () => {
const a = { x: 10, width: 50 };
const b = { x: 40, width: 50 };
const a = { x: 10, width: 50, height: 100, y: 0 };
const b = { x: 40, width: 50, height: 100, y: 0 };
const actual = overlapX(a, b);
const expected = true;
expect(actual).toEqual(expected);
});
it("overlapX detects when the boxes are within SNAP_DISTANCE on the X axis", () => {
const a = { x: 10, width: 50 };
const b = { x: 70, width: 50 };
const a = { x: 10, width: 50, height: 100, y: 0 };
const b = { x: 70, width: 50, height: 100, y: 0 };
const actual = overlapX(a, b);
const expected = true;
expect(actual).toEqual(expected);
});
it("overlapX detects when the boxes do not overlap in the X axis", () => {
const a = { x: 10, width: 50 };
const b = { x: 90, width: 50 };
const a = { x: 10, width: 50, height: 100, y: 0 };
const b = { x: 90, width: 50, height: 100, y: 0 };
const actual = overlapX(a, b);
const expected = false;
expect(actual).toEqual(expected);

View file

@ -7,6 +7,12 @@ interface Diff {
x?: number;
y?: number;
}
interface BoundingBox {
width: number;
height: number;
}
interface Box extends Point {
width: number;
height: number;
@ -96,7 +102,7 @@ export const snapToMany = (boxA: Box, otherBoxes: Box[]): Diff => {
return { x, y };
};
export const snapWithin = (boxA: Box, boundingBox: Box): Diff => {
export const snapWithin = (boxA: Box, boundingBox: BoundingBox): Diff => {
let x, y;
if (boxA.x - SNAP_DISTANCE < 0) {