Skip to content

Commit

Permalink
Simplify MountRenderer.unmount
Browse files Browse the repository at this point in the history
Using current versions of Preact, `render(null, container)` is the idiomatic way
to unmount a component.
  • Loading branch information
robertknight committed Dec 3, 2024
1 parent 65d479e commit 65e414c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
7 changes: 2 additions & 5 deletions src/MountRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
RSTNode,
} from 'enzyme';
import type { VNode } from 'preact';
import { h, createElement } from 'preact';
import { createElement } from 'preact';
import { act } from 'preact/test-utils';
import type { PreactAdapterOptions } from './Adapter.js';

Expand Down Expand Up @@ -74,10 +74,7 @@ export default class MountRenderer implements AbstractMountRenderer {
}

unmount() {
// A custom tag name is used here to work around
// https://github.com/developit/preact/issues/1288.
render(h('unmount-me', {}), this._container);
this._container.innerHTML = '';
render(null, this._container);
}

getNode() {
Expand Down
4 changes: 3 additions & 1 deletion src/preact10-rst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ function rstNodeFromComponent(vnode: VNode, component: Component): RSTNode {
/**
* Convert the Preact components rendered into `container` into an RST node.
*/
export function getNode(container: HTMLElement): RSTNode {
export function getNode(container: HTMLElement): RSTNode | null {
const rendered = getLastVNodeRenderedIntoContainer(container);
const rstNode = rstNodeFromVNode(rendered);

Expand All @@ -193,6 +193,8 @@ export function getNode(container: HTMLElement): RSTNode {
if (Array.isArray(rstNode)) {
if (rstNode.length === 1) {
return rstNode[0] as RSTNode;
} else if (rstNode.length === 0) {
return null;
} else {
throw new Error(
'Root element must not be a fragment with multiple children'
Expand Down

0 comments on commit 65e414c

Please sign in to comment.