Skip to content

Commit f40ad74

Browse files
committed
test(core): assert reference identity and cloning at scaling boundaries
1 parent 431a45e commit f40ad74

1 file changed

Lines changed: 48 additions & 9 deletions

File tree

packages/virtual-core/tests/index.test.ts

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,25 +2655,64 @@ test('getVirtualItems should return physical coordinates when scaling is active'
26552655
expect(firstItem.index).toBe(firstMeasurement.index)
26562656
expect(firstItem.key).toBe(firstMeasurement.key)
26572657
expect(firstItem.lane).toBe(firstMeasurement.lane)
2658+
2659+
// When scaling is active, items must be new objects (not same reference)
2660+
expect(firstItem).not.toBe(firstMeasurement)
26582661
})
26592662

26602663
test('getVirtualItems should return references (not copies) when scale is 1', () => {
2664+
const mockScrollElement = {
2665+
scrollTop: 0,
2666+
scrollLeft: 0,
2667+
scrollWidth: 400,
2668+
scrollHeight: 500,
2669+
clientWidth: 400,
2670+
clientHeight: 400,
2671+
offsetWidth: 400,
2672+
offsetHeight: 400,
2673+
ownerDocument: {
2674+
defaultView: {
2675+
requestAnimationFrame: vi.fn(),
2676+
cancelAnimationFrame: vi.fn(),
2677+
performance: { now: () => Date.now() },
2678+
ResizeObserver: vi.fn(() => ({
2679+
observe: vi.fn(),
2680+
unobserve: vi.fn(),
2681+
disconnect: vi.fn(),
2682+
})),
2683+
},
2684+
},
2685+
scrollTo: vi.fn(),
2686+
} as unknown as HTMLDivElement
2687+
26612688
const virtualizer = new Virtualizer({
26622689
count: 10,
26632690
estimateSize: () => 50,
2664-
getScrollElement: () => null,
2691+
getScrollElement: () => mockScrollElement,
26652692
scrollToFn: vi.fn(),
2666-
observeElementRect: vi.fn(),
2667-
observeElementOffset: vi.fn(),
2693+
observeElementRect: (_instance, cb) => {
2694+
cb({ width: 400, height: 400 })
2695+
return () => {}
2696+
},
2697+
observeElementOffset: (_instance, cb) => {
2698+
cb(0, false)
2699+
return () => {}
2700+
},
26682701
})
26692702

2670-
// No scaling — items should be same references as measurements
2671-
const measurements = virtualizer['getMeasurements']()
2672-
// Without a scroll element, getVirtualItems returns []
2673-
// so let's test via the measurements directly
2703+
virtualizer._willUpdate()
2704+
26742705
expect(virtualizer.scale).toBe(1)
2675-
// Verify the total size matches (no scaling applied)
2676-
expect(virtualizer.getTotalSize()).toBe(500)
2706+
2707+
const measurements = virtualizer['getMeasurements']()
2708+
const items = virtualizer.getVirtualItems()
2709+
2710+
expect(items.length).toBeGreaterThan(0)
2711+
2712+
// Verify strict reference equality (===) between returned items and measurements
2713+
items.forEach((item) => {
2714+
expect(item).toBe(measurements[item.index])
2715+
})
26772716
})
26782717

26792718
test('scroll offset should be upscaled from physical to virtual', () => {

0 commit comments

Comments
 (0)