Skip to content

Commit

Permalink
Add getContainerVNode getter
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Dec 3, 2019
1 parent 24372e6 commit ba13d9e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
3 changes: 3 additions & 0 deletions shared-internals/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export function getChildren(
vnode: preact.VNode
): Array<preact.VNode | null | undefined>;
export function getComponentVNode(component: preact.Component): preact.VNode;
export function getContainerVNode(
container: Element | Document | ShadowRoot | DocumentFragment
): preact.VNode | null;

//
// Option hooks
Expand Down
11 changes: 11 additions & 0 deletions shared-internals/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ export function getComponentVNode(component) {
return component._vnode;
}

/**
* Return the `VNode` that is associated with a DOM node that was rendered into.
* This property only exists on root nodes, commonly called containers. They
* are created via top-level `render(vnode, container)` calls.
* @param {Element | Document | ShadowRoot | DocumentFragment} container
* @returns {import('../../src/internal').VNode | null}
*/
export function getContainerVNode(container) {
return container._children;
}

// Options getters/setters

/**
Expand Down
21 changes: 20 additions & 1 deletion shared-internals/test/browser/internals.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { createElement, render, Component, createRef, options } from 'preact';
import {
createElement,
render,
Component,
createRef,
options,
Fragment
} from 'preact';
import { setupScratch, teardown } from '../../../test/_util/helpers';
import {
getParent,
getComponent,
getDom,
getChildren,
getComponentVNode,
getContainerVNode,
getOptionsDiff,
getOptionsCommit,
getOptionsRoot,
Expand Down Expand Up @@ -109,6 +117,17 @@ describe('shared-internals', () => {
});
});

describe('getContainerVNode', () => {
it('should return undefined', () => {
expect(getContainerVNode(scratch)).to.equal(undefined);
});

it('should return root Fragment vnode', () => {
render(<div />, scratch);
expect(getContainerVNode(scratch).type).to.equal(Fragment);
});
});

describe('options', () => {
let prevDiff;
let prevCommit;
Expand Down

0 comments on commit ba13d9e

Please sign in to comment.