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(
,
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(
,
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(
,
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(
,
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(
,
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(
,
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(
,
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(
,
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(
,
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(
,
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(
,
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(
,
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(
,
scratch
);
let sibling = getDomSibling(divAVNode);
expect(sibling).to.equalNode(scratch.firstChild.childNodes[1]);
});
it('should return null if last sibling', () => {
render(
,
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(
,
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(
,
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(
,
scratch
);
expect(getDomSibling(divAVNode)).to.equal(null);
});
});