Skip to content

Commit c4a03a3

Browse files
committed
refactor: checks
1 parent ecc2b7f commit c4a03a3

File tree

7 files changed

+27
-27
lines changed

7 files changed

+27
-27
lines changed

src/fire-event.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
ScrollViewProps,
77
} from 'react-native';
88
import act from './act';
9-
import { isHostElement } from './helpers/component-tree';
9+
import { isValidElement } from './helpers/component-tree';
1010
import { isHostScrollView, isHostTextInput } from './helpers/host-component-names';
1111
import { isPointerEventEnabled } from './helpers/pointer-events';
1212
import { isTextInputEditable } from './helpers/text-input';
@@ -18,7 +18,7 @@ import { EventBuilder } from './user-event/event-builder';
1818
type EventHandler = (...args: unknown[]) => unknown;
1919

2020
export function isTouchResponder(element: HostElement) {
21-
if (!isHostElement(element)) {
21+
if (!isValidElement(element)) {
2222
return false;
2323
}
2424

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { View } from 'react-native';
33
import { render, screen } from '../..';
4-
import { getHostSiblings, getUnsafeRootElement } from '../component-tree';
4+
import { getHostSiblings, getRootElement } from '../component-tree';
55

66
function MultipleHostChildren() {
77
return (
@@ -46,6 +46,6 @@ describe('getUnsafeRootElement()', () => {
4646
);
4747

4848
const view = screen.getByTestId('view');
49-
expect(getUnsafeRootElement(view)).toEqual(screen.UNSAFE_root);
49+
expect(getRootElement(view)).toEqual(screen.UNSAFE_root);
5050
});
5151
});

src/helpers/accessibility.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
StyleSheet,
77
} from 'react-native';
88
import { HostElement } from '../renderer/host-element';
9-
import { getHostSiblings, getUnsafeRootElement } from './component-tree';
9+
import { getHostSiblings, getRootElement } from './component-tree';
1010
import {
1111
getHostComponentNames,
1212
isHostImage,
@@ -253,7 +253,7 @@ export function computeAccessibleName(element: HostElement): string | undefined
253253

254254
const labelElementId = computeAriaLabelledBy(element);
255255
if (labelElementId) {
256-
const rootElement = getUnsafeRootElement(element);
256+
const rootElement = getRootElement(element);
257257
const labelElement = findAllByProps(rootElement, { nativeID: labelElementId });
258258
if (labelElement.length > 0) {
259259
return getTextContent(labelElement[0]);

src/helpers/component-tree.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,34 @@ import { HostElement } from '../renderer/host-element';
44
* Checks if the given element is a host element.
55
* @param element The element to check.
66
*/
7-
export function isHostElement(element?: HostElement | null): element is HostElement {
7+
export function isValidElement(element?: HostElement | null): element is HostElement {
88
return typeof element?.type === 'string' && element.type !== 'CONTAINER';
99
}
1010

11-
/**
12-
* Returns host siblings for given element.
13-
* @param element The element start traversing from.
14-
*/
15-
export function getHostSiblings(element: HostElement | null): HostElement[] {
16-
const hostParent = element?.parent ?? null;
17-
return (
18-
hostParent?.children.filter(
19-
(sibling): sibling is HostElement => typeof sibling === 'object' && sibling !== element,
20-
) ?? []
21-
);
22-
}
23-
2411
/**
2512
* Returns the unsafe root element of the tree (probably composite).
2613
*
2714
* @param element The element start traversing from.
2815
* @returns The root element of the tree (host or composite).
2916
*/
30-
export function getUnsafeRootElement(element: HostElement) {
17+
export function getRootElement(element: HostElement) {
3118
let current: HostElement | null = element;
3219
while (current?.parent) {
3320
current = current.parent;
3421
}
3522

3623
return current;
3724
}
25+
26+
/**
27+
* Returns host siblings for given element.
28+
* @param element The element start traversing from.
29+
*/
30+
export function getHostSiblings(element: HostElement | null): HostElement[] {
31+
const hostParent = element?.parent ?? null;
32+
return (
33+
hostParent?.children.filter(
34+
(sibling): sibling is HostElement => typeof sibling === 'object' && sibling !== element,
35+
) ?? []
36+
);
37+
}

src/helpers/find-all.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { getConfig } from '../config';
22
import { HostElement } from '../renderer/host-element';
33
import { isHiddenFromAccessibility } from './accessibility';
4-
import { isHostElement } from './component-tree';
4+
import { isValidElement } from './component-tree';
55

66
interface FindAllOptions {
77
/** Match elements hidden from accessibility */
@@ -56,7 +56,7 @@ function findAllInternal(
5656
if (
5757
// When matchDeepestOnly = true: add current element only if no descendants match
5858
(!options?.matchDeepestOnly || matchingDescendants.length === 0) &&
59-
isHostElement(node) &&
59+
isValidElement(node) &&
6060
predicate(node)
6161
) {
6262
results.push(node);

src/matchers/to-be-on-the-screen.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { matcherHint, RECEIVED_COLOR } from 'jest-matcher-utils';
2-
import { getUnsafeRootElement } from '../helpers/component-tree';
2+
import { getRootElement } from '../helpers/component-tree';
33
import { HostElement } from '../renderer/host-element';
44
import { screen } from '../screen';
55
import { checkHostElement, formatElement } from './utils';
@@ -9,7 +9,7 @@ export function toBeOnTheScreen(this: jest.MatcherContext, element: HostElement)
99
checkHostElement(element, toBeOnTheScreen, this);
1010
}
1111

12-
const pass = element === null ? false : screen.UNSAFE_root === getUnsafeRootElement(element);
12+
const pass = element === null ? false : screen.UNSAFE_root === getRootElement(element);
1313

1414
const errorFound = () => {
1515
return `expected element tree not to contain element, but found\n${formatElement(element)}`;

src/matchers/utils.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from 'jest-matcher-utils';
99
import prettyFormat, { plugins } from 'pretty-format';
1010
import redent from 'redent';
11-
import { isHostElement } from '../helpers/component-tree';
11+
import { isValidElement } from '../helpers/component-tree';
1212
import { defaultMapProps } from '../helpers/format-default';
1313
import { HostElement, HostNode } from '../renderer/host-element';
1414

@@ -50,7 +50,7 @@ export function checkHostElement(
5050
matcherFn: jest.CustomMatcher,
5151
context: jest.MatcherContext,
5252
): asserts element is HostElement {
53-
if (!isHostElement(element)) {
53+
if (!isValidElement(element)) {
5454
throw new HostElementTypeError(element, matcherFn, context);
5555
}
5656
}

0 commit comments

Comments
 (0)