|
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | 4 | import React, { useState } from 'react'; |
5 | | -import { act, fireEvent, render, screen } from '@testing-library/react'; |
| 5 | +import { act, fireEvent, render, screen, waitFor } from '@testing-library/react'; |
6 | 6 |
|
7 | 7 | import { warnOnce } from '../../logging'; |
8 | 8 | import Portal, { PortalProps } from '../index'; |
@@ -108,6 +108,30 @@ describe('Portal', () => { |
108 | 108 | expect(document.body.contains(container)).toBe(false); |
109 | 109 | }); |
110 | 110 |
|
| 111 | + test('should support aborting async container setup', async () => { |
| 112 | + const container = document.createElement('div'); |
| 113 | + const onAbort = jest.fn(); |
| 114 | + const onContinue = jest.fn(); |
| 115 | + const getContainer: PortalProps['getContainer'] = async ({ abortSignal }) => { |
| 116 | + abortSignal.addEventListener('abort', onAbort); |
| 117 | + await Promise.resolve(); |
| 118 | + onContinue(abortSignal.aborted); |
| 119 | + return container; |
| 120 | + }; |
| 121 | + const removeContainer = jest.fn(); |
| 122 | + const { unmount } = renderPortal({ |
| 123 | + children: <p data-testid="portal-content">Hello!</p>, |
| 124 | + getContainer, |
| 125 | + removeContainer, |
| 126 | + }); |
| 127 | + unmount(); |
| 128 | + await waitFor(() => { |
| 129 | + expect(onContinue).not.toHaveBeenCalled(); |
| 130 | + expect(onAbort).toHaveBeenCalled(); |
| 131 | + expect(removeContainer).toHaveBeenCalledWith(null); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
111 | 135 | test('allows conditional change of getContainer/removeContainer', async () => { |
112 | 136 | function MovablePortal({ getContainer, removeContainer }: Pick<PortalProps, 'getContainer' | 'removeContainer'>) { |
113 | 137 | const [visible, setVisible] = useState(false); |
|
0 commit comments