Skip to content

Commit db4a1a7

Browse files
committed
test(core): assert reference identity and cloning at scaling boundaries
1 parent bfc5974 commit db4a1a7

1 file changed

Lines changed: 55 additions & 14 deletions

File tree

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

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2846,25 +2846,64 @@ test('getVirtualItems should return physical coordinates when scaling is active'
28462846
expect(firstItem.index).toBe(firstMeasurement.index)
28472847
expect(firstItem.key).toBe(firstMeasurement.key)
28482848
expect(firstItem.lane).toBe(firstMeasurement.lane)
2849+
2850+
// When scaling is active, items must be new objects (not same reference)
2851+
expect(firstItem).not.toBe(firstMeasurement)
28492852
})
28502853

28512854
test('getVirtualItems should return references (not copies) when scale is 1', () => {
2855+
const mockScrollElement = {
2856+
scrollTop: 0,
2857+
scrollLeft: 0,
2858+
scrollWidth: 400,
2859+
scrollHeight: 500,
2860+
clientWidth: 400,
2861+
clientHeight: 400,
2862+
offsetWidth: 400,
2863+
offsetHeight: 400,
2864+
ownerDocument: {
2865+
defaultView: {
2866+
requestAnimationFrame: vi.fn(),
2867+
cancelAnimationFrame: vi.fn(),
2868+
performance: { now: () => Date.now() },
2869+
ResizeObserver: vi.fn(() => ({
2870+
observe: vi.fn(),
2871+
unobserve: vi.fn(),
2872+
disconnect: vi.fn(),
2873+
})),
2874+
},
2875+
},
2876+
scrollTo: vi.fn(),
2877+
} as unknown as HTMLDivElement
2878+
28522879
const virtualizer = new Virtualizer({
28532880
count: 10,
28542881
estimateSize: () => 50,
2855-
getScrollElement: () => null,
2882+
getScrollElement: () => mockScrollElement,
28562883
scrollToFn: vi.fn(),
2857-
observeElementRect: vi.fn(),
2858-
observeElementOffset: vi.fn(),
2884+
observeElementRect: (_instance, cb) => {
2885+
cb({ width: 400, height: 400 })
2886+
return () => {}
2887+
},
2888+
observeElementOffset: (_instance, cb) => {
2889+
cb(0, false)
2890+
return () => {}
2891+
},
28592892
})
28602893

2861-
// No scaling — items should be same references as measurements
2862-
const measurements = virtualizer['getMeasurements']()
2863-
// Without a scroll element, getVirtualItems returns []
2864-
// so let's test via the measurements directly
2894+
virtualizer._willUpdate()
2895+
28652896
expect(virtualizer.scale).toBe(1)
2866-
// Verify the total size matches (no scaling applied)
2867-
expect(virtualizer.getTotalSize()).toBe(500)
2897+
2898+
const measurements = virtualizer['getMeasurements']()
2899+
const items = virtualizer.getVirtualItems()
2900+
2901+
expect(items.length).toBeGreaterThan(0)
2902+
2903+
// Verify strict reference equality (===) between returned items and measurements
2904+
items.forEach((item) => {
2905+
expect(item).toBe(measurements[item.index])
2906+
})
28682907
})
28692908

28702909
test('scroll offset should be upscaled from physical to virtual', () => {
@@ -3009,10 +3048,12 @@ test('resize anchoring should work correctly with scaling active', () => {
30093048
const calledAdjustments = (scrollToFn.mock.calls[0]![1] as any)
30103049
?.adjustments as number | undefined
30113050
const totalPhysical = calledPhysicalOffset + (calledAdjustments ?? 0)
3012-
// The adjustment (delta=50 in virtual, 50/scale in physical) should be reflected
3051+
3052+
// Adjustment uses the post-resize scale since cache updates before the scroll write.
3053+
const newScale = virtualizer.scale
3054+
expect(newScale).not.toBe(scale)
3055+
const virtualScroll = physicalScroll * scale
3056+
const expectedPhysical = (virtualScroll + 50) / newScale
30133057
expect(totalPhysical).toBeGreaterThan(physicalScroll)
3014-
expect(totalPhysical).toBeCloseTo(
3015-
physicalScroll + 50 / scale,
3016-
0,
3017-
)
3058+
expect(totalPhysical).toBeCloseTo(expectedPhysical, 0)
30183059
})

0 commit comments

Comments
 (0)