-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.test.ts
110 lines (91 loc) · 2.56 KB
/
grid.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { describe, expect, test } from "bun:test"
import { grid } from "../src/grid"
describe("grid", () => {
test("basic 2x2 grid with default spacing", () => {
const cells = grid({ rows: 2, cols: 2 })
expect(cells.length).toBe(4)
expect(cells[0].index).toBe(0)
expect(cells[3].index).toBe(3)
// Check first cell (top-left in cartesian)
expect(cells[0].center).toEqual({ x: -0.5, y: 0.5 })
expect(cells[0].topLeft).toEqual({ x: -1, y: 1 })
expect(cells[0].bottomRight).toEqual({ x: 0, y: 0 })
})
test("2x3 grid with custom spacing", () => {
const cells = grid({
rows: 2,
cols: 3,
xSpacing: 10,
ySpacing: 5,
})
expect(cells.length).toBe(6)
// Check dimensions
const cell = cells[0]
expect(cell.bottomRight.x - cell.topLeft.x).toBe(10)
expect(cell.topLeft.y - cell.bottomRight.y).toBe(5)
})
test("grid with fixed dimensions", () => {
const cells = grid({
rows: 2,
cols: 2,
width: 100,
height: 100,
})
expect(cells.length).toBe(4)
// Each cell should be 50x50
const cell = cells[0]
expect(cell.bottomRight.x - cell.topLeft.x).toBe(50)
expect(cell.topLeft.y - cell.bottomRight.y).toBe(50)
})
test("grid with offset", () => {
const cells = grid({
rows: 1,
cols: 1,
offsetX: 10,
offsetY: 20,
})
expect(cells[0].center).toEqual({ x: 10, y: 20 })
})
test("up-is-negative coordinate system", () => {
const cells = grid({
rows: 2,
cols: 1,
yDirection: "up-is-negative",
})
// In up-is-negative, y increases downward
expect(cells[0].center.y).toBeLessThan(cells[1].center.y)
})
test("cartesian coordinate system", () => {
const cells = grid({
rows: 2,
cols: 1,
yDirection: "cartesian",
})
// In cartesian, y increases upward
expect(cells[0].center.y).toBeGreaterThan(cells[1].center.y)
})
test("centered grid (default)", () => {
const cells = grid({
rows: 2,
cols: 2,
xSpacing: 10,
ySpacing: 10,
})
// For a 2x2 grid with 10 unit spacing, the total size is 20x20
// When centered, it should go from -10 to +10 in both dimensions
expect(cells[0].center.x).toBe(-5)
expect(cells[0].center.y).toBe(5)
})
test("non-centered grid", () => {
const cells = grid({
rows: 2,
cols: 2,
xSpacing: 10,
ySpacing: 10,
centered: false,
})
// When not centered, it should start from 0,0
expect(cells[0].center.x).toBe(5)
expect(cells[0].center.y).toBe(15)
})
})