Skip to content

Commit abe8d71

Browse files
committed
refactor: inline host parent getter
1 parent 806c9f8 commit abe8d71

File tree

6 files changed

+5
-59
lines changed

6 files changed

+5
-59
lines changed

src/helpers/__tests__/component-tree.test.tsx

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Text, TextInput, View } from 'react-native';
33
import { render, screen } from '../..';
44
import {
55
getHostChildren,
6-
getHostParent,
76
getHostSelves,
87
getHostSiblings,
98
getUnsafeRootElement,
@@ -19,43 +18,6 @@ function MultipleHostChildren() {
1918
);
2019
}
2120

22-
describe('getHostParent()', () => {
23-
it('returns host parent for host component', () => {
24-
render(
25-
<View testID="grandparent">
26-
<View testID="parent">
27-
<View testID="subject" />
28-
<View testID="sibling" />
29-
</View>
30-
</View>,
31-
);
32-
33-
const hostParent = getHostParent(screen.getByTestId('subject'));
34-
expect(hostParent).toBe(screen.getByTestId('parent'));
35-
36-
const hostGrandparent = getHostParent(hostParent);
37-
expect(hostGrandparent).toBe(screen.getByTestId('grandparent'));
38-
39-
expect(getHostParent(hostGrandparent)).toBe(screen.UNSAFE_root);
40-
});
41-
42-
it('returns host parent for null', () => {
43-
expect(getHostParent(null)).toBe(null);
44-
});
45-
46-
it('returns host parent for composite component', () => {
47-
render(
48-
<View testID="parent">
49-
<MultipleHostChildren />
50-
<View testID="subject" />
51-
</View>,
52-
);
53-
54-
// eslint-disable-next-line jest/no-conditional-expect
55-
expect(true).toBeTruthy();
56-
});
57-
});
58-
5921
describe('getHostChildren()', () => {
6022
it('returns host children for host component', () => {
6123
render(

src/helpers/component-tree.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,6 @@ export function isHostElement(element?: HostElement | null): element is HostElem
88
return typeof element?.type === 'string' && element.type !== 'CONTAINER';
99
}
1010

11-
/**
12-
* Returns first host ancestor for given element.
13-
* @param element The element start traversing from.
14-
*/
15-
export function getHostParent(element: HostElement | null): HostElement | null {
16-
if (element == null) {
17-
return null;
18-
}
19-
20-
return element.parent;
21-
}
22-
2311
/**
2412
* Returns host children for given element.
2513
* @param element The element start traversing from.
@@ -62,7 +50,7 @@ export function getHostSelves(element: HostElement | null): HostElement[] {
6250
* @param element The element start traversing from.
6351
*/
6452
export function getHostSiblings(element: HostElement | null): HostElement[] {
65-
const hostParent = getHostParent(element);
53+
const hostParent = element?.parent ?? null;
6654
const hostSelves = getHostSelves(element);
6755
return getHostChildren(hostParent).filter((sibling) => !hostSelves.includes(sibling));
6856
}

src/helpers/pointer-events.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { HostElement } from '../renderer/host-element';
2-
import { getHostParent } from './component-tree';
32

43
/**
54
* pointerEvents controls whether the View can be the target of touch events.
@@ -17,7 +16,7 @@ export const isPointerEventEnabled = (element: HostElement, isParent?: boolean):
1716
return false;
1817
}
1918

20-
const hostParent = getHostParent(element);
19+
const hostParent = element.parent;
2120
if (!hostParent) return true;
2221

2322
return isPointerEventEnabled(hostParent, true);

src/matchers/to-be-disabled.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { matcherHint } from 'jest-matcher-utils';
22
import { computeAriaDisabled } from '../helpers/accessibility';
3-
import { getHostParent } from '../helpers/component-tree';
43
import { HostElement } from '../renderer/host-element';
54
import { checkHostElement, formatElement } from './utils';
65

@@ -43,7 +42,7 @@ export function toBeEnabled(this: jest.MatcherContext, element: HostElement) {
4342
}
4443

4544
function isAncestorDisabled(element: HostElement): boolean {
46-
const parent = getHostParent(element);
45+
const parent = element.parent;
4746
if (parent == null) {
4847
return false;
4948
}

src/matchers/to-be-visible.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { matcherHint } from 'jest-matcher-utils';
22
import { StyleSheet } from 'react-native';
33
import { isHiddenFromAccessibility } from '../helpers/accessibility';
4-
import { getHostParent } from '../helpers/component-tree';
54
import { isHostModal } from '../helpers/host-component-names';
65
import { HostElement } from '../renderer/host-element';
76
import { checkHostElement, formatElement } from './utils';
@@ -45,7 +44,7 @@ function isElementVisible(
4544
return false;
4645
}
4746

48-
const hostParent = getHostParent(element);
47+
const hostParent = element.parent;
4948
if (hostParent === null) {
5049
return true;
5150
}

src/user-event/press/press.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import act from '../../act';
2-
import { getHostParent } from '../../helpers/component-tree';
32
import { isTextInputEditable } from '../../helpers/text-input';
43
import { isPointerEventEnabled } from '../../helpers/pointer-events';
54
import { isHostText, isHostTextInput } from '../../helpers/host-component-names';
@@ -56,7 +55,7 @@ const basePress = async (
5655
return;
5756
}
5857

59-
const hostParentElement = getHostParent(element);
58+
const hostParentElement = element.parent;
6059
if (!hostParentElement) {
6160
return;
6261
}

0 commit comments

Comments
 (0)