From 015e7cbd9303d0447e86e2b247388f51ee1a6263 Mon Sep 17 00:00:00 2001 From: David Vacca Date: Mon, 26 Jun 2023 16:58:14 -0700 Subject: [PATCH] Create method to test reload of ReactHost (#37981) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/37981 This diff introduces a the test ReactHost.testReload() to verify that reloading of react host works as expected changelog: [internal] internal Reviewed By: cortinico Differential Revision: D46813814 fbshipit-source-id: 59e23e2f296c81b7ca3decb81e1f53b5871bbff4 --- .../react/bridgeless/ReactHostTest.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridgeless/ReactHostTest.java b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridgeless/ReactHostTest.java index 77913e95429..2aa0634d341 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridgeless/ReactHostTest.java +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridgeless/ReactHostTest.java @@ -22,6 +22,7 @@ import android.app.Activity; import com.facebook.react.MemoryPressureRouter; import com.facebook.react.bridge.JSBundleLoader; import com.facebook.react.bridge.MemoryPressureListener; +import com.facebook.react.bridge.ReactContext; import com.facebook.react.bridge.UIManager; import com.facebook.react.bridgeless.internal.bolts.Task; import com.facebook.react.bridgeless.internal.bolts.TaskCompletionSource; @@ -150,6 +151,23 @@ public class ReactHostTest { assertThat(mReactHost.getCurrentReactContext()).isNull(); } + @Test + public void testReload() throws Exception { + statAndTestReactHost(); + + ReactContext oldReactContext = mReactHost.getCurrentReactContext(); + BridgelessReactContext newReactContext = mock(BridgelessReactContext.class); + assertThat(newReactContext).isNotEqualTo(oldReactContext); + whenNew(BridgelessReactContext.class).withAnyArguments().thenReturn(newReactContext); + + waitForTaskUIThread(mReactHost.reload("Reload from testing infra")); + + assertThat(mReactHost.isInstanceInitialized()).isTrue(); + assertThat(mReactHost.getCurrentReactContext()).isNotNull(); + assertThat(mReactHost.getCurrentReactContext()).isEqualTo(newReactContext); + assertThat(mReactHost.getCurrentReactContext()).isNotEqualTo(oldReactContext); + } + private static void waitForTaskUIThread(Task task) throws InterruptedException { boolean isTaskCompleted = false; while (!isTaskCompleted) {