Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: auto delete portals on container removal
This allows for decorations widgets to use portals.
- Loading branch information
Showing
2 changed files
with
99 additions
and
9 deletions.
There are no files selected for viewing
62 changes: 57 additions & 5 deletions
62
@remirror/react/src/components/__tests__/remirror-portals.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,71 @@ | ||
import { PortalContainer } from '@remirror/core'; | ||
import { act, render } from '@testing-library/react'; | ||
import { useRemirrorManager } from '../../hooks'; | ||
import { RemirrorManager } from '../remirror-manager'; | ||
import { RemirrorPortals } from '../remirror-portals'; | ||
|
||
describe('NodeViewPortalComponent', () => { | ||
describe('RemirrorPortals', () => { | ||
const container = document.createElement('span'); | ||
container.id = 'root'; | ||
|
||
afterEach(() => { | ||
document.body.innerHTML = ''; | ||
}); | ||
|
||
it('should update on render', () => { | ||
const portalContainer = new PortalContainer(); | ||
const { getByTestId } = render(<RemirrorPortals portalContainer={portalContainer} />); | ||
const mockRender = jest.fn(() => <div data-testid='test' />); | ||
|
||
act(() => { | ||
const mockRender = jest.fn(() => <div data-testid='test' />); | ||
const element = document.createElement('span'); | ||
document.body.appendChild(element); | ||
portalContainer.render({ render: mockRender, container: element }); | ||
document.body.appendChild(container); | ||
portalContainer.render({ render: mockRender, container }); | ||
}); | ||
|
||
expect(getByTestId('test')).toBeTruthy(); | ||
expect(mockRender).toBeCalledWith({}, {}); | ||
}); | ||
|
||
it('provides access to the manager context', () => { | ||
expect.assertions(1); | ||
const Component = () => { | ||
const manager = useRemirrorManager(); | ||
|
||
expect(manager).toBeTruthy(); | ||
return <div data-testid='test' />; | ||
}; | ||
|
||
const portalContainer = new PortalContainer(); | ||
render( | ||
<RemirrorManager> | ||
<RemirrorPortals portalContainer={portalContainer} /> | ||
</RemirrorManager>, | ||
); | ||
|
||
act(() => { | ||
document.body.appendChild(container); | ||
portalContainer.render({ render: Component, container }); | ||
}); | ||
}); | ||
|
||
it('removes the portal the when dom node is removed', () => { | ||
expect.assertions(3); | ||
|
||
const portalContainer = new PortalContainer(); | ||
const { queryByTestId } = render(<RemirrorPortals portalContainer={portalContainer} />); | ||
|
||
act(() => { | ||
document.body.appendChild(container); | ||
portalContainer.render({ render: () => <div data-testid='test' />, container }); | ||
}); | ||
|
||
expect(queryByTestId('test')).toBeTruthy(); | ||
|
||
document.body.removeChild(container); | ||
portalContainer.on(portals => { | ||
expect(portals.has(container)).toBeFalse(); | ||
}); | ||
|
||
expect(queryByTestId('test')).toBeNull(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters