-
-
Notifications
You must be signed in to change notification settings - Fork 466
test: add solid-virtual tests #1286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gameroman
wants to merge
1
commit into
TanStack:main
Choose a base branch
from
gameroman:add-solid-virtual-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+95
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { expect, test } from 'vitest' | ||
| import { createRoot, createSignal } from 'solid-js' | ||
|
|
||
| import { createVirtualizer } from '../src/index' | ||
|
|
||
| test('virtual items correctly index into filtered data after reactive shrink', () => { | ||
| createRoot((dispose) => { | ||
| const all = ['apple', 'banana', 'cherry', 'date', 'fig', 'grape'] | ||
| const [query, setQuery] = createSignal('') | ||
|
|
||
| const filtered = () => { | ||
| const q = query() | ||
| if (!q) return all | ||
| return all.filter(s => s.includes(q)) | ||
| } | ||
|
|
||
| const virtualizer = createVirtualizer<HTMLDivElement, HTMLDivElement>({ | ||
| get count() { | ||
| return filtered().length | ||
| }, | ||
| getScrollElement: () => null, | ||
| estimateSize: () => 60, | ||
| initialRect: { width: 800, height: 600 }, | ||
| }) | ||
|
|
||
| expect(virtualizer.getVirtualItems().length).toBe(6) | ||
| expect(virtualizer.getVirtualItems().map(v => filtered()[v.index])) | ||
| .toEqual(['apple', 'banana', 'cherry', 'date', 'fig', 'grape']) | ||
|
|
||
| setQuery('e') | ||
|
|
||
| expect(virtualizer.getVirtualItems().length).toBe(4) | ||
| const items = virtualizer.getVirtualItems() | ||
| expect(items.map(v => filtered()[v.index])) | ||
| .toEqual(['apple', 'cherry', 'date', 'grape']) | ||
|
|
||
| setQuery('zz') | ||
|
|
||
| expect(virtualizer.getVirtualItems().length).toBe(0) | ||
| expect(virtualizer.getVirtualItems().map(v => filtered()[v.index])) | ||
| .toEqual([]) | ||
|
|
||
| setQuery('cherry') | ||
|
|
||
| expect(virtualizer.getVirtualItems().length).toBe(1) | ||
| expect(virtualizer.getVirtualItems().map(v => filtered()[v.index])) | ||
| .toEqual(['cherry']) | ||
|
|
||
| dispose() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { createRoot, createSignal } from 'solid-js' | ||
| import { unwrap } from 'solid-js/store' | ||
| import { createVirtualizer } from '../src/index' | ||
|
|
||
| describe('reactivity: unaffected slots keep stable references', () => { | ||
| it('does not recreate VirtualItem objects for slots whose data did not change', () => { | ||
| createRoot((dispose) => { | ||
| const data = Array.from({ length: 50 }, (_, i) => `item-${i}`) | ||
| const [filtered, setFiltered] = createSignal(data) | ||
|
|
||
| const virtualizer = createVirtualizer({ | ||
| get count() { | ||
| return filtered().length | ||
| }, | ||
| getScrollElement: () => document.createElement('div'), | ||
| estimateSize: () => 30, | ||
| overscan: 0, | ||
| }) | ||
|
|
||
| const before = virtualizer.getVirtualItems() | ||
| const beforeRefs = before.map((item) => unwrap(item)) | ||
|
|
||
| // Shrink the array; leaves the first visible rows' index/start/end/size untouched. | ||
| setFiltered(data.slice(0, 40)) | ||
|
|
||
| const after = virtualizer.getVirtualItems() | ||
| const afterRefs = after.map((item) => unwrap(item)) | ||
|
|
||
| const unaffectedCount = Math.min(beforeRefs.length, afterRefs.length) | ||
| for (let i = 0; i < unaffectedCount; i++) { | ||
| if ( | ||
| beforeRefs[i].start === afterRefs[i].start && | ||
| beforeRefs[i].end === afterRefs[i].end && | ||
| beforeRefs[i].index === afterRefs[i].index | ||
| ) { | ||
| expect(afterRefs[i]).toBe(beforeRefs[i]) | ||
| } | ||
| } | ||
|
|
||
| dispose() | ||
| }) | ||
| }) | ||
| }) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Use a non-zero viewport for this test.
This callback creates an unmounted element with zero height. The core returns no virtual range for a zero-sized viewport, so both reference arrays can be empty and the loop makes no identity assertion. (raw.githubusercontent.com)
Return
nulland setinitialRect, or use one stable sized element. Also assert thatunaffectedCountis greater than zero.Proposed fix
🤖 Prompt for AI Agents