diff --git a/src/internal/portal/__tests__/portal.test.tsx b/src/internal/portal/__tests__/portal.test.tsx index b5c8b04..a00ee22 100644 --- a/src/internal/portal/__tests__/portal.test.tsx +++ b/src/internal/portal/__tests__/portal.test.tsx @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import React, { useState } from 'react'; -import { act, fireEvent, render, screen } from '@testing-library/react'; +import { act, fireEvent, render, screen, waitFor } from '@testing-library/react'; import { warnOnce } from '../../logging'; import Portal, { PortalProps } from '../index'; @@ -108,6 +108,30 @@ describe('Portal', () => { expect(document.body.contains(container)).toBe(false); }); + test('should support aborting async container setup', async () => { + const container = document.createElement('div'); + const onAbort = jest.fn(); + const onContinue = jest.fn(); + const getContainer: PortalProps['getContainer'] = async ({ abortSignal }) => { + abortSignal.addEventListener('abort', onAbort); + await Promise.resolve(); + onContinue(abortSignal.aborted); + return container; + }; + const removeContainer = jest.fn(); + const { unmount } = renderPortal({ + children:
Hello!
, + getContainer, + removeContainer, + }); + unmount(); + await waitFor(() => { + expect(onContinue).not.toHaveBeenCalled(); + expect(onAbort).toHaveBeenCalled(); + expect(removeContainer).toHaveBeenCalledWith(null); + }); + }); + test('allows conditional change of getContainer/removeContainer', async () => { function MovablePortal({ getContainer, removeContainer }: Pick