From b6d1626f99aec2ac0ea9c1355bdcab5601477c3a Mon Sep 17 00:00:00 2001 From: Brian Ingles Date: Wed, 10 Apr 2024 15:36:37 -0500 Subject: [PATCH] Fixed tests (#1909) --- .../src/styleguide/StyleGuide.test.tsx | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/packages/code-studio/src/styleguide/StyleGuide.test.tsx b/packages/code-studio/src/styleguide/StyleGuide.test.tsx index f238df8847..d7d6853243 100644 --- a/packages/code-studio/src/styleguide/StyleGuide.test.tsx +++ b/packages/code-studio/src/styleguide/StyleGuide.test.tsx @@ -9,29 +9,41 @@ 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 + */ +function mockListViewDimension(value: number) { + return 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; + }; +} + describe(' mounts', () => { test('h1 text of StyleGuide renders', () => { // 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(() => 1000); + .mockImplementation(mockListViewDimension(1000)); jest .spyOn(window.HTMLElement.prototype, 'clientHeight', 'get') - .mockImplementation(() => 1000); + .mockImplementation(mockListViewDimension(1000)); jest .spyOn(window.HTMLElement.prototype, 'scrollHeight', 'get') - .mockImplementation(() => 40); + .mockImplementation(mockListViewDimension(40)); jest .spyOn(window.HTMLElement.prototype, 'scrollWidth', 'get') - .mockImplementation(() => 40); + .mockImplementation(mockListViewDimension(40)); expect(() => render(