From 83fc68508e29b8ade2b5b4ea6d0ac18b8446287a Mon Sep 17 00:00:00 2001 From: QQ Date: Sat, 14 Nov 2026 15:00:01 +0800 Subject: [PATCH] Import packages/plugin/tests/client-portal.test.ts --- packages/plugin/tests/client-portal.test.ts | 51 +++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 packages/plugin/tests/client-portal.test.ts diff --git a/packages/plugin/tests/client-portal.test.ts b/packages/plugin/tests/client-portal.test.ts new file mode 100644 index 0000000..e685975 --- /dev/null +++ b/packages/plugin/tests/client-portal.test.ts @@ -0,0 +1,51 @@ +import { afterEach, describe, expect, it, vi } from 'vitest' +import { renderBodyPortal } from '../src/client/portal.js' + +const body = { tagName: 'BODY' } as unknown as Element +const child = { type: 'div' } + +afterEach(() => { + vi.unstubAllGlobals() +}) + +describe('renderBodyPortal', () => { + it('portals into document.body so fixed layers escape the host containing block', () => { + vi.stubGlobal('document', { body }) + const portal = vi.fn((_children: unknown, _container: Element) => ({ portalled: true })) + expect(renderBodyPortal(portal, child)).toEqual({ portalled: true }) + expect(portal).toHaveBeenCalledWith(child, body) + }) + + it('renders inline when the host exposes no portal renderer', () => { + vi.stubGlobal('document', { body }) + expect(renderBodyPortal(undefined, child)).toBe(child) + }) + + it('renders inline when the renderer is not a function', () => { + vi.stubGlobal('document', { body }) + expect(renderBodyPortal(null as unknown as undefined, child)).toBe(child) + expect(renderBodyPortal({} as unknown as undefined, child)).toBe(child) + expect(renderBodyPortal('createPortal' as unknown as undefined, child)).toBe(child) + }) + + it('renders inline during server-side rendering, where document is absent', () => { + expect(typeof document).toBe('undefined') + const portal = vi.fn() + expect(renderBodyPortal(portal, child)).toBe(child) + expect(portal).not.toHaveBeenCalled() + }) + + it('renders inline before document.body exists', () => { + vi.stubGlobal('document', { body: null }) + const portal = vi.fn() + expect(renderBodyPortal(portal, child)).toBe(child) + expect(portal).not.toHaveBeenCalled() + }) + + it('renders inline instead of throwing when createPortal rejects the container', () => { + vi.stubGlobal('document', { body }) + const portal = vi.fn(() => { throw new Error('Target container is not a DOM element.') }) + expect(() => renderBodyPortal(portal, child)).not.toThrow() + expect(renderBodyPortal(portal, child)).toBe(child) + }) +})