Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { computeLabelPosition } from "./PickLabelPosition.ts";
4
5const VIEWPORT = { viewportWidth: 1280, viewportHeight: 800 };
6
7describe("computeLabelPosition", () => {
8 it("anchors to the element's top-left when there's room above and to the right", () => {
9 const { x, y } = computeLabelPosition({
10 ...VIEWPORT,
11 targetLeft: 200,
12 targetTop: 200,
13 targetBottom: 240,
14 labelWidth: 120,
15 labelHeight: 18,
16 });
17 expect(x).toBe(200);
18 // 200 (top) - 18 (height) - 4 (gap)
19 expect(y).toBe(200 - 18 - 4);
20 });
21
22 it("clamps left edge so the label stays inside the viewport", () => {
23 const { x } = computeLabelPosition({
24 ...VIEWPORT,
25 targetLeft: -50,
26 targetTop: 200,
27 targetBottom: 240,
28 labelWidth: 120,
29 labelHeight: 18,
30 });
31 expect(x).toBe(4);
32 });
33
34 it("clamps right edge when the label would overflow the viewport (the bug we shipped)", () => {
35 const { x } = computeLabelPosition({
36 ...VIEWPORT,
37 targetLeft: 1240,
38 targetTop: 200,
39 targetBottom: 240,
40 labelWidth: 200,
41 labelHeight: 18,
42 });
43 // viewportWidth (1280) - labelWidth (200) - margin (4) = 1076
44 expect(x).toBe(1076);
45 });
46
47 it("flips the label below the element when there's no room above", () => {
48 const { y } = computeLabelPosition({
49 ...VIEWPORT,
50 targetLeft: 200,
51 targetTop: 4,
52 targetBottom: 44,
53 labelWidth: 120,
54 labelHeight: 18,
55 });
56 // labelY = 4 - 18 - 4 = -18 → flip → 44 + 4 = 48
57 expect(y).toBe(48);
58 });
59
60 it("pins to the bottom margin when the element fills the viewport (no room above OR below)", () => {
61 const { y } = computeLabelPosition({
62 ...VIEWPORT,
63 targetLeft: 200,
64 targetTop: 0,
65 targetBottom: 800,
66 labelWidth: 120,
67 labelHeight: 18,
68 });
69 // Above overflows top → flip below = 800 + 4 = 804 → also overflows
70 // bottom → pin to viewportHeight - labelHeight - margin = 778.
71 expect(y).toBe(800 - 18 - 4);
72 });
73
74 it("never returns a negative coordinate", () => {
75 const { x, y } = computeLabelPosition({
76 ...VIEWPORT,
77 targetLeft: -1000,
78 targetTop: -1000,
79 targetBottom: -900,
80 labelWidth: 5000,
81 labelHeight: 5000,
82 });
83 expect(x).toBeGreaterThanOrEqual(0);
84 expect(y).toBeGreaterThanOrEqual(0);
85 });
86});