add test for TextInput blut and focus (#48862)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/48862

changelog: [internal]

Add simple tests that use new `Fantom.dispatchNativeEvent` and `Fantom.runOnUIThread`.

Reviewed By: rubennorte

Differential Revision: D68492472

fbshipit-source-id: 33537ed7a728c2cc01abdcc5aa178b3afa3779fa
This commit is contained in:
Samuel Susla authored and Facebook GitHub Bot committed 2025-01-23 05:30:24 -08:00
1 parent 864c34e9f6
commit 3e29cbe1d0
1 file changed
+53 -1
@@ -8,15 +8,18 @@
* @format
* @oncall react_native
* @fantom_flags enableFixForViewCommandRace:true
* @fantom_flags enableAccessToHostTreeInFabric:true
*/
import '../../../Core/InitializeCore.js';
import ensureInstance from '../../../../src/private/utilities/ensureInstance';
import ReactNativeElement from '../../../../src/private/webapis/dom/nodes/ReactNativeElement';
import TextInput from '../TextInput';
import * as Fantom from '@react-native/fantom';
import * as React from 'react';
import {useEffect, useLayoutEffect, useRef} from 'react';
describe('TextInput', () => {
describe('focus view command', () => {
it('creates view before dispatching view command from ref function', () => {
const root = Fantom.createRoot();
@@ -95,3 +98,52 @@ describe('TextInput', () => {
);
});
});
describe('focus and blur event', () => {
it('sends focus and blur events', () => {
const root = Fantom.createRoot();
let maybeNode;
let focusEvent = jest.fn();
let blurEvent = jest.fn();
Fantom.runTask(() => {
root.render(
<TextInput
onFocus={focusEvent}
onBlur={blurEvent}
ref={node => {
maybeNode = node;
}}
/>,
);
});
const element = ensureInstance(maybeNode, ReactNativeElement);
expect(focusEvent).toHaveBeenCalledTimes(0);
expect(blurEvent).toHaveBeenCalledTimes(0);
Fantom.runOnUIThread(() => {
Fantom.dispatchNativeEvent(element, 'focus');
});
// The tasks have not run.
expect(focusEvent).toHaveBeenCalledTimes(0);
expect(blurEvent).toHaveBeenCalledTimes(0);
Fantom.runWorkLoop();
expect(focusEvent).toHaveBeenCalledTimes(1);
expect(blurEvent).toHaveBeenCalledTimes(0);
Fantom.runOnUIThread(() => {
Fantom.dispatchNativeEvent(element, 'blur');
});
Fantom.runWorkLoop();
expect(focusEvent).toHaveBeenCalledTimes(1);
expect(blurEvent).toHaveBeenCalledTimes(1);
});
});