61 lines
1.9 KiB
TypeScript
61 lines
1.9 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { onScreen, place } from "../src/lib/place";
|
|
import type { Box } from "../src/lib/place";
|
|
|
|
const VIEW = { width: 1280, height: 800 };
|
|
const PANEL = { width: 320, height: 200 };
|
|
const row = (over: Partial<Box> = {}): Box => ({
|
|
top: 300,
|
|
left: 400,
|
|
width: 500,
|
|
height: 40,
|
|
...over,
|
|
});
|
|
|
|
describe("onScreen", () => {
|
|
it("skips the copy that is hidden and has no box", () => {
|
|
const hidden = row({ top: 0, left: 0, width: 0, height: 0 });
|
|
const shown = row();
|
|
expect(onScreen([hidden, shown])).toBe(shown);
|
|
});
|
|
|
|
it("takes the first one when several are on screen", () => {
|
|
const first = row();
|
|
expect(onScreen([first, row({ top: 500 })])).toBe(first);
|
|
});
|
|
|
|
it("says nothing when every copy is hidden", () => {
|
|
expect(onScreen([row({ width: 0, height: 0 })])).toBeNull();
|
|
expect(onScreen([])).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("place", () => {
|
|
it("sits just under the anchor", () => {
|
|
expect(place(row(), PANEL, VIEW)).toEqual({ top: 346, left: 400 });
|
|
});
|
|
|
|
it("flips above when there is no room below", () => {
|
|
expect(place(row({ top: 700 }), PANEL, VIEW)).toEqual({ top: 494, left: 400 });
|
|
});
|
|
|
|
it("stays inside the window when there is room neither way", () => {
|
|
const tall = { width: 320, height: 780 };
|
|
const spot = place(row({ top: 400 }), tall, VIEW);
|
|
expect(spot.top).toBeGreaterThanOrEqual(10);
|
|
expect(spot.top + tall.height).toBeLessThanOrEqual(VIEW.height);
|
|
});
|
|
|
|
it("pulls back from the right edge", () => {
|
|
expect(place(row({ left: 1200 }), PANEL, VIEW).left).toBe(950);
|
|
});
|
|
|
|
it("never goes off the left edge", () => {
|
|
expect(place(row({ left: -40 }), PANEL, VIEW).left).toBe(10);
|
|
});
|
|
|
|
it("centres rather than hiding in the corner with nothing to anchor to", () => {
|
|
expect(place(null, PANEL, VIEW)).toEqual({ top: 300, left: 480 });
|
|
});
|
|
});
|