From 342b24cfc94f848613c989b78525cf5053899807 Mon Sep 17 00:00:00 2001 From: Brian Ingles Date: Wed, 10 Apr 2024 15:59:16 -0500 Subject: [PATCH] Cleanup (#1909) --- .../src/styleguide/StyleGuide.test.tsx | 43 ++++++++----------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/packages/code-studio/src/styleguide/StyleGuide.test.tsx b/packages/code-studio/src/styleguide/StyleGuide.test.tsx index d7d6853243..28355f2637 100644 --- a/packages/code-studio/src/styleguide/StyleGuide.test.tsx +++ b/packages/code-studio/src/styleguide/StyleGuide.test.tsx @@ -9,20 +9,20 @@ import StyleGuide from './StyleGuide'; window.HTMLElement.prototype.scroll = jest.fn(); window.HTMLElement.prototype.scrollIntoView = jest.fn(); -/* - * React Spectrum `useVirtualizerItem` depends on `scrollWidth` and `scrollHeight`. - * Mocking these to avoid React "Maximum update depth exceeded" errors. - * https://github.com/adobe/react-spectrum/blob/0b2a838b36ad6d86eee13abaf68b7e4d2b4ada6c/packages/%40react-aria/virtualizer/src/useVirtualizerItem.ts#L49C3-L49C60 +/** + * Mock a dimension property of a ListView element. */ -function mockListViewDimension(value: number) { - return function getDimension() { - const isSpectrumListView = - this instanceof HTMLElement && - this.className.includes('_react-spectrum-ListView'); +function mockListViewDimension(propName: keyof HTMLElement, value: number) { + jest + .spyOn(window.HTMLElement.prototype, propName, 'get') + .mockImplementation(function getDimension() { + const isSpectrumListView = + this instanceof HTMLElement && + this.className.includes('_react-spectrum-ListView'); - // For non ListView, just return zero which is the default value anyway. - return isSpectrumListView === true ? value : 0; - }; + // For non ListView, just return zero which is the default value anyway. + return isSpectrumListView === true ? value : 0; + }); } describe(' mounts', () => { @@ -30,20 +30,15 @@ describe(' mounts', () => { // Provide a non-null array to ThemeProvider to tell it to initialize const customThemes: ThemeData[] = []; + // React Spectrum `useVirtualizerItem` depends on `scrollWidth` and `scrollHeight`. + // Mocking these to avoid React "Maximum update depth exceeded" errors. + // https://github.com/adobe/react-spectrum/blob/0b2a838b36ad6d86eee13abaf68b7e4d2b4ada6c/packages/%40react-aria/virtualizer/src/useVirtualizerItem.ts#L49C3-L49C60 // From preview docs: https://reactspectrum.blob.core.windows.net/reactspectrum/726a5e8f0ed50fc8d98e39c74bd6dfeb3660fbdf/docs/react-spectrum/testing.html#virtualized-components // The virtualizer will now think it has a visible area of 1000px x 1000px and that the items within it are 40px x 40px - jest - .spyOn(window.HTMLElement.prototype, 'clientWidth', 'get') - .mockImplementation(mockListViewDimension(1000)); - jest - .spyOn(window.HTMLElement.prototype, 'clientHeight', 'get') - .mockImplementation(mockListViewDimension(1000)); - jest - .spyOn(window.HTMLElement.prototype, 'scrollHeight', 'get') - .mockImplementation(mockListViewDimension(40)); - jest - .spyOn(window.HTMLElement.prototype, 'scrollWidth', 'get') - .mockImplementation(mockListViewDimension(40)); + mockListViewDimension('clientWidth', 1000); + mockListViewDimension('clientHeight', 1000); + mockListViewDimension('scrollHeight', 40); + mockListViewDimension('scrollWidth', 40); expect(() => render(