From 86f792705c38a605032cb6b5d145063dcb518763 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aar=C3=B3n=20Garc=C3=ADa=20Herv=C3=A1s?= Date: Fri, 26 Jul 2024 17:50:53 +0200 Subject: [PATCH 1/5] Fix batch of tests depending on specific React version --- .../test-utils/src/createRenderer.tsx | 2 +- .../test-utils/src/fireDiscreteEvent.ts | 2 +- packages-internal/test-utils/src/userEvent.ts | 6 +++-- .../useAutocomplete/useAutocomplete.test.js | 12 ++++++---- .../src/Autocomplete/Autocomplete.test.tsx | 20 +++++++++------- .../src/Autocomplete/Autocomplete.test.js | 24 ++++++++++--------- .../src/InputBase/InputBase.test.js | 6 +++-- .../src/ListItem/ListItem.test.js | 4 +++- packages/mui-material/src/Menu/Menu.test.js | 4 +++- .../mui-material/src/Popover/Popover.test.js | 12 ++++++---- .../mui-material/src/Select/Select.test.js | 6 +++-- packages/mui-material/src/Tabs/Tabs.test.js | 6 +++-- .../src/internal/SwitchBase.test.js | 20 +++++++++++----- .../useAutocomplete/useAutocomplete.test.js | 12 ++++++---- 14 files changed, 83 insertions(+), 53 deletions(-) diff --git a/packages-internal/test-utils/src/createRenderer.tsx b/packages-internal/test-utils/src/createRenderer.tsx index 170c179a7a862c..aeb3381663f187 100644 --- a/packages-internal/test-utils/src/createRenderer.tsx +++ b/packages-internal/test-utils/src/createRenderer.tsx @@ -568,7 +568,7 @@ export function createRenderer(globalOptions: CreateRendererOptions = {}): Rende wrapper: InnerWrapper = React.Fragment, } = options; - const usesLegacyRoot = !React.version.startsWith('18'); + const usesLegacyRoot = Number(React.version.split('.')[0]) < 18; const Mode = strict && (strictEffects || usesLegacyRoot) ? React.StrictMode : React.Fragment; return function Wrapper({ children }: { children?: React.ReactNode }) { return ( diff --git a/packages-internal/test-utils/src/fireDiscreteEvent.ts b/packages-internal/test-utils/src/fireDiscreteEvent.ts index 65157f9acf9689..a171f45c025dc0 100644 --- a/packages-internal/test-utils/src/fireDiscreteEvent.ts +++ b/packages-internal/test-utils/src/fireDiscreteEvent.ts @@ -8,7 +8,7 @@ const noWrapper = (callback: () => void) => callback(); * @returns {void} */ function withMissingActWarningsIgnored(callback: () => void) { - if (React.version.startsWith('18')) { + if (Number(React.version.split('.')[0]) >= 18) { callback(); return; } diff --git a/packages-internal/test-utils/src/userEvent.ts b/packages-internal/test-utils/src/userEvent.ts index 7bf5cd34950572..4da5bf2db16c00 100644 --- a/packages-internal/test-utils/src/userEvent.ts +++ b/packages-internal/test-utils/src/userEvent.ts @@ -2,6 +2,8 @@ import * as React from 'react'; import { click, mouseDown, mouseUp, keyDown, keyUp } from './fireDiscreteEvent'; import { act, fireEvent } from './createRenderer'; +const reactVersion = Number(React.version.split('.')[0]); + export function touch(target: Element): void { fireEvent.touchStart(target); fireEvent.touchEnd(target); @@ -11,7 +13,7 @@ export const mousePress: (...args: Parameters<(typeof fireEvent)['mouseUp']>) => target, options, ) => { - if (React.version.startsWith('18')) { + if (reactVersion >= 18) { fireEvent.mouseDown(target, options); fireEvent.mouseUp(target, options); fireEvent.click(target, options); @@ -24,7 +26,7 @@ export const mousePress: (...args: Parameters<(typeof fireEvent)['mouseUp']>) => }; export function keyPress(target: Element, options: { key: string; [key: string]: any }): void { - if (React.version.startsWith('18')) { + if (reactVersion >= 18) { fireEvent.keyDown(target, options); fireEvent.keyUp(target, options); } else { diff --git a/packages/mui-base/src/useAutocomplete/useAutocomplete.test.js b/packages/mui-base/src/useAutocomplete/useAutocomplete.test.js index 72ac62297340d0..9c6657b3cfb911 100644 --- a/packages/mui-base/src/useAutocomplete/useAutocomplete.test.js +++ b/packages/mui-base/src/useAutocomplete/useAutocomplete.test.js @@ -4,6 +4,8 @@ import { createRenderer, screen, ErrorBoundary, act, fireEvent } from '@mui/inte import { spy } from 'sinon'; import { useAutocomplete, createFilterOptions } from '@mui/base/useAutocomplete'; +const reactVersion = Number(React.version.split('.')[0]); + describe('useAutocomplete', () => { const { render } = createRenderer(); @@ -289,14 +291,14 @@ describe('useAutocomplete', () => { 'MUI: Unable to find the input element.', errorMessage, // strict effects runs effects twice - React.version.startsWith('18') && 'MUI: Unable to find the input element.', - React.version.startsWith('18') && errorMessage, + reactVersion >= 18 && 'MUI: Unable to find the input element.', + reactVersion >= 18 && errorMessage, 'The above error occurred in the