docs(react-virtual): explain element measurements in unit tests #1369
Openadityareddy-dev(GitHub profile, opens in a new tab)wants to merge 3 commits into
main fromrefs/pull/1369/headopened - docs/framework/react/react-virtual.md lines 171 to 178OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -147,0 +158,21 @@158+import { afterEach, beforeEach, vi } from 'vitest'159+160+beforeEach(() => {161+ vi.spyOn(HTMLElement.prototype, 'clientHeight', 'get').mockImplementation(162+ function (this: HTMLElement) {163+ return this.dataset.testid === 'scroller' ? 200 : 0164+ },165+ )166+ vi.spyOn(HTMLElement.prototype, 'clientWidth', 'get').mockImplementation(167+ function (this: HTMLElement) {168+ return this.dataset.testid === 'scroller' ? 320 : 0169+ },170+ )171+ vi.spyOn(HTMLElement.prototype, 'offsetHeight', 'get').mockImplementation(172+ function (this: HTMLElement) {173+ return this.hasAttribute('data-index') ? 40 : 0174+ },175+ )176+ vi.spyOn(HTMLElement.prototype, 'offsetWidth', 'get').mockImplementation(177+ function (this: HTMLElement) {178+ return this.hasAttribute('data-index') ? 320 : 0docs/framework/react/react-virtual.md line 173OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -147,0 +153,21 @@153+For an element-based list, mock the scroll element's `clientHeight` and `clientWidth`. If your list uses `measureElement`, also mock the items' `offsetHeight` and `offsetWidth` for their initial measurements. Mocking only `getBoundingClientRect` or only `offsetHeight` on the scroll element does not provide its viewport size.154+155+For example, this Vitest setup gives a scroll element with `data-testid="scroller"` a 200 × 320 viewport and its items a height of 40:156+157+```tsx158+import { afterEach, beforeEach, vi } from 'vitest'159+160+beforeEach(() => {161+ vi.spyOn(HTMLElement.prototype, 'clientHeight', 'get').mockImplementation(162+ function (this: HTMLElement) {163+ return this.dataset.testid === 'scroller' ? 200 : 0164+ },165+ )166+ vi.spyOn(HTMLElement.prototype, 'clientWidth', 'get').mockImplementation(167+ function (this: HTMLElement) {168+ return this.dataset.testid === 'scroller' ? 320 : 0169+ },170+ )171+ vi.spyOn(HTMLElement.prototype, 'offsetHeight', 'get').mockImplementation(172+ function (this: HTMLElement) {173+ return this.hasAttribute('data-index') ? 40 : 0
- docs/framework/react/react-virtual.md lines 188 to 190Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -147,0 +170,21 @@170+ )171+ vi.spyOn(HTMLElement.prototype, 'offsetHeight', 'get').mockImplementation(172+ function (this: HTMLElement) {173+ return this.matches('[data-testid="scroller"] [data-index]') ? 40 : 0174+ },175+ )176+ vi.spyOn(HTMLElement.prototype, 'offsetWidth', 'get').mockImplementation(177+ function (this: HTMLElement) {178+ return this.matches('[data-testid="scroller"] [data-index]') ? 320 : 0179+ },180+ )181+})182+183+afterEach(() => {184+ vi.restoreAllMocks()185+})186+```187+188+Scope the mocks to your list so unrelated elements keep their default measurements. The item selectors use the default `data-index` attribute. If you set a custom `indexAttribute`, replace `data-index` with that attribute. Restore the getters after each test to avoid affecting other tests. If your test environment does not provide `ResizeObserver`, supply a mock or polyfill as well. To test later item resizes, deliver `ResizeObserver` entries with the corresponding `borderBoxSize`.189+190+Alternatively, pass a custom [`observeElementRect`](../../api/virtualizer.md#observeelementrect) to report a viewport size without mocking its DOM getters. For dynamically measured items, you can also pass a custom [`measureElement`](../../api/virtualizer.md#measureelement). Use browser tests when you need real layout, scrolling, or resize behavior.
Sign in to join this conversation.
⚠️ No Changeset found#
Latest commit: 4c64baf6e0cf0e01e631477ae522ec453bcbbe28
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
This PR includes no changesets
When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR