|
| 1 | +import { render, waitFor } from '@testing-library/react'; |
| 2 | +import React from 'react'; |
| 3 | +import { describe, expect, it, vi } from 'vitest'; |
| 4 | + |
| 5 | +import { ExternalElementMounter } from '../ExternalElementMounter'; |
| 6 | + |
| 7 | +describe('ExternalElementMounter', () => { |
| 8 | + it('calls mount with the host div element', async () => { |
| 9 | + const mount = vi.fn(); |
| 10 | + const unmount = vi.fn(); |
| 11 | + |
| 12 | + render( |
| 13 | + <ExternalElementMounter |
| 14 | + mount={mount} |
| 15 | + unmount={unmount} |
| 16 | + />, |
| 17 | + ); |
| 18 | + |
| 19 | + await waitFor(() => expect(mount).toHaveBeenCalledOnce()); |
| 20 | + expect(mount).toHaveBeenCalledWith(expect.any(HTMLDivElement)); |
| 21 | + }); |
| 22 | + |
| 23 | + it('calls unmount with the same element when removed', async () => { |
| 24 | + const mount = vi.fn(); |
| 25 | + const unmount = vi.fn(); |
| 26 | + |
| 27 | + const { unmount: removeComponent } = render( |
| 28 | + <ExternalElementMounter |
| 29 | + mount={mount} |
| 30 | + unmount={unmount} |
| 31 | + />, |
| 32 | + ); |
| 33 | + |
| 34 | + await waitFor(() => expect(mount).toHaveBeenCalledOnce()); |
| 35 | + const mountedEl = mount.mock.calls[0][0]; |
| 36 | + removeComponent(); |
| 37 | + |
| 38 | + expect(unmount).toHaveBeenCalledWith(mountedEl); |
| 39 | + }); |
| 40 | + |
| 41 | + it('calls mount even when a forwarded ref is present in props (React 19 regression)', async () => { |
| 42 | + // In React 19, ref is a regular prop. Before the fix, a ref forwarded through |
| 43 | + // the component chain would land in ...rest and overwrite nodeRef in |
| 44 | + // `<div ref={nodeRef} {...rest} />`, preventing mount from being called. |
| 45 | + // React 18 strips ref before passing to non-forwardRef components, so the |
| 46 | + // regression is only observable in a React 19 runtime — but this test guards |
| 47 | + // against it being reintroduced. |
| 48 | + const mount = vi.fn(); |
| 49 | + const unmount = vi.fn(); |
| 50 | + |
| 51 | + render( |
| 52 | + React.createElement(ExternalElementMounter, { |
| 53 | + mount, |
| 54 | + unmount, |
| 55 | + ref: { current: null }, |
| 56 | + }), |
| 57 | + ); |
| 58 | + |
| 59 | + await waitFor(() => expect(mount).toHaveBeenCalledOnce()); |
| 60 | + expect(mount).toHaveBeenCalledWith(expect.any(HTMLDivElement)); |
| 61 | + }); |
| 62 | +}); |
0 commit comments