-
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 { TestInstance, TestNode } from 'test-renderer';
import { screen } from '../screen';
/**
* Checks if the given element is a host element.
* @param node The element to check.
*/
export function isTestInstance(node?: TestNode | null): node is TestInstance {
return typeof node !== 'string' && typeof node?.type === 'string';
}
export function isElementMounted(element: TestInstance) {
return getContainerElement(element) === screen.container;
}
/**
* Returns host siblings for given element.
* @param instance The element start traversing from.
*/
export function getInstanceSiblings(instance: TestInstance): TestInstance[] {
// Should not happen
const parent = instance.parent;
if (!parent) {
return [];
}
return parent.children.filter(
(sibling) => typeof sibling !== 'string' && sibling !== instance,
) as TestInstance[];
}
/**
* Returns the container element of the tree.
*
* @param instance The element start traversing from.
* @returns The container element of the tree.
*/
export function getContainerElement(instance: TestInstance) {
let current = instance;
while (current.parent) {
current = current.parent;
}
return current;
}