-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathcomponent-tree.ts
More file actions
46 lines (39 loc) · 1.17 KB
/
component-tree.ts
File metadata and controls
46 lines (39 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import type { HostElement, HostNode } from 'test-renderer';
import { screen } from '../screen';
/**
* Checks if the given element is a host element.
* @param element The element to check.
*/
export function isHostElement(element?: HostNode | null): element is HostElement {
return typeof element !== 'string' && typeof element?.type === 'string';
}
export function isElementMounted(element: HostElement) {
return getContainerElement(element) === screen.container;
}
/**
* Returns host siblings for given element.
* @param element The element start traversing from.
*/
export function getHostSiblings(element: HostElement): HostElement[] {
// Should not happen
const parent = element.parent;
if (!parent) {
return [];
}
return parent.children.filter(
(sibling) => typeof sibling !== 'string' && sibling !== element,
) as HostElement[];
}
/**
* Returns the containerelement of the tree.
*
* @param element The element start traversing from.
* @returns The container element of the tree.
*/
export function getContainerElement(element: HostElement) {
let current = element;
while (current.parent) {
current = current.parent;
}
return current;
}