Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Snackbar/AppRootPortal): fix default shouldDisablePortalLogic in AppRootPortal #6389

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions packages/vkui/src/components/AppRoot/AppRootPortal.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import * as React from 'react';
import { render, screen } from '@testing-library/react';
import { AppRoot, type AppRootProps } from './AppRoot';
import { AppRootPortal, type AppRootPortalProps } from './AppRootPortal';

function TestComponent({
appRootProps = {},
usePortal,
}: {
appRootProps?: AppRootProps;
usePortal?: AppRootPortalProps['usePortal'];
} = {}) {
const portalRootRef = React.useRef<HTMLDivElement | null>(null);

return (
<AppRoot portalRoot={portalRootRef} {...appRootProps}>
<AppRootPortal usePortal={usePortal}>
<div>content</div>
</AppRootPortal>
<div data-testid="portal-root" ref={portalRootRef} />
</AppRoot>
);
}

describe('AppRootPortal', () => {
it('does not use portal by default in full mode', () => {
render(<TestComponent appRootProps={{ mode: 'full' }} />);

expect(screen.getByTestId('portal-root').childElementCount).toBe(0);
});

it('uses portal in embedded mode', () => {
render(<TestComponent appRootProps={{ mode: 'embedded' }} />);

expect(screen.getByTestId('portal-root').childElementCount).toBe(1);
});

it('uses portal in partial mode', () => {
render(<TestComponent appRootProps={{ mode: 'partial' }} />);

expect(screen.getByTestId('portal-root').childElementCount).toBe(1);
});

it('does not use portal when portal is disabled in AppRoot', () => {
render(<TestComponent appRootProps={{ mode: 'partial', disablePortal: true }} />);

expect(screen.getByTestId('portal-root').childElementCount).toBe(0);
});

it('does not use portal when usePortal is false', () => {
render(<TestComponent appRootProps={{ mode: 'partial' }} usePortal={false} />);

expect(screen.getByTestId('portal-root').childElementCount).toBe(0);
});

it('uses portal in full mode when usePortal is true', () => {
render(<TestComponent appRootProps={{ mode: 'full' }} usePortal />);

expect(screen.getByTestId('portal-root').childElementCount).toBe(1);
});

it('uses portal passed via usePortal prop', () => {
const Wrapper = function Wrapper() {
const customPortalRef = React.useRef<HTMLDivElement | null>(null);
return (
<React.Fragment>
<TestComponent usePortal={customPortalRef} />
<div data-testid="custom-portal" ref={customPortalRef} />
</React.Fragment>
);
};
render(<Wrapper />);

expect(screen.getByTestId('portal-root').childElementCount).toBe(0);
expect(screen.getByTestId('custom-portal').childElementCount).toBe(1);
});
});
4 changes: 2 additions & 2 deletions packages/vkui/src/components/AppRoot/AppRootPortal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const AppRootPortal = ({ children, usePortal }: AppRootPortalProps) => {

const portalContainer = resolvePortalContainer(usePortal, portalRoot.current);
if (!portalContainer || shouldDisablePortal(usePortal, mode, Boolean(disablePortal))) {
return children;
return <React.Fragment>{children}</React.Fragment>;
}

return createPortal(
Expand All @@ -47,7 +47,7 @@ function shouldDisablePortal(
return disablePortal || usePortal !== true;
}
// fallback
return disablePortal || mode !== 'full';
return disablePortal || mode === 'full';
}

function resolvePortalContainer<PortalRootFromContext extends HTMLElement | null | undefined>(
Expand Down
Loading