onejs-react 0.1.9 → 0.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/__tests__/collection-sync.test.tsx +436 -0
- package/src/__tests__/components.test.tsx +3 -1
- package/src/__tests__/host-config.test.ts +7 -5
- package/src/__tests__/mocks.ts +40 -0
- package/src/__tests__/setup.ts +5 -3
- package/src/__tests__/style-parser.test.ts +6 -4
- package/src/hooks.ts +4 -0
- package/src/host-config.ts +37 -77
package/package.json
CHANGED
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* End-to-end tests for collection state sync patterns.
|
|
3
|
+
*
|
|
4
|
+
* Simulates the real-world use case of syncing dynamic C# game state
|
|
5
|
+
* (inventories, quest logs, NPC lists) to React components using
|
|
6
|
+
* useFrameSync selector mode + toArray.
|
|
7
|
+
*
|
|
8
|
+
* Tests the parent/child pattern:
|
|
9
|
+
* - Parent watches list structure (Count) via selector
|
|
10
|
+
* - Child components each watch their own item properties via selector
|
|
11
|
+
* - When an item property changes, only that child re-renders
|
|
12
|
+
* - When items are added/removed, the parent re-renders
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"
|
|
16
|
+
import React from "react"
|
|
17
|
+
import { useFrameSync, toArray } from "../hooks"
|
|
18
|
+
import { render, unmount } from "../renderer"
|
|
19
|
+
import { createMockContainer, flushMicrotasks } from "./mocks"
|
|
20
|
+
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
// RAF mock
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
|
|
25
|
+
type RafCallback = (time: number) => void
|
|
26
|
+
|
|
27
|
+
let rafQueue: Array<{ id: number; callback: RafCallback }> = []
|
|
28
|
+
let nextRafId = 0
|
|
29
|
+
|
|
30
|
+
function flushRaf() {
|
|
31
|
+
const queue = [...rafQueue]
|
|
32
|
+
rafQueue = []
|
|
33
|
+
const now = Date.now()
|
|
34
|
+
for (const { callback } of queue) {
|
|
35
|
+
callback(now)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function advanceFrame() {
|
|
40
|
+
flushRaf()
|
|
41
|
+
await flushMicrotasks()
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
beforeEach(() => {
|
|
45
|
+
rafQueue = []
|
|
46
|
+
nextRafId = 0
|
|
47
|
+
;(globalThis as any).requestAnimationFrame = vi.fn((cb: RafCallback) => {
|
|
48
|
+
const id = ++nextRafId
|
|
49
|
+
rafQueue.push({ id, callback: cb })
|
|
50
|
+
return id
|
|
51
|
+
})
|
|
52
|
+
;(globalThis as any).cancelAnimationFrame = vi.fn((id: number) => {
|
|
53
|
+
rafQueue = rafQueue.filter((e) => e.id !== id)
|
|
54
|
+
})
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
afterEach(() => {
|
|
58
|
+
delete (globalThis as any).requestAnimationFrame
|
|
59
|
+
delete (globalThis as any).cancelAnimationFrame
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
// Mock C# proxy objects — simulates proxy caching behavior
|
|
64
|
+
//
|
|
65
|
+
// In real OneJS, accessing a C# object from JS always returns the same
|
|
66
|
+
// proxy reference (cached by handle). Properties read through to C# on
|
|
67
|
+
// each access, so mutations are visible through the same reference.
|
|
68
|
+
// ---------------------------------------------------------------------------
|
|
69
|
+
|
|
70
|
+
interface MockItem {
|
|
71
|
+
Id: number
|
|
72
|
+
Name: string
|
|
73
|
+
Durability: number
|
|
74
|
+
StackCount: number
|
|
75
|
+
Version: number
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
interface MockInventory {
|
|
79
|
+
Items: { Count: number; [index: number]: MockItem }
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function createMockInventory(items: MockItem[]): MockInventory {
|
|
83
|
+
// Simulates a C# List<Item> — same proxy object, Count and indexer
|
|
84
|
+
// read live values (mutating the items array is visible immediately)
|
|
85
|
+
const proxy: any = {
|
|
86
|
+
get Count() { return items.length },
|
|
87
|
+
}
|
|
88
|
+
// Numeric indexer — proxy reads live from the array
|
|
89
|
+
return {
|
|
90
|
+
Items: new Proxy(proxy, {
|
|
91
|
+
get(target, prop) {
|
|
92
|
+
if (prop === "Count") return items.length
|
|
93
|
+
const idx = Number(prop)
|
|
94
|
+
if (!isNaN(idx) && idx >= 0 && idx < items.length) {
|
|
95
|
+
return items[idx]
|
|
96
|
+
}
|
|
97
|
+
return target[prop]
|
|
98
|
+
}
|
|
99
|
+
})
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function createMockItem(id: number, name: string, durability: number): MockItem {
|
|
104
|
+
return { Id: id, Name: name, Durability: durability, StackCount: 1, Version: 1 }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// ---------------------------------------------------------------------------
|
|
108
|
+
// Tests
|
|
109
|
+
// ---------------------------------------------------------------------------
|
|
110
|
+
|
|
111
|
+
describe("collection sync: parent/child pattern", () => {
|
|
112
|
+
let container: ReturnType<typeof createMockContainer>
|
|
113
|
+
|
|
114
|
+
beforeEach(() => {
|
|
115
|
+
container = createMockContainer()
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
afterEach(() => {
|
|
119
|
+
unmount(container as any)
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
it("parent re-renders when items are added, children render per item", async () => {
|
|
123
|
+
const items: MockItem[] = [
|
|
124
|
+
createMockItem(1, "Sword", 100),
|
|
125
|
+
createMockItem(2, "Shield", 80),
|
|
126
|
+
]
|
|
127
|
+
const inventory = createMockInventory(items)
|
|
128
|
+
|
|
129
|
+
let parentRenders = 0
|
|
130
|
+
const childRenders: Record<number, number> = {}
|
|
131
|
+
|
|
132
|
+
function ItemView({ item }: { item: MockItem }) {
|
|
133
|
+
const data = useFrameSync(
|
|
134
|
+
() => item,
|
|
135
|
+
(i) => [i.Name, i.Durability, i.StackCount]
|
|
136
|
+
)
|
|
137
|
+
childRenders[data.Id] = (childRenders[data.Id] || 0) + 1
|
|
138
|
+
return null
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function InventoryList() {
|
|
142
|
+
const inv = useFrameSync(
|
|
143
|
+
() => inventory,
|
|
144
|
+
(i) => [i.Items.Count]
|
|
145
|
+
)
|
|
146
|
+
parentRenders++
|
|
147
|
+
return (
|
|
148
|
+
<>
|
|
149
|
+
{toArray<MockItem>(inv.Items).map(item => (
|
|
150
|
+
<ItemView key={item.Id} item={item} />
|
|
151
|
+
))}
|
|
152
|
+
</>
|
|
153
|
+
)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
render(<InventoryList />, container as any)
|
|
157
|
+
await flushMicrotasks()
|
|
158
|
+
await advanceFrame()
|
|
159
|
+
|
|
160
|
+
expect(parentRenders).toBeGreaterThanOrEqual(1)
|
|
161
|
+
expect(childRenders[1]).toBeGreaterThanOrEqual(1)
|
|
162
|
+
expect(childRenders[2]).toBeGreaterThanOrEqual(1)
|
|
163
|
+
|
|
164
|
+
const parentRendersAfterInit = parentRenders
|
|
165
|
+
const child1RendersAfterInit = childRenders[1]
|
|
166
|
+
const child2RendersAfterInit = childRenders[2]
|
|
167
|
+
|
|
168
|
+
// Add a new item
|
|
169
|
+
items.push(createMockItem(3, "Potion", 1))
|
|
170
|
+
await advanceFrame()
|
|
171
|
+
|
|
172
|
+
// Parent should re-render (Count changed from 2 to 3)
|
|
173
|
+
expect(parentRenders).toBeGreaterThan(parentRendersAfterInit)
|
|
174
|
+
// New child should have rendered
|
|
175
|
+
expect(childRenders[3]).toBeGreaterThanOrEqual(1)
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
it("only the affected child re-renders when an item property changes", async () => {
|
|
179
|
+
const items: MockItem[] = [
|
|
180
|
+
createMockItem(1, "Sword", 100),
|
|
181
|
+
createMockItem(2, "Shield", 80),
|
|
182
|
+
createMockItem(3, "Potion", 1),
|
|
183
|
+
]
|
|
184
|
+
const inventory = createMockInventory(items)
|
|
185
|
+
|
|
186
|
+
let parentRenders = 0
|
|
187
|
+
const childRenders: Record<number, number> = {}
|
|
188
|
+
|
|
189
|
+
function ItemView({ item }: { item: MockItem }) {
|
|
190
|
+
const data = useFrameSync(
|
|
191
|
+
() => item,
|
|
192
|
+
(i) => [i.Name, i.Durability, i.StackCount]
|
|
193
|
+
)
|
|
194
|
+
childRenders[data.Id] = (childRenders[data.Id] || 0) + 1
|
|
195
|
+
return null
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function InventoryList() {
|
|
199
|
+
const inv = useFrameSync(
|
|
200
|
+
() => inventory,
|
|
201
|
+
(i) => [i.Items.Count]
|
|
202
|
+
)
|
|
203
|
+
parentRenders++
|
|
204
|
+
return (
|
|
205
|
+
<>
|
|
206
|
+
{toArray<MockItem>(inv.Items).map(item => (
|
|
207
|
+
<ItemView key={item.Id} item={item} />
|
|
208
|
+
))}
|
|
209
|
+
</>
|
|
210
|
+
)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
render(<InventoryList />, container as any)
|
|
214
|
+
await flushMicrotasks()
|
|
215
|
+
await advanceFrame()
|
|
216
|
+
|
|
217
|
+
const parentRendersAfterInit = parentRenders
|
|
218
|
+
const child1After = childRenders[1]
|
|
219
|
+
const child2After = childRenders[2]
|
|
220
|
+
const child3After = childRenders[3]
|
|
221
|
+
|
|
222
|
+
// Change only Sword's durability (simulates using the item)
|
|
223
|
+
items[0].Durability = 90
|
|
224
|
+
await advanceFrame()
|
|
225
|
+
|
|
226
|
+
// Parent should NOT re-render (Count unchanged)
|
|
227
|
+
expect(parentRenders).toBe(parentRendersAfterInit)
|
|
228
|
+
// Only Sword's child should re-render
|
|
229
|
+
expect(childRenders[1]).toBeGreaterThan(child1After)
|
|
230
|
+
// Shield and Potion should NOT re-render
|
|
231
|
+
expect(childRenders[2]).toBe(child2After)
|
|
232
|
+
expect(childRenders[3]).toBe(child3After)
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
it("version stamp on items catches any property change", async () => {
|
|
236
|
+
const items: MockItem[] = [
|
|
237
|
+
createMockItem(1, "Sword", 100),
|
|
238
|
+
createMockItem(2, "Shield", 80),
|
|
239
|
+
]
|
|
240
|
+
const inventory = createMockInventory(items)
|
|
241
|
+
|
|
242
|
+
const childRenders: Record<number, number> = {}
|
|
243
|
+
|
|
244
|
+
function ItemView({ item }: { item: MockItem }) {
|
|
245
|
+
// Watch only Version — catches all changes without listing every property
|
|
246
|
+
const data = useFrameSync(
|
|
247
|
+
() => item,
|
|
248
|
+
(i) => [i.Version]
|
|
249
|
+
)
|
|
250
|
+
childRenders[data.Id] = (childRenders[data.Id] || 0) + 1
|
|
251
|
+
return null
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function InventoryList() {
|
|
255
|
+
const inv = useFrameSync(
|
|
256
|
+
() => inventory,
|
|
257
|
+
(i) => [i.Items.Count]
|
|
258
|
+
)
|
|
259
|
+
return (
|
|
260
|
+
<>
|
|
261
|
+
{toArray<MockItem>(inv.Items).map(item => (
|
|
262
|
+
<ItemView key={item.Id} item={item} />
|
|
263
|
+
))}
|
|
264
|
+
</>
|
|
265
|
+
)
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
render(<InventoryList />, container as any)
|
|
269
|
+
await flushMicrotasks()
|
|
270
|
+
await advanceFrame()
|
|
271
|
+
|
|
272
|
+
const child1After = childRenders[1]
|
|
273
|
+
const child2After = childRenders[2]
|
|
274
|
+
|
|
275
|
+
// Change Sword's durability AND bump its version (simulates Fody)
|
|
276
|
+
items[0].Durability = 90
|
|
277
|
+
items[0].Version = 2
|
|
278
|
+
await advanceFrame()
|
|
279
|
+
|
|
280
|
+
// Sword re-renders (version changed)
|
|
281
|
+
expect(childRenders[1]).toBeGreaterThan(child1After)
|
|
282
|
+
// Shield does NOT re-render (version unchanged)
|
|
283
|
+
expect(childRenders[2]).toBe(child2After)
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
it("handles item removal correctly", async () => {
|
|
287
|
+
const items: MockItem[] = [
|
|
288
|
+
createMockItem(1, "Sword", 100),
|
|
289
|
+
createMockItem(2, "Shield", 80),
|
|
290
|
+
createMockItem(3, "Potion", 1),
|
|
291
|
+
]
|
|
292
|
+
const inventory = createMockInventory(items)
|
|
293
|
+
|
|
294
|
+
let parentRenders = 0
|
|
295
|
+
const childRenders: Record<number, number> = {}
|
|
296
|
+
|
|
297
|
+
function ItemView({ item }: { item: MockItem }) {
|
|
298
|
+
const data = useFrameSync(
|
|
299
|
+
() => item,
|
|
300
|
+
(i) => [i.Name, i.Durability]
|
|
301
|
+
)
|
|
302
|
+
childRenders[data.Id] = (childRenders[data.Id] || 0) + 1
|
|
303
|
+
return null
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function InventoryList() {
|
|
307
|
+
const inv = useFrameSync(
|
|
308
|
+
() => inventory,
|
|
309
|
+
(i) => [i.Items.Count]
|
|
310
|
+
)
|
|
311
|
+
parentRenders++
|
|
312
|
+
return (
|
|
313
|
+
<>
|
|
314
|
+
{toArray<MockItem>(inv.Items).map(item => (
|
|
315
|
+
<ItemView key={item.Id} item={item} />
|
|
316
|
+
))}
|
|
317
|
+
</>
|
|
318
|
+
)
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
render(<InventoryList />, container as any)
|
|
322
|
+
await flushMicrotasks()
|
|
323
|
+
await advanceFrame()
|
|
324
|
+
|
|
325
|
+
const parentRendersAfterInit = parentRenders
|
|
326
|
+
expect(childRenders[1]).toBeGreaterThanOrEqual(1)
|
|
327
|
+
expect(childRenders[2]).toBeGreaterThanOrEqual(1)
|
|
328
|
+
expect(childRenders[3]).toBeGreaterThanOrEqual(1)
|
|
329
|
+
|
|
330
|
+
// Remove Shield (index 1)
|
|
331
|
+
items.splice(1, 1)
|
|
332
|
+
await advanceFrame()
|
|
333
|
+
|
|
334
|
+
// Parent should re-render (Count changed from 3 to 2)
|
|
335
|
+
expect(parentRenders).toBeGreaterThan(parentRendersAfterInit)
|
|
336
|
+
})
|
|
337
|
+
|
|
338
|
+
it("nullable parent with optional chaining works end-to-end", async () => {
|
|
339
|
+
let currentPlace: { Name: string; NPCs: { Count: number; [i: number]: { Name: string; Dialogue: string } } } | null = {
|
|
340
|
+
Name: "Tavern",
|
|
341
|
+
NPCs: {
|
|
342
|
+
Count: 2,
|
|
343
|
+
0: { Name: "Barkeep", Dialogue: "Welcome!" },
|
|
344
|
+
1: { Name: "Bard", Dialogue: "La la la~" },
|
|
345
|
+
},
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
let parentRenders = 0
|
|
349
|
+
const npcRenders: Record<string, number> = {}
|
|
350
|
+
|
|
351
|
+
function NPCView({ npc }: { npc: { Name: string; Dialogue: string } }) {
|
|
352
|
+
const data = useFrameSync(
|
|
353
|
+
() => npc,
|
|
354
|
+
(n) => [n.Name, n.Dialogue]
|
|
355
|
+
)
|
|
356
|
+
npcRenders[data.Name] = (npcRenders[data.Name] || 0) + 1
|
|
357
|
+
return null
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function PlaceUI() {
|
|
361
|
+
const place = useFrameSync(
|
|
362
|
+
() => currentPlace,
|
|
363
|
+
(p) => [p?.Name, p?.NPCs?.Count]
|
|
364
|
+
)
|
|
365
|
+
parentRenders++
|
|
366
|
+
|
|
367
|
+
if (!place) return null
|
|
368
|
+
|
|
369
|
+
return (
|
|
370
|
+
<>
|
|
371
|
+
{toArray<{ Name: string; Dialogue: string }>(place.NPCs).map((npc, i) => (
|
|
372
|
+
<NPCView key={npc.Name} npc={npc} />
|
|
373
|
+
))}
|
|
374
|
+
</>
|
|
375
|
+
)
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
render(<PlaceUI />, container as any)
|
|
379
|
+
await flushMicrotasks()
|
|
380
|
+
await advanceFrame()
|
|
381
|
+
|
|
382
|
+
expect(parentRenders).toBeGreaterThanOrEqual(1)
|
|
383
|
+
expect(npcRenders["Barkeep"]).toBeGreaterThanOrEqual(1)
|
|
384
|
+
expect(npcRenders["Bard"]).toBeGreaterThanOrEqual(1)
|
|
385
|
+
|
|
386
|
+
const parentAfterInit = parentRenders
|
|
387
|
+
const barkeepAfter = npcRenders["Barkeep"]
|
|
388
|
+
|
|
389
|
+
// Change Barkeep's dialogue (NPC property change)
|
|
390
|
+
currentPlace!.NPCs[0].Dialogue = "What'll ya have?"
|
|
391
|
+
await advanceFrame()
|
|
392
|
+
|
|
393
|
+
// Parent should NOT re-render (Name and NPC Count unchanged)
|
|
394
|
+
expect(parentRenders).toBe(parentAfterInit)
|
|
395
|
+
// Only Barkeep should re-render
|
|
396
|
+
expect(npcRenders["Barkeep"]).toBeGreaterThan(barkeepAfter)
|
|
397
|
+
|
|
398
|
+
const parentAfterDialogue = parentRenders
|
|
399
|
+
|
|
400
|
+
// Player leaves the tavern (set to null)
|
|
401
|
+
currentPlace = null
|
|
402
|
+
await advanceFrame()
|
|
403
|
+
|
|
404
|
+
// Parent should re-render (place changed to null)
|
|
405
|
+
expect(parentRenders).toBeGreaterThan(parentAfterDialogue)
|
|
406
|
+
})
|
|
407
|
+
|
|
408
|
+
it("multiple property changes in one frame only cause one re-render", async () => {
|
|
409
|
+
const item = createMockItem(1, "Sword", 100)
|
|
410
|
+
|
|
411
|
+
let renderCount = 0
|
|
412
|
+
|
|
413
|
+
function ItemView() {
|
|
414
|
+
const data = useFrameSync(
|
|
415
|
+
() => item,
|
|
416
|
+
(i) => [i.Name, i.Durability, i.StackCount, i.Version]
|
|
417
|
+
)
|
|
418
|
+
renderCount++
|
|
419
|
+
return null
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
render(<ItemView />, container as any)
|
|
423
|
+
await flushMicrotasks()
|
|
424
|
+
await advanceFrame()
|
|
425
|
+
const afterInit = renderCount
|
|
426
|
+
|
|
427
|
+
// Change multiple properties at once (between frames)
|
|
428
|
+
item.Name = "Broken Sword"
|
|
429
|
+
item.Durability = 0
|
|
430
|
+
item.Version = 2
|
|
431
|
+
await advanceFrame()
|
|
432
|
+
|
|
433
|
+
// Should only re-render once for all changes in the same frame
|
|
434
|
+
expect(renderCount).toBe(afterInit + 1)
|
|
435
|
+
})
|
|
436
|
+
})
|
|
@@ -58,7 +58,9 @@ describe('components', () => {
|
|
|
58
58
|
const el = container.children[0] as MockVisualElement;
|
|
59
59
|
expect(getStyleValue(el.style.width)).toBe(200);
|
|
60
60
|
expect(getStyleValue(el.style.height)).toBe(100);
|
|
61
|
-
|
|
61
|
+
// flexDirection 'row' is converted to the Unity enum value FlexDirection.Row
|
|
62
|
+
const FlexDirection = (globalThis as any).CS.UnityEngine.UIElements.FlexDirection;
|
|
63
|
+
expect(el.style.flexDirection).toBe(FlexDirection.Row);
|
|
62
64
|
expect(getStyleValue(el.style.paddingTop)).toBe(10);
|
|
63
65
|
});
|
|
64
66
|
|
|
@@ -663,21 +663,23 @@ describe('host-config', () => {
|
|
|
663
663
|
});
|
|
664
664
|
|
|
665
665
|
describe('visibility', () => {
|
|
666
|
-
it('hideInstance sets display to
|
|
666
|
+
it('hideInstance sets display to DisplayStyle.None', () => {
|
|
667
667
|
const instance = createInstance('ojs-view', {});
|
|
668
668
|
|
|
669
669
|
hideInstance(instance);
|
|
670
670
|
|
|
671
|
-
|
|
671
|
+
const DisplayStyle = (globalThis as any).CS.UnityEngine.UIElements.DisplayStyle;
|
|
672
|
+
expect(instance.element.style.display).toBe(DisplayStyle.None);
|
|
672
673
|
});
|
|
673
674
|
|
|
674
|
-
it('unhideInstance
|
|
675
|
+
it('unhideInstance sets display to DisplayStyle.Flex', () => {
|
|
675
676
|
const instance = createInstance('ojs-view', {});
|
|
676
|
-
|
|
677
|
+
const DisplayStyle = (globalThis as any).CS.UnityEngine.UIElements.DisplayStyle;
|
|
678
|
+
instance.element.style.display = DisplayStyle.None;
|
|
677
679
|
|
|
678
680
|
unhideInstance(instance, {});
|
|
679
681
|
|
|
680
|
-
expect(instance.element.style.display).toBe(
|
|
682
|
+
expect(instance.element.style.display).toBe(DisplayStyle.Flex);
|
|
681
683
|
});
|
|
682
684
|
});
|
|
683
685
|
|
package/src/__tests__/mocks.ts
CHANGED
|
@@ -225,12 +225,17 @@ export class MockImage extends MockVisualElement {
|
|
|
225
225
|
|
|
226
226
|
/**
|
|
227
227
|
* Create the mock CS global object that mirrors QuickJSBootstrap.js proxy
|
|
228
|
+
*
|
|
229
|
+
* Enum values match Unity's actual enum definitions so that tests
|
|
230
|
+
* verify the real mapping behavior (CSS string -> Unity enum number).
|
|
228
231
|
*/
|
|
229
232
|
export function createMockCS() {
|
|
230
233
|
return {
|
|
231
234
|
UnityEngine: {
|
|
232
235
|
// Core types
|
|
233
236
|
Color: MockColor,
|
|
237
|
+
Rect: class { constructor(public x: number, public y: number, public width: number, public height: number) {} },
|
|
238
|
+
ScaleMode: { StretchToFill: 0, ScaleAndCrop: 1, ScaleToFit: 2 },
|
|
234
239
|
// UI Elements
|
|
235
240
|
UIElements: {
|
|
236
241
|
VisualElement: MockVisualElement,
|
|
@@ -246,6 +251,39 @@ export function createMockCS() {
|
|
|
246
251
|
Length: MockLength,
|
|
247
252
|
LengthUnit: MockLengthUnit,
|
|
248
253
|
StyleKeyword: MockStyleKeyword,
|
|
254
|
+
// Enums (values match Unity's actual enum definitions)
|
|
255
|
+
FlexDirection: { Column: 0, ColumnReverse: 1, Row: 2, RowReverse: 3 },
|
|
256
|
+
Wrap: { NoWrap: 0, Wrap: 1, WrapReverse: 2 },
|
|
257
|
+
Align: { Auto: 0, FlexStart: 1, Center: 2, FlexEnd: 3, Stretch: 4 },
|
|
258
|
+
Justify: { FlexStart: 0, Center: 1, FlexEnd: 2, SpaceBetween: 3, SpaceAround: 4 },
|
|
259
|
+
Position: { Relative: 0, Absolute: 1 },
|
|
260
|
+
Overflow: { Visible: 0, Hidden: 1 },
|
|
261
|
+
DisplayStyle: { Flex: 0, None: 1 },
|
|
262
|
+
Visibility: { Visible: 0, Hidden: 1 },
|
|
263
|
+
WhiteSpace: { Normal: 0, NoWrap: 1 },
|
|
264
|
+
TextOverflow: { Clip: 0, Ellipsis: 1 },
|
|
265
|
+
TextOverflowPosition: { End: 0, Start: 1, Middle: 2 },
|
|
266
|
+
OverflowClipBox: { PaddingBox: 0, ContentBox: 1 },
|
|
267
|
+
PickingMode: { Position: 0, Ignore: 1 },
|
|
268
|
+
SliderDirection: { Horizontal: 0, Vertical: 1 },
|
|
269
|
+
// ScrollView enums
|
|
270
|
+
ScrollViewMode: { Vertical: 0, Horizontal: 1, VerticalAndHorizontal: 2 },
|
|
271
|
+
ScrollerVisibility: { Auto: 0, AlwaysVisible: 1, Hidden: 2 },
|
|
272
|
+
TouchScrollBehavior: { Unrestricted: 0, Elastic: 1, Clamped: 2 },
|
|
273
|
+
NestedInteractionKind: { Default: 0, StopScrolling: 1, ForwardScrolling: 2 },
|
|
274
|
+
// ListView enums
|
|
275
|
+
SelectionType: { None: 0, Single: 1, Multiple: 2 },
|
|
276
|
+
ListViewReorderMode: { Simple: 0, Animated: 1 },
|
|
277
|
+
AlternatingRowBackground: { None: 0, ContentOnly: 1, All: 2 },
|
|
278
|
+
CollectionVirtualizationMethod: { FixedHeight: 0, DynamicHeight: 1 },
|
|
279
|
+
},
|
|
280
|
+
},
|
|
281
|
+
OneJS: {
|
|
282
|
+
GPU: {
|
|
283
|
+
GPUBridge: {
|
|
284
|
+
SetElementBackgroundImage: () => {},
|
|
285
|
+
ClearElementBackgroundImage: () => {},
|
|
286
|
+
},
|
|
249
287
|
},
|
|
250
288
|
},
|
|
251
289
|
};
|
|
@@ -323,5 +361,7 @@ export function getEventAPI() {
|
|
|
323
361
|
addEventListener: ReturnType<typeof import('vitest').vi.fn>;
|
|
324
362
|
removeEventListener: ReturnType<typeof import('vitest').vi.fn>;
|
|
325
363
|
removeAllEventListeners: ReturnType<typeof import('vitest').vi.fn>;
|
|
364
|
+
setParent: ReturnType<typeof import('vitest').vi.fn>;
|
|
365
|
+
removeParent: ReturnType<typeof import('vitest').vi.fn>;
|
|
326
366
|
};
|
|
327
367
|
}
|
package/src/__tests__/setup.ts
CHANGED
|
@@ -13,13 +13,13 @@ import { createMockCS, resetAllMocks } from "./mocks";
|
|
|
13
13
|
|
|
14
14
|
// Extend globalThis type for our mocks
|
|
15
15
|
declare global {
|
|
16
|
-
// eslint-disable-next-line no-var
|
|
17
|
-
var CS: ReturnType<typeof createMockCS>;
|
|
18
16
|
// eslint-disable-next-line no-var
|
|
19
17
|
var __eventAPI: {
|
|
20
18
|
addEventListener: ReturnType<typeof vi.fn>;
|
|
21
19
|
removeEventListener: ReturnType<typeof vi.fn>;
|
|
22
20
|
removeAllEventListeners: ReturnType<typeof vi.fn>;
|
|
21
|
+
setParent: ReturnType<typeof vi.fn>;
|
|
22
|
+
removeParent: ReturnType<typeof vi.fn>;
|
|
23
23
|
};
|
|
24
24
|
}
|
|
25
25
|
|
|
@@ -33,13 +33,15 @@ beforeEach(() => {
|
|
|
33
33
|
resetAllMocks();
|
|
34
34
|
|
|
35
35
|
// Create fresh mock CS global
|
|
36
|
-
|
|
36
|
+
(globalThis as any).CS = createMockCS();
|
|
37
37
|
|
|
38
38
|
// Mock event API with spies
|
|
39
39
|
global.__eventAPI = {
|
|
40
40
|
addEventListener: vi.fn(),
|
|
41
41
|
removeEventListener: vi.fn(),
|
|
42
42
|
removeAllEventListeners: vi.fn(),
|
|
43
|
+
setParent: vi.fn(),
|
|
44
|
+
removeParent: vi.fn(),
|
|
43
45
|
};
|
|
44
46
|
|
|
45
47
|
// Use real console but spy on it for test assertions
|
|
@@ -303,10 +303,12 @@ describe("style-parser", () => {
|
|
|
303
303
|
expect(parseStyleValue("flexShrink", 0)).toBe(0)
|
|
304
304
|
})
|
|
305
305
|
|
|
306
|
-
it("
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
expect(parseStyleValue("
|
|
306
|
+
it("converts enum properties to Unity enum values", () => {
|
|
307
|
+
const CS = (globalThis as any).CS
|
|
308
|
+
const UIE = CS.UnityEngine.UIElements
|
|
309
|
+
expect(parseStyleValue("flexDirection", "row")).toBe(UIE.FlexDirection.Row)
|
|
310
|
+
expect(parseStyleValue("display", "none")).toBe(UIE.DisplayStyle.None)
|
|
311
|
+
expect(parseStyleValue("position", "absolute")).toBe(UIE.Position.Absolute)
|
|
310
312
|
})
|
|
311
313
|
|
|
312
314
|
it("passes through unknown properties unchanged", () => {
|
package/src/hooks.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { useState, useEffect, useRef, useReducer } from "react"
|
|
2
2
|
|
|
3
|
+
// QuickJS environment declarations
|
|
4
|
+
declare function requestAnimationFrame(callback: (time: number) => void): number;
|
|
5
|
+
declare function cancelAnimationFrame(id: number): void;
|
|
6
|
+
|
|
3
7
|
/**
|
|
4
8
|
* Syncs a value from C# (or any external source) to React state, checking every frame.
|
|
5
9
|
*
|
package/src/host-config.ts
CHANGED
|
@@ -56,7 +56,10 @@ declare const CS: {
|
|
|
56
56
|
CollectionVirtualizationMethod: CSEnum;
|
|
57
57
|
DisplayStyle: CSEnum;
|
|
58
58
|
PickingMode: CSEnum;
|
|
59
|
+
SliderDirection: CSEnum;
|
|
59
60
|
};
|
|
61
|
+
ScaleMode: CSEnum;
|
|
62
|
+
Rect: new (...args: any[]) => any;
|
|
60
63
|
};
|
|
61
64
|
OneJS: {
|
|
62
65
|
GPU: {
|
|
@@ -578,9 +581,10 @@ function removeMergedTextChild(parentInstance: Instance, child: Instance) {
|
|
|
578
581
|
|
|
579
582
|
// Apply common props (text, value, label)
|
|
580
583
|
function applyCommonProps(element: CSObject, props: Record<string, unknown>) {
|
|
581
|
-
|
|
582
|
-
if (props.
|
|
583
|
-
if (props.
|
|
584
|
+
const el = element as any;
|
|
585
|
+
if (props.text !== undefined) el.text = props.text as string;
|
|
586
|
+
if (props.value !== undefined) el.value = props.value;
|
|
587
|
+
if (props.label !== undefined) el.label = props.label as string;
|
|
584
588
|
}
|
|
585
589
|
|
|
586
590
|
// Helper to set enum prop if defined
|
|
@@ -599,106 +603,62 @@ function setValueProp<T>(target: T, key: keyof T, props: Record<string, unknown>
|
|
|
599
603
|
|
|
600
604
|
// Apply TextField-specific properties
|
|
601
605
|
function applyTextFieldProps(element: CSObject, props: Record<string, unknown>) {
|
|
602
|
-
|
|
603
|
-
if (props.readOnly !== undefined)
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
if (props.
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
if (props.
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
if (props.
|
|
613
|
-
(element as { isPasswordField: boolean }).isPasswordField = props.isPasswordField as boolean;
|
|
614
|
-
}
|
|
615
|
-
if (props.maskChar !== undefined) {
|
|
616
|
-
(element as { maskChar: string }).maskChar = (props.maskChar as string).charAt(0);
|
|
617
|
-
}
|
|
618
|
-
if (props.isDelayed !== undefined) {
|
|
619
|
-
(element as { isDelayed: boolean }).isDelayed = props.isDelayed as boolean;
|
|
620
|
-
}
|
|
621
|
-
if (props.selectAllOnFocus !== undefined) {
|
|
622
|
-
(element as { selectAllOnFocus: boolean }).selectAllOnFocus = props.selectAllOnFocus as boolean;
|
|
623
|
-
}
|
|
624
|
-
if (props.selectAllOnMouseUp !== undefined) {
|
|
625
|
-
(element as { selectAllOnMouseUp: boolean }).selectAllOnMouseUp = props.selectAllOnMouseUp as boolean;
|
|
626
|
-
}
|
|
627
|
-
if (props.hideMobileInput !== undefined) {
|
|
628
|
-
(element as { hideMobileInput: boolean }).hideMobileInput = props.hideMobileInput as boolean;
|
|
629
|
-
}
|
|
630
|
-
if (props.autoCorrection !== undefined) {
|
|
631
|
-
(element as { autoCorrection: boolean }).autoCorrection = props.autoCorrection as boolean;
|
|
632
|
-
}
|
|
606
|
+
const el = element as any;
|
|
607
|
+
if (props.readOnly !== undefined) el.isReadOnly = props.readOnly;
|
|
608
|
+
if (props.multiline !== undefined) el.multiline = props.multiline;
|
|
609
|
+
if (props.maxLength !== undefined) el.maxLength = props.maxLength;
|
|
610
|
+
if (props.isPasswordField !== undefined) el.isPasswordField = props.isPasswordField;
|
|
611
|
+
if (props.maskChar !== undefined) el.maskChar = (props.maskChar as string).charAt(0);
|
|
612
|
+
if (props.isDelayed !== undefined) el.isDelayed = props.isDelayed;
|
|
613
|
+
if (props.selectAllOnFocus !== undefined) el.selectAllOnFocus = props.selectAllOnFocus;
|
|
614
|
+
if (props.selectAllOnMouseUp !== undefined) el.selectAllOnMouseUp = props.selectAllOnMouseUp;
|
|
615
|
+
if (props.hideMobileInput !== undefined) el.hideMobileInput = props.hideMobileInput;
|
|
616
|
+
if (props.autoCorrection !== undefined) el.autoCorrection = props.autoCorrection;
|
|
633
617
|
// Note: placeholder is handled differently in Unity - it's set via the textEdition interface
|
|
634
618
|
// For now we skip it as it requires more complex handling
|
|
635
619
|
}
|
|
636
620
|
|
|
637
621
|
// Apply Slider-specific properties
|
|
638
622
|
function applySliderProps(element: CSObject, props: Record<string, unknown>) {
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
if (props.
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
if (props.
|
|
646
|
-
(element as { showInputField: boolean }).showInputField = props.showInputField as boolean;
|
|
647
|
-
}
|
|
648
|
-
if (props.inverted !== undefined) {
|
|
649
|
-
(element as { inverted: boolean }).inverted = props.inverted as boolean;
|
|
650
|
-
}
|
|
651
|
-
if (props.pageSize !== undefined) {
|
|
652
|
-
(element as { pageSize: number }).pageSize = props.pageSize as number;
|
|
653
|
-
}
|
|
654
|
-
if (props.fill !== undefined) {
|
|
655
|
-
(element as { fill: boolean }).fill = props.fill as boolean;
|
|
656
|
-
}
|
|
623
|
+
const el = element as any;
|
|
624
|
+
if (props.lowValue !== undefined) el.lowValue = props.lowValue;
|
|
625
|
+
if (props.highValue !== undefined) el.highValue = props.highValue;
|
|
626
|
+
if (props.showInputField !== undefined) el.showInputField = props.showInputField;
|
|
627
|
+
if (props.inverted !== undefined) el.inverted = props.inverted;
|
|
628
|
+
if (props.pageSize !== undefined) el.pageSize = props.pageSize;
|
|
629
|
+
if (props.fill !== undefined) el.fill = props.fill;
|
|
657
630
|
if (props.direction !== undefined) {
|
|
658
|
-
|
|
659
|
-
(element as { direction: unknown }).direction = UIE.SliderDirection[props.direction as string];
|
|
631
|
+
el.direction = CS.UnityEngine.UIElements.SliderDirection[props.direction as string];
|
|
660
632
|
}
|
|
661
633
|
}
|
|
662
634
|
|
|
663
635
|
// Apply Toggle-specific properties
|
|
664
636
|
function applyToggleProps(element: CSObject, props: Record<string, unknown>) {
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
if (props.toggleOnLabelClick !== undefined) {
|
|
669
|
-
(element as { toggleOnLabelClick: boolean }).toggleOnLabelClick = props.toggleOnLabelClick as boolean;
|
|
670
|
-
}
|
|
637
|
+
const el = element as any;
|
|
638
|
+
if (props.text !== undefined) el.text = props.text;
|
|
639
|
+
if (props.toggleOnLabelClick !== undefined) el.toggleOnLabelClick = props.toggleOnLabelClick;
|
|
671
640
|
}
|
|
672
641
|
|
|
673
642
|
// Apply Image-specific properties
|
|
674
643
|
function applyImageProps(element: CSObject, props: Record<string, unknown>) {
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
if (props.
|
|
679
|
-
(element as { sprite: unknown }).sprite = props.sprite;
|
|
680
|
-
}
|
|
681
|
-
if (props.vectorImage !== undefined) {
|
|
682
|
-
(element as { vectorImage: unknown }).vectorImage = props.vectorImage;
|
|
683
|
-
}
|
|
644
|
+
const el = element as any;
|
|
645
|
+
if (props.image !== undefined) el.image = props.image;
|
|
646
|
+
if (props.sprite !== undefined) el.sprite = props.sprite;
|
|
647
|
+
if (props.vectorImage !== undefined) el.vectorImage = props.vectorImage;
|
|
684
648
|
if (props.scaleMode !== undefined) {
|
|
685
|
-
|
|
686
|
-
(element as { scaleMode: unknown }).scaleMode = scaleMode;
|
|
649
|
+
el.scaleMode = CS.UnityEngine.ScaleMode[props.scaleMode as string];
|
|
687
650
|
}
|
|
688
651
|
if (props.tintColor !== undefined) {
|
|
689
|
-
// Parse color string to Unity Color
|
|
690
652
|
const color = parseColor(props.tintColor as string);
|
|
691
|
-
if (color)
|
|
692
|
-
(element as { tintColor: unknown }).tintColor = color;
|
|
693
|
-
}
|
|
653
|
+
if (color) el.tintColor = color;
|
|
694
654
|
}
|
|
695
655
|
if (props.sourceRect !== undefined) {
|
|
696
656
|
const rect = props.sourceRect as { x: number; y: number; width: number; height: number };
|
|
697
|
-
|
|
657
|
+
el.sourceRect = new CS.UnityEngine.Rect(rect.x, rect.y, rect.width, rect.height);
|
|
698
658
|
}
|
|
699
659
|
if (props.uv !== undefined) {
|
|
700
660
|
const rect = props.uv as { x: number; y: number; width: number; height: number };
|
|
701
|
-
|
|
661
|
+
el.uv = new CS.UnityEngine.Rect(rect.x, rect.y, rect.width, rect.height);
|
|
702
662
|
}
|
|
703
663
|
}
|
|
704
664
|
|