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
This commit is contained in:
Rubén Norte authored and Facebook GitHub Bot committed 2024-12-02 04:47:17 -08:00
1 parent 88c9a42fca
commit 09f6d165ec
1 file changed
+71
@@ -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<string> {
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();
}