Skip to content

Commit 9a9fb4f

Browse files
authored
fix: Fix Rect intersection check so that users dont need to provide Virtualizer mocks for tests (#8396)
* Fix Rect intersection check so users dont have to mock scrollHeight in tests * update docs * properly turn in virt for our tests
1 parent 7be72f1 commit 9a9fb4f

File tree

6 files changed

+20
-7
lines changed

6 files changed

+20
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"build:s2-docs": "NODE_ENV=storybook storybook build -c .storybook-s2 --docs",
2626
"start:docs": "DOCS_ENV=dev parcel 'packages/@react-{spectrum,aria,stately}/*/docs/*.mdx' 'packages/react-aria-components/docs/**/*.mdx' 'packages/@internationalized/*/docs/*.mdx' 'packages/dev/docs/pages/**/*.mdx'",
2727
"build:docs": "DOCS_ENV=staging parcel build 'packages/@react-{spectrum,aria,stately}/*/docs/*.mdx' 'packages/react-aria-components/docs/**/*.mdx' 'packages/@internationalized/*/docs/*.mdx' 'packages/dev/docs/pages/**/*.mdx'",
28-
"test": "cross-env STRICT_MODE=1 yarn jest",
28+
"test": "cross-env STRICT_MODE=1 VIRT_ON=1 yarn jest",
2929
"test:lint": "node packages/**/*.test-lint.js",
3030
"test-loose": "cross-env VIRT_ON=1 yarn jest",
3131
"test-storybook": "test-storybook --url http://localhost:9003 --browsers chromium --no-cache",

packages/@react-spectrum/color/test/ColorPicker.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ describe('ColorPicker', function () {
2222

2323
beforeAll(() => {
2424
user = userEvent.setup({delay: null, pointerMap});
25+
jest.spyOn(window.HTMLElement.prototype, 'clientWidth', 'get').mockImplementation(() => 1000);
26+
jest.spyOn(window.HTMLElement.prototype, 'clientHeight', 'get').mockImplementation(() => 1000);
2527
jest.spyOn(window.HTMLElement.prototype, 'scrollHeight', 'get').mockImplementation(() => 50);
2628
jest.useFakeTimers();
2729
});

packages/@react-spectrum/picker/test/TempUtilTest.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ describe('Picker/Select ', function () {
2727

2828
beforeAll(function () {
2929
user = userEvent.setup({delay: null, pointerMap});
30+
jest.spyOn(window.HTMLElement.prototype, 'clientWidth', 'get').mockImplementation(() => 1000);
31+
jest.spyOn(window.HTMLElement.prototype, 'clientHeight', 'get').mockImplementation(() => 1000);
3032
jest.spyOn(window.HTMLElement.prototype, 'scrollHeight', 'get').mockImplementation(() => 50);
3133
simulateDesktop();
3234
});

packages/@react-spectrum/table/test/TestTableUtils.test.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ describe('Table ', function () {
3737
let onSortChange = jest.fn();
3838
let testUtilRealTimer = new User({advanceTimer: (waitTime) => new Promise((resolve) => setTimeout(resolve, waitTime))});
3939

40+
beforeAll(function () {
41+
jest.spyOn(window.HTMLElement.prototype, 'clientWidth', 'get').mockImplementation(() => 1000);
42+
jest.spyOn(window.HTMLElement.prototype, 'clientHeight', 'get').mockImplementation(() => 1000);
43+
jest.spyOn(window.HTMLElement.prototype, 'scrollHeight', 'get').mockImplementation(() => 50);
44+
});
45+
4046
let TableExample = (props) => {
4147
let [sort, setSort] = useState({});
4248
let setSortDescriptor = (sort) => {

packages/@react-stately/virtualizer/src/Rect.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ export class Rect {
9292
* @param rect - The rectangle to check.
9393
*/
9494
intersects(rect: Rect): boolean {
95-
return this.area > 0
96-
&& rect.area > 0
97-
&& this.x <= rect.x + rect.width
98-
&& rect.x <= this.x + this.width
99-
&& this.y <= rect.y + rect.height
100-
&& rect.y <= this.y + this.height;
95+
let isTestEnv = process.env.NODE_ENV === 'test' && !process.env.VIRT_ON;
96+
return (isTestEnv || this.area > 0 && rect.area > 0)
97+
&& this.x <= rect.x + rect.width
98+
&& rect.x <= this.x + this.width
99+
&& this.y <= rect.y + rect.height
100+
&& rect.y <= this.y + this.height;
101101
}
102102

103103
/**

packages/dev/docs/pages/react-spectrum/testing.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ afterAll(() => {
253253

254254
### Virtualized components
255255

256+
**Note:** As of [3.41](https://www.npmjs.com/package/@adobe/react-spectrum/v/3.41.0), Virtualizer behavior is disabled in tests automatically so the below mocks are now unnecessary for most test cases. However, if you are writing tests for interactions
257+
that depend on item size calculations such as scroll into view, you will need to provide the mocks below as well as enable Virtualizer via the `VIRT_ON=1` process environment variable when running your tests.
258+
256259
Many of our collection components are virtualized out of the box and thus rely on DOM node measurement to know how large a item should be and how many items can be rendered at once.
257260
To properly render virtualized components in a test environment, you'll have to to mock the `clientHeight` and `clientWidth` getters of the `HTMLElement` prototype.
258261
`scrollHeight` and `scrollWidth` should also be mocked to set the height and width of each item. See below for an example implementation in Jest.

0 commit comments

Comments
 (0)