-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathapplyObjectLimit.test.ts
More file actions
113 lines (102 loc) · 3.33 KB
/
Copy pathapplyObjectLimit.test.ts
File metadata and controls
113 lines (102 loc) · 3.33 KB
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
111
112
113
import { describe, expect, test } from "bun:test"
import { applyObjectLimit } from "site/utils/applyObjectLimit"
const order = [
"arrows",
"infiniteLines",
"lines",
"rects",
"polygons",
"circles",
"texts",
"points",
] as const
const emptyBuckets = () => ({
arrows: [] as number[],
infiniteLines: [] as number[],
lines: [] as number[],
rects: [] as number[],
polygons: [] as number[],
circles: [] as number[],
texts: [] as number[],
points: [] as number[],
})
describe("applyObjectLimit", () => {
test("returns buckets unchanged when limit is undefined", () => {
const buckets = { ...emptyBuckets(), lines: [1, 2, 3], points: [4, 5] }
const result = applyObjectLimit(buckets, order, undefined)
expect(result).toBe(buckets)
})
test("returns buckets unchanged when limit is zero", () => {
const buckets = { ...emptyBuckets(), lines: [1, 2, 3] }
const result = applyObjectLimit(buckets, order, 0)
expect(result).toBe(buckets)
})
test("returns buckets unchanged when total does not exceed limit", () => {
const buckets = { ...emptyBuckets(), lines: [1, 2], points: [3] }
const result = applyObjectLimit(buckets, order, 5)
expect(result).toBe(buckets)
})
test("caps total objects at limit when only one bucket exceeds it", () => {
const buckets = { ...emptyBuckets(), points: [1, 2, 3, 4, 5] }
const result = applyObjectLimit(buckets, order, 3)
expect(result.points).toEqual([3, 4, 5])
})
test("fills budget from the last bucket backwards across types", () => {
const buckets = {
...emptyBuckets(),
arrows: [10, 11, 12],
lines: [20, 21],
points: [30],
}
const result = applyObjectLimit(buckets, order, 4)
expect(result.points).toEqual([30])
expect(result.lines).toEqual([20, 21])
expect(result.arrows).toEqual([12])
expect(result.infiniteLines).toEqual([])
expect(result.rects).toEqual([])
expect(result.polygons).toEqual([])
expect(result.circles).toEqual([])
expect(result.texts).toEqual([])
})
test("earlier buckets are dropped entirely when later buckets exhaust budget", () => {
const buckets = {
...emptyBuckets(),
arrows: [1, 2],
lines: [3, 4],
points: [5, 6, 7],
}
const result = applyObjectLimit(buckets, order, 3)
expect(result.points).toEqual([5, 6, 7])
expect(result.lines).toEqual([])
expect(result.arrows).toEqual([])
})
test("does not mutate the input buckets", () => {
const buckets = {
...emptyBuckets(),
arrows: [1, 2, 3],
points: [4, 5],
}
const snapshot = {
...buckets,
arrows: [...buckets.arrows],
points: [...buckets.points],
}
applyObjectLimit(buckets, order, 2)
expect(buckets.arrows).toEqual(snapshot.arrows)
expect(buckets.points).toEqual(snapshot.points)
})
test("limit equal to total returns all objects", () => {
const buckets = { ...emptyBuckets(), lines: [1, 2], points: [3, 4] }
const result = applyObjectLimit(buckets, order, 4)
expect(result.lines).toEqual([1, 2])
expect(result.points).toEqual([3, 4])
})
test("preserves order within each kept bucket", () => {
const buckets = {
...emptyBuckets(),
lines: [100, 200, 300, 400],
}
const result = applyObjectLimit(buckets, order, 2)
expect(result.lines).toEqual([300, 400])
})
})