positions.test.ts
1 import { describe, expect, test } from "bun:test"; 2 import Yoga from "yoga-layout"; 3 import { absoluteLeft, absoluteTop } from "~/utils/positions"; 4 5 describe("positions", () => { 6 describe("absoluteLeft", () => { 7 test("should return left position for node without parent", () => { 8 const node = Yoga.Node.create(); 9 node.setWidth(100); 10 node.setHeight(100); 11 node.calculateLayout(100, 100, Yoga.DIRECTION_LTR); 12 13 const result = absoluteLeft(node); 14 expect(result).toBe(0); // Default position when no explicit position set 15 16 node.free(); 17 }); 18 19 test("should calculate absolute left position with nested structure", () => { 20 const parent = Yoga.Node.create(); 21 const child = Yoga.Node.create(); 22 23 parent.setWidth(200); 24 parent.setHeight(200); 25 parent.setPadding(Yoga.EDGE_LEFT, 50); 26 27 child.setWidth(100); 28 child.setHeight(100); 29 30 parent.insertChild(child, 0); 31 parent.calculateLayout(200, 200, Yoga.DIRECTION_LTR); 32 33 const result = absoluteLeft(child); 34 expect(result).toBeGreaterThanOrEqual(0); // Should be positioned based on layout calculations 35 36 parent.free(); 37 }); 38 }); 39 40 describe("absoluteTop", () => { 41 test("should return top position for node without parent", () => { 42 const node = Yoga.Node.create(); 43 node.setWidth(100); 44 node.setHeight(100); 45 node.calculateLayout(100, 100, Yoga.DIRECTION_LTR); 46 47 const result = absoluteTop(node); 48 expect(result).toBe(0); // Default position when no explicit position set 49 50 node.free(); 51 }); 52 53 test("should calculate absolute top position with nested structure", () => { 54 const parent = Yoga.Node.create(); 55 const child = Yoga.Node.create(); 56 57 parent.setWidth(200); 58 parent.setHeight(200); 59 parent.setPadding(Yoga.EDGE_TOP, 30); 60 61 child.setWidth(100); 62 child.setHeight(100); 63 64 parent.insertChild(child, 0); 65 parent.calculateLayout(200, 200, Yoga.DIRECTION_LTR); 66 67 const result = absoluteTop(child); 68 expect(result).toBeGreaterThanOrEqual(0); // Should be positioned based on layout calculations 69 70 parent.free(); 71 }); 72 }); 73 74 describe("complex nested structures", () => { 75 test("should handle deeply nested nodes", () => { 76 const grandparent = Yoga.Node.create(); 77 const parent = Yoga.Node.create(); 78 const child = Yoga.Node.create(); 79 80 grandparent.setWidth(300); 81 grandparent.setHeight(300); 82 grandparent.setPadding(Yoga.EDGE_ALL, 10); 83 84 parent.setWidth(200); 85 parent.setHeight(200); 86 parent.setMargin(Yoga.EDGE_LEFT, 20); 87 parent.setMargin(Yoga.EDGE_TOP, 15); 88 89 child.setWidth(100); 90 child.setHeight(100); 91 92 grandparent.insertChild(parent, 0); 93 parent.insertChild(child, 0); 94 grandparent.calculateLayout(300, 300, Yoga.DIRECTION_LTR); 95 96 const leftResult = absoluteLeft(child); 97 const topResult = absoluteTop(child); 98 99 expect(leftResult).toBeGreaterThanOrEqual(0); 100 expect(topResult).toBeGreaterThanOrEqual(0); 101 102 grandparent.free(); 103 }); 104 }); 105 });