grocery-app/backend/tests/location-map-layout.service.test.js
2026-06-03 12:09:22 -07:00

79 lines
2.6 KiB
JavaScript

const {
buildStarterObjectsFromZones,
getStarterMapSize,
} = require("../services/location-map-layout.service");
function makeZones(count) {
return Array.from({ length: count }, (_, index) => ({
id: index + 1,
name: `Zone ${index + 1}`,
}));
}
function overlaps(first, second) {
return !(
first.x + first.width <= second.x ||
second.x + second.width <= first.x ||
first.y + first.height <= second.y ||
second.y + second.height <= first.y
);
}
describe("location map starter layout", () => {
test("creates bounded starter rectangles for many zones", () => {
const zones = makeZones(13);
const mapSize = getStarterMapSize(zones, { width: 1000, height: 700 });
const objects = buildStarterObjectsFromZones(zones, mapSize.width, mapSize.height);
expect(mapSize).toEqual({ width: 1000, height: 1050 });
expect(objects).toHaveLength(13);
objects.forEach((object, index) => {
expect(object).toEqual(
expect.objectContaining({
zone_id: zones[index].id,
type: "zone",
label: zones[index].name,
locked: false,
visible: true,
sort_order: index + 1,
metadata_json: { starter: true },
})
);
expect(object.x).toBeGreaterThanOrEqual(0);
expect(object.y).toBeGreaterThanOrEqual(0);
expect(object.width).toBeGreaterThanOrEqual(120);
expect(object.height).toBeGreaterThanOrEqual(80);
expect(object.x + object.width).toBeLessThanOrEqual(mapSize.width);
expect(object.y + object.height).toBeLessThanOrEqual(mapSize.height);
});
for (let firstIndex = 0; firstIndex < objects.length; firstIndex += 1) {
for (let secondIndex = firstIndex + 1; secondIndex < objects.length; secondIndex += 1) {
expect(overlaps(objects[firstIndex], objects[secondIndex])).toBe(false);
}
}
});
test("expands cramped map dimensions before placing starter rectangles", () => {
const zones = makeZones(5);
const mapSize = getStarterMapSize(zones, { width: 300, height: 180 });
const objects = buildStarterObjectsFromZones(zones, 300, 180);
expect(mapSize).toEqual({ width: 472, height: 420 });
expect(objects).toHaveLength(5);
objects.forEach((object) => {
expect(object.x + object.width).toBeLessThanOrEqual(mapSize.width);
expect(object.y + object.height).toBeLessThanOrEqual(mapSize.height);
});
});
test("returns no objects for locations with no zones", () => {
expect(buildStarterObjectsFromZones([], 1000, 700)).toEqual([]);
expect(getStarterMapSize([], { width: 1000, height: 700 })).toEqual({
width: 1000,
height: 700,
});
});
});