Skip to content

Commit 2670b59

Browse files
refactor(v13): cleanup existing code (#1701)
* refactor: cleanu existing code * chore: fix lint
1 parent 048d17d commit 2670b59

File tree

11 files changed

+42
-102
lines changed

11 files changed

+42
-102
lines changed

src/fire-event.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import act from './act';
1010
import { isElementMounted, isHostElement } from './helpers/component-tree';
1111
import { isHostScrollView, isHostTextInput } from './helpers/host-component-names';
1212
import { isPointerEventEnabled } from './helpers/pointer-events';
13-
import { isTextInputEditable } from './helpers/text-input';
13+
import { isEditableTextInput } from './helpers/text-input';
1414
import { Point, StringWithAutocomplete } from './types';
1515
import { nativeState } from './native-state';
1616

@@ -54,7 +54,7 @@ export function isEventEnabled(
5454
) {
5555
if (nearestTouchResponder != null && isHostTextInput(nearestTouchResponder)) {
5656
return (
57-
isTextInputEditable(nearestTouchResponder) ||
57+
isEditableTextInput(nearestTouchResponder) ||
5858
textInputEventsIgnoringEditableProp.has(eventName)
5959
);
6060
}
@@ -160,12 +160,7 @@ const scrollEventNames = new Set([
160160
]);
161161

162162
function setNativeStateIfNeeded(element: ReactTestInstance, eventName: string, value: unknown) {
163-
if (
164-
eventName === 'changeText' &&
165-
typeof value === 'string' &&
166-
isHostTextInput(element) &&
167-
isTextInputEditable(element)
168-
) {
163+
if (eventName === 'changeText' && typeof value === 'string' && isEditableTextInput(element)) {
169164
nativeState.valueForElement.set(element, value);
170165
}
171166

src/helpers/__tests__/query-name.test.ts

-10
This file was deleted.
+26-10
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
11
import * as React from 'react';
2-
import { View } from 'react-native';
2+
import { TextInput, View } from 'react-native';
33
import { render, screen } from '../..';
4-
import { getTextInputValue, isTextInputEditable } from '../text-input';
4+
import { getTextInputValue, isEditableTextInput } from '../text-input';
55

6-
test('getTextInputValue() throws error when invoked on non-text input', () => {
7-
render(<View testID="view" />);
6+
test('getTextInputValue basic test', () => {
7+
render(
8+
<View>
9+
<TextInput testID="value" value="text-a" />
10+
<TextInput testID="default-value" defaultValue="text-b" />
11+
<View testID="view" />
12+
</View>,
13+
);
14+
15+
expect(getTextInputValue(screen.getByTestId('value'))).toBe('text-a');
16+
expect(getTextInputValue(screen.getByTestId('default-value'))).toBe('text-b');
817

918
const view = screen.getByTestId('view');
1019
expect(() => getTextInputValue(view)).toThrowErrorMatchingInlineSnapshot(
1120
`"Element is not a "TextInput", but it has type "View"."`,
1221
);
1322
});
1423

15-
test('isTextInputEditable() throws error when invoked on non-text input', () => {
16-
render(<View testID="view" />);
17-
18-
const view = screen.getByTestId('view');
19-
expect(() => isTextInputEditable(view)).toThrowErrorMatchingInlineSnapshot(
20-
`"Element is not a "TextInput", but it has type "View"."`,
24+
test('isEditableTextInput basic test', () => {
25+
render(
26+
<View>
27+
<TextInput testID="default" />
28+
<TextInput testID="editable" editable={true} />
29+
<TextInput testID="non-editable" editable={false} />
30+
<View testID="view" />
31+
</View>,
2132
);
33+
34+
expect(isEditableTextInput(screen.getByTestId('default'))).toBe(true);
35+
expect(isEditableTextInput(screen.getByTestId('editable'))).toBe(true);
36+
expect(isEditableTextInput(screen.getByTestId('non-editable'))).toBe(false);
37+
expect(isEditableTextInput(screen.getByTestId('view'))).toBe(false);
2238
});

src/helpers/accessibility.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { getHostSiblings, getUnsafeRootElement, isHostElement } from './componen
1010
import { findAll } from './find-all';
1111
import { isHostImage, isHostSwitch, isHostText, isHostTextInput } from './host-component-names';
1212
import { getTextContent } from './text-content';
13-
import { isTextInputEditable } from './text-input';
13+
import { isEditableTextInput } from './text-input';
1414

1515
type IsInaccessibleOptions = {
1616
cache?: WeakMap<ReactTestInstance, boolean>;
@@ -208,7 +208,7 @@ export function computeAriaChecked(element: ReactTestInstance): AccessibilitySta
208208

209209
// See: https://github.com/callstack/react-native-testing-library/wiki/Accessibility:-State#disabled-state
210210
export function computeAriaDisabled(element: ReactTestInstance): boolean {
211-
if (isHostTextInput(element) && !isTextInputEditable(element)) {
211+
if (isHostTextInput(element) && !isEditableTextInput(element)) {
212212
return true;
213213
}
214214

src/helpers/deprecation.ts

-53
This file was deleted.

src/helpers/query-name.ts

-4
This file was deleted.

src/helpers/text-input.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@ import { ReactTestInstance } from 'react-test-renderer';
22
import { nativeState } from '../native-state';
33
import { isHostTextInput } from './host-component-names';
44

5-
export function isTextInputEditable(element: ReactTestInstance) {
6-
if (!isHostTextInput(element)) {
7-
throw new Error(`Element is not a "TextInput", but it has type "${element.type}".`);
8-
}
9-
10-
return element.props.editable !== false;
5+
export function isEditableTextInput(element: ReactTestInstance) {
6+
return isHostTextInput(element) && element.props.editable !== false;
117
}
128

139
export function getTextInputValue(element: ReactTestInstance) {

src/user-event/clear.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ReactTestInstance } from 'react-test-renderer';
22
import { ErrorWithStack } from '../helpers/errors';
33
import { isHostTextInput } from '../helpers/host-component-names';
4-
import { getTextInputValue, isTextInputEditable } from '../helpers/text-input';
4+
import { getTextInputValue, isEditableTextInput } from '../helpers/text-input';
55
import { isPointerEventEnabled } from '../helpers/pointer-events';
66
import { EventBuilder } from './event-builder';
77
import { UserEventInstance } from './setup';
@@ -16,7 +16,7 @@ export async function clear(this: UserEventInstance, element: ReactTestInstance)
1616
);
1717
}
1818

19-
if (!isTextInputEditable(element) || !isPointerEventEnabled(element)) {
19+
if (!isEditableTextInput(element) || !isPointerEventEnabled(element)) {
2020
return;
2121
}
2222

src/user-event/paste.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ReactTestInstance } from 'react-test-renderer';
22
import { ErrorWithStack } from '../helpers/errors';
33
import { isHostTextInput } from '../helpers/host-component-names';
44
import { isPointerEventEnabled } from '../helpers/pointer-events';
5-
import { getTextInputValue, isTextInputEditable } from '../helpers/text-input';
5+
import { getTextInputValue, isEditableTextInput } from '../helpers/text-input';
66
import { nativeState } from '../native-state';
77
import { EventBuilder } from './event-builder';
88
import { UserEventInstance } from './setup';
@@ -20,7 +20,7 @@ export async function paste(
2020
);
2121
}
2222

23-
if (!isTextInputEditable(element) || !isPointerEventEnabled(element)) {
23+
if (!isEditableTextInput(element) || !isPointerEventEnabled(element)) {
2424
return;
2525
}
2626

src/user-event/press/press.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { ReactTestInstance } from 'react-test-renderer';
22
import act from '../../act';
33
import { getHostParent } from '../../helpers/component-tree';
4-
import { isTextInputEditable } from '../../helpers/text-input';
4+
import { isEditableTextInput } from '../../helpers/text-input';
55
import { isPointerEventEnabled } from '../../helpers/pointer-events';
6-
import { isHostText, isHostTextInput } from '../../helpers/host-component-names';
6+
import { isHostText } from '../../helpers/host-component-names';
77
import { EventBuilder } from '../event-builder';
88
import { UserEventConfig, UserEventInstance } from '../setup';
99
import { dispatchEvent, wait } from '../utils';
@@ -49,7 +49,7 @@ const basePress = async (
4949
return;
5050
}
5151

52-
if (isHostTextInput(element) && isTextInputEditable(element) && isPointerEventEnabled(element)) {
52+
if (isEditableTextInput(element) && isPointerEventEnabled(element)) {
5353
await emitTextInputPressEvents(config, element, options);
5454
return;
5555
}

src/user-event/type/type.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { isHostTextInput } from '../../helpers/host-component-names';
33
import { nativeState } from '../../native-state';
44
import { EventBuilder } from '../event-builder';
55
import { ErrorWithStack } from '../../helpers/errors';
6-
import { getTextInputValue, isTextInputEditable } from '../../helpers/text-input';
6+
import { getTextInputValue, isEditableTextInput } from '../../helpers/text-input';
77
import { isPointerEventEnabled } from '../../helpers/pointer-events';
88
import { UserEventConfig, UserEventInstance } from '../setup';
99
import { dispatchEvent, wait, getTextContentSize } from '../utils';
@@ -28,7 +28,7 @@ export async function type(
2828
}
2929

3030
// Skip events if the element is disabled
31-
if (!isTextInputEditable(element) || !isPointerEventEnabled(element)) {
31+
if (!isEditableTextInput(element) || !isPointerEventEnabled(element)) {
3232
return;
3333
}
3434

0 commit comments

Comments
 (0)