From 3e29cbe1d0d7ff90a75be47ba4aecfb83b8027d5 Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Thu, 23 Jan 2025 05:30:24 -0800 Subject: [PATCH] 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 --- .../TextInput/__tests__/TextInput-itest.js | 54 ++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/packages/react-native/Libraries/Components/TextInput/__tests__/TextInput-itest.js b/packages/react-native/Libraries/Components/TextInput/__tests__/TextInput-itest.js index 4f165471a0b..da55807a5f3 100644 --- a/packages/react-native/Libraries/Components/TextInput/__tests__/TextInput-itest.js +++ b/packages/react-native/Libraries/Components/TextInput/__tests__/TextInput-itest.js @@ -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( + { + 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); + }); +});