TanStack / virtual

🤖 Headless UI for Virtualizing Large Element Lists in JS/TS, React, Solid, Vue and Svelte
https://tanstack.com/virtual
MIT License
5.23k stars 269 forks source link

useVirtualizer not working in Vue Test Utils #709

Open Fedezable opened 2 months ago

Fedezable commented 2 months ago

Describe the bug

The virtualizer doesn't seem to work when used in Vue Test Utils.

Your minimal, reproducible example

-

Steps to reproduce

  1. Create a new vite app with vue + ts template npm create vite@latest

  2. Install vitest and vue test utils npm i vitest npm i @vue/test-utils

  3. Create a new component based on any vue tanstack virtual example, I tried with: -> RowVirtualizerFixed.vue in fixed example

  4. Create a new basic test file RowVirtualizerFixed.test.ts and log the wrapper's html:

import { mount } from '@vue/test-utils'
import { describe, it } from 'vitest'
import VirtualRowsExample from './VirtualRowsExample.vue'

describe('VirtualRowsExample', () => {
  it('should render virtualized rows', () => {
    const wrapper = mount(VirtualRowsExample)
    console.log(wrapper.html())
  })
})
  1. Run the tests
  2. As you will see the wrapper won't print any html where the rows should be visible

Looks like rowVirtualizer.value.getVirtualItems is not returning any row

<div class="List" style="height: 200px; width: 400px; overflow: auto;">
  <div style="height: 350000px; width: 100%; position: relative;"></div>
</div>

The component looks fine when used in the app

Expected behavior

Some Rows should be printed in wrapper's html

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

Windows

tanstack-virtual version

3.0.4

TypeScript version

4.9.3

Additional context

No response

Terms & Code of Conduct

Zertz commented 1 month ago

For us, the last working version is 3.0.0-beta.62. Every version from 3.0.0-beta.63 to 3.5.0 fails our tests.

piecyk commented 1 month ago

This is expected as in jsdom environment getBoundingClientRect will return height 0 for scroller element. There few options we can tackle this, one is to mock getBoundingClientRect using vi.fn have a look at https://github.com/TanStack/virtual/issues/641#issuecomment-1902239048