-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: resolving issues with virtual lists having zero height in releas…
…e summary
- Loading branch information
Showing
2 changed files
with
130 additions
and
76 deletions.
There are no files selected for viewing
This file contains 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
38 changes: 38 additions & 0 deletions
38
packages/sanity/test/testUtils/setupVirtualisedEnvironment.ts
This file contains 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,38 @@ | ||
import {afterEach, beforeEach, vi} from 'vitest' | ||
|
||
export const setupVirtualisedEnvironment = ( | ||
dataTestId?: string, | ||
rectWidth: number = 350, | ||
rectHeight: number = 800, | ||
) => { | ||
const originalGetBoundingClientRect = Element.prototype.getBoundingClientRect | ||
|
||
const getDOMRect = (width: number, height: number) => ({ | ||
width, | ||
height, | ||
top: 0, | ||
left: 0, | ||
bottom: 0, | ||
right: 0, | ||
x: 0, | ||
y: 0, | ||
toJSON: () => {}, | ||
}) | ||
|
||
beforeEach(() => { | ||
// Virtual list will return an empty list of items unless we have some size, | ||
// so we need to mock getBoundingClientRect to return a size for the list. | ||
// Not pretty, but it's what they recommend for testing outside of browsers: | ||
// https://github.com/TanStack/virtual/issues/641 | ||
Element.prototype.getBoundingClientRect = vi.fn(function (this: Element) { | ||
if (!dataTestId || this.getAttribute('data-testid') === dataTestId) { | ||
return getDOMRect(rectWidth, rectHeight) | ||
} | ||
return getDOMRect(0, 0) | ||
}) | ||
}) | ||
|
||
afterEach(() => { | ||
Element.prototype.getBoundingClientRect = originalGetBoundingClientRect | ||
}) | ||
} |