diff --git a/packages/react/src/floating-ui-react/components/FloatingPortal.test.tsx b/packages/react/src/floating-ui-react/components/FloatingPortal.test.tsx index a4ee6c681a..a879524ef9 100644 --- a/packages/react/src/floating-ui-react/components/FloatingPortal.test.tsx +++ b/packages/react/src/floating-ui-react/components/FloatingPortal.test.tsx @@ -1,13 +1,14 @@ -import { fireEvent, flushMicrotasks, render, screen } from '@mui/internal-test-utils'; import * as React from 'react'; - +import { fireEvent, flushMicrotasks, render, screen } from '@mui/internal-test-utils'; import { isJSDOM } from '@base-ui-components/utils/detectBrowser'; import { FloatingPortal, useFloating } from '../index'; +import type { UseFloatingPortalNodeProps } from './FloatingPortal'; + +interface AppProps { + root?: UseFloatingPortalNodeProps['root']; +} -function App(props: { - root?: HTMLElement | null | React.RefObject; - id?: string; -}) { +function App(props: AppProps) { const [open, setOpen] = React.useState(false); const { refs } = useFloating({ open, @@ -25,30 +26,6 @@ function App(props: { } describe.skipIf(!isJSDOM)('FloatingPortal', () => { - test('creates a custom id node', async () => { - render(); - await flushMicrotasks(); - expect(document.querySelector('#custom-id')).toBeInTheDocument(); - }); - - test('uses a custom id node as the root', async () => { - const customRoot = document.createElement('div'); - customRoot.id = 'custom-root'; - document.body.appendChild(customRoot); - render(); - fireEvent.click(screen.getByTestId('reference')); - await flushMicrotasks(); - expect(screen.getByTestId('floating').parentElement?.parentElement).toBe(customRoot); - customRoot.remove(); - }); - - test('creates a custom id node as the root', async () => { - render(); - fireEvent.click(screen.getByTestId('reference')); - await flushMicrotasks(); - expect(screen.getByTestId('floating').parentElement?.parentElement?.id).toBe('custom-id'); - }); - test('allows custom roots', async () => { const customRoot = document.createElement('div'); customRoot.id = 'custom-root'; @@ -89,7 +66,7 @@ describe.skipIf(!isJSDOM)('FloatingPortal', () => { return ( {renderRoot &&
} - ; + ); } @@ -103,4 +80,43 @@ describe.skipIf(!isJSDOM)('FloatingPortal', () => { const root = screen.getByTestId('root'); expect(root).toBe(subRoot?.parentElement); }); + + test('reattaches the portal when the root changes', async () => { + const customRoot = document.createElement('div'); + document.body.appendChild(customRoot); + + try { + function RootSwitcher() { + const [root, setRoot] = React.useState(undefined); + + return ( + + +