import { createElement, render, Fragment } from '../../src/'; import { getDomSibling } from '../../src/component'; import { setupScratch, teardown } from '../_util/helpers'; /** @jsx createElement */ describe('getDomSibling', () => { /** @type {import('../../src/internal').PreactElement} */ let scratch; const getRoot = dom => dom._children; beforeEach(() => { scratch = setupScratch(); }); afterEach(() => { teardown(scratch); }); it('should find direct sibling', () => { render(
A
B
, scratch ); let vnode = getRoot(scratch)._children[0]._children[0]; expect(getDomSibling(vnode)).to.equalNode(scratch.firstChild.childNodes[1]); }); it('should find direct text node sibling', () => { render(
A
B
, scratch ); let vnode = getRoot(scratch)._children[0]._children[0]; expect(getDomSibling(vnode)).to.equalNode(scratch.firstChild.childNodes[1]); }); it('should find nested text node sibling', () => { render(
A
B
, scratch ); let vnode = getRoot(scratch)._children[0]._children[0]; expect(getDomSibling(vnode)).to.equalNode(scratch.firstChild.childNodes[1]); }); it('should find text node sibling with placeholder', () => { render(
A{null}B
, scratch); let vnode = getRoot(scratch)._children[0]._children[0]; expect(getDomSibling(vnode)).to.equalNode(scratch.firstChild.childNodes[1]); }); it('should find sibling with placeholder', () => { render(
A
{null}
B
, scratch ); let vnode = getRoot(scratch)._children[0]._children[0]; expect(getDomSibling(vnode)).to.equalNode(scratch.firstChild.childNodes[1]); }); it('should find sibling with nested placeholder', () => { render(
A
{null}
B
, scratch ); let vnode = getRoot(scratch)._children[0]._children[0]._children[0]; expect(getDomSibling(vnode)).to.equalNode(scratch.firstChild.childNodes[1]); }); it('should find sibling in parent', () => { render(
A
B
, scratch ); let vnode = getRoot(scratch)._children[0]._children[0]._children[0]; expect(getDomSibling(vnode)).to.equalNode(scratch.firstChild.childNodes[1]); }); it('should find unrelated sibling from a DOM VNode', () => { render(
A
B
, scratch ); let divAVNode = getRoot(scratch)._children[0]._children[0]._children[0] ._children[0]._children[0]; expect(divAVNode.type).to.equal('div'); expect(getDomSibling(divAVNode)).to.equalNode( scratch.firstChild.childNodes[1] ); }); it('should find unrelated sibling from a Fragment VNode', () => { render(
A
B
, scratch ); let fragment = getRoot(scratch)._children[0]._children[0]._children[0] ._children[0]; expect(fragment.type).to.equal(Fragment); expect(getDomSibling(fragment)).to.equalNode( scratch.firstChild.childNodes[1] ); }); it('should find unrelated sibling from a Component VNode', () => { const Foo = props => props.children; render(
A
B
, scratch ); let foo = getRoot(scratch)._children[0]._children[0]._children[0] ._children[0]; expect(foo.type).to.equal(Foo); expect(getDomSibling(foo)).to.equalNode(scratch.firstChild.childNodes[1]); }); it('should find sibling through components', () => { const Foo = props => props.children; render(
A
B
, scratch ); let divAVNode = getRoot(scratch)._children[0]._children[0]._children[0]; expect(divAVNode.type).to.equal('div'); expect(getDomSibling(divAVNode)).to.equalNode( scratch.firstChild.childNodes[1] ); }); it('should find sibling rendered in Components that wrap JSX children', () => { const Foo = props =>

{props.children}

; render(
A
a span
, scratch ); let divAVNode = getRoot(scratch)._children[0]._children[0]; expect(divAVNode.type).to.equal('div'); let sibling = getDomSibling(divAVNode); expect(sibling).to.equalNode(scratch.firstChild.childNodes[1]); }); it('should find sibling rendered in Components without JSX children', () => { const Foo = props =>

A paragraph

; render(
A
, scratch ); let divAVNode = getRoot(scratch)._children[0]._children[0]; expect(divAVNode.type).to.equal('div'); let sibling = getDomSibling(divAVNode); expect(sibling).to.equalNode(scratch.firstChild.childNodes[1]); }); it('should climb through Components without JSX children', () => { const divAVNode =
A
; const Foo = () => divAVNode; render(
B
, scratch ); let sibling = getDomSibling(divAVNode); expect(sibling).to.equalNode(scratch.firstChild.childNodes[1]); }); it('should return null if last sibling', () => { render(
A
B
C
, scratch ); const divCVNode = getRoot(scratch)._children[0]._children[2]._children[0]; expect(getDomSibling(divCVNode)).to.equal(null); }); it('should return null if no sibling', () => { render(
A
{null}
, scratch ); let divAVNode = getRoot(scratch)._children[0]._children[0]._children[0] ._children[0]._children[0]; expect(getDomSibling(divAVNode)).to.equal(null); }); it('should return null if no sibling with lots of empty trees', () => { render(
A
{null}
, scratch ); let divAVNode = getRoot(scratch)._children[0]._children[0]._children[0] ._children[0]._children[0]; expect(getDomSibling(divAVNode)).to.equal(null); }); it('should return null if current parent has no siblings (even if parent has siblings at same level)', () => { let divAVNode =
A
; render(
{divAVNode}
B
, scratch ); expect(getDomSibling(divAVNode)).to.equal(null); }); });