From 09f6d165ec45706eec4d0c6733fdea239c625201 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Mon, 2 Dec 2024 04:47:17 -0800 Subject: [PATCH] Export ReactNativeTester (#48024) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/48024 Changelog: [internal] Reviewed By: rshest Differential Revision: D66599071 fbshipit-source-id: 52585e030642e5b6bd8921d2c9f9d14ee5d6ce71 --- .../private/__tests__/ReactNativeTester.js | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 packages/react-native/src/private/__tests__/ReactNativeTester.js diff --git a/packages/react-native/src/private/__tests__/ReactNativeTester.js b/packages/react-native/src/private/__tests__/ReactNativeTester.js new file mode 100644 index 00000000000..70e480acec8 --- /dev/null +++ b/packages/react-native/src/private/__tests__/ReactNativeTester.js @@ -0,0 +1,71 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict-local + * @format + */ + +import type {MixedElement} from 'react'; + +import ReactFabric from '../../../Libraries/Renderer/shims/ReactFabric'; + +let globalSurfaceIdCounter = 1; + +const nativeRuntimeScheduler = global.nativeRuntimeScheduler; +const schedulerPriorityImmediate = + nativeRuntimeScheduler.unstable_ImmediatePriority; + +class Root { + #surfaceId: number; + #hasRendered: boolean = false; + + constructor() { + this.#surfaceId = globalSurfaceIdCounter; + globalSurfaceIdCounter += 10; + } + + render(element: MixedElement) { + if (!this.#hasRendered) { + global.$$JSTesterModuleName$$.startSurface(this.#surfaceId); + this.#hasRendered = true; + } + + ReactFabric.render(element, this.#surfaceId, () => {}, true); + } + + getMountingLogs(): Array { + return global.$$JSTesterModuleName$$.getMountingManagerLogs( + this.#surfaceId, + ); + } + + destroy() { + // TODO: check for leaks. + global.$$JSTesterModuleName$$.stopSurface(this.#surfaceId); + global.$$JSTesterModuleName$$.flushMessageQueue(); + } + + // TODO: add an API to check if all surfaces were deallocated when tests are finished. +} + +/* + * Runs a task on on the event loop. To be used together with root.render. + * + * React must run inside of event loop to ensure scheduling environment is closer to production. + */ +export function runTask(task: () => void) { + nativeRuntimeScheduler.unstable_scheduleCallback( + schedulerPriorityImmediate, + task, + ); + global.$$JSTesterModuleName$$.flushMessageQueue(); +} + +// TODO: Add option to define surface props and pass it to startSurface +// Surfacep rops: concurrentRoot, surfaceWidth, surfaceHeight, layoutDirection, pointScaleFactor. +export function createRoot(): Root { + return new Root(); +}