-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Resolve current React context from the captured host #58406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
+151
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
packages/react-native/ReactAndroid/src/test/java/com/facebook/react/ReactDelegateTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| package com.facebook.react | ||
|
|
||
| import android.annotation.SuppressLint | ||
| import android.app.Activity | ||
| import android.app.Application | ||
| import com.facebook.react.bridge.ReactContext | ||
| import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags | ||
| import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsDefaults | ||
| import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests | ||
| import org.assertj.core.api.Assertions.assertThat | ||
| import org.junit.After | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.mockito.kotlin.doReturn | ||
| import org.mockito.kotlin.mock | ||
| import org.robolectric.Robolectric | ||
| import org.robolectric.RobolectricTestRunner | ||
|
|
||
| @SuppressLint( | ||
| "ActivityStoredInField", | ||
| "DeprecatedClass", | ||
| "DeprecatedMethod", | ||
| "DeprecatedSuperclass", | ||
| ) | ||
| @RunWith(RobolectricTestRunner::class) | ||
| class ReactDelegateTest { | ||
|
|
||
| private lateinit var activity: Activity | ||
|
|
||
| @Before | ||
| fun setUp() { | ||
| ReactNativeFeatureFlagsForTests.setUp() | ||
| activity = Robolectric.buildActivity(Activity::class.java).create().get() | ||
| } | ||
|
|
||
| @After | ||
| fun tearDown() { | ||
| ReactNativeFeatureFlags.dangerouslyReset() | ||
| } | ||
|
|
||
| @Test | ||
| fun currentReactContext_withReactHost_returnsContext() { | ||
| overrideBridgelessArchitecture(true) | ||
| val reactContext = mock<ReactContext>() | ||
| val reactHost = mock<ReactHost> { on { currentReactContext } doReturn reactContext } | ||
| val delegate = ReactDelegate(activity, reactHost, "test-app", null) | ||
|
|
||
| assertThat(delegate.currentReactContext).isSameAs(reactContext) | ||
| } | ||
|
|
||
| @Test | ||
| fun currentReactContext_withReactHost_ignoresArchitectureFlag() { | ||
| overrideBridgelessArchitecture(false) | ||
| val reactContext = mock<ReactContext>() | ||
| val reactHost = mock<ReactHost> { on { currentReactContext } doReturn reactContext } | ||
| val delegate = ReactDelegate(activity, reactHost, "test-app", null) | ||
|
|
||
| assertThat(delegate.currentReactContext).isSameAs(reactContext) | ||
| } | ||
|
|
||
| @Suppress("DEPRECATION") | ||
| @Test | ||
| fun currentReactContext_withoutReactNativeHost_returnsNull() { | ||
| overrideBridgelessArchitecture(false) | ||
| val delegate = ReactDelegate(activity, null as ReactNativeHost?, "test-app", null) | ||
|
|
||
| assertThat(delegate.currentReactContext).isNull() | ||
| } | ||
|
|
||
| @Suppress("DEPRECATION") | ||
| @Test | ||
| fun currentReactContext_withUninitializedReactNativeHost_doesNotCreateInstance() { | ||
| overrideBridgelessArchitecture(false) | ||
| val reactNativeHost = | ||
| object : ReactNativeHost(activity.application as Application) { | ||
| override fun getUseDeveloperSupport(): Boolean = false | ||
|
|
||
| override fun getPackages(): List<ReactPackage> = emptyList() | ||
| } | ||
| val delegate = ReactDelegate(activity, reactNativeHost, "test-app", null) | ||
|
|
||
| assertThat(delegate.currentReactContext).isNull() | ||
| assertThat(reactNativeHost.hasInstance()).isFalse() | ||
| } | ||
|
|
||
| @Suppress("DEPRECATION") | ||
| @Test | ||
| fun currentReactContext_withInitializedReactNativeHost_returnsContext() { | ||
| overrideBridgelessArchitecture(false) | ||
| val reactContext = mock<ReactContext>() | ||
| val instanceManager = | ||
| mock<ReactInstanceManager> { on { currentReactContext } doReturn reactContext } | ||
| val reactNativeHost = createInitializedReactNativeHost(instanceManager) | ||
| val delegate = ReactDelegate(activity, reactNativeHost, "test-app", null) | ||
|
|
||
| assertThat(delegate.currentReactContext).isSameAs(reactContext) | ||
| } | ||
|
|
||
| @Suppress("DEPRECATION") | ||
| @Test | ||
| fun currentReactContext_withReactNativeHost_ignoresArchitectureFlag() { | ||
| overrideBridgelessArchitecture(true) | ||
| val reactContext = mock<ReactContext>() | ||
| val instanceManager = | ||
| mock<ReactInstanceManager> { on { currentReactContext } doReturn reactContext } | ||
| val reactNativeHost = createInitializedReactNativeHost(instanceManager) | ||
| val delegate = ReactDelegate(activity, reactNativeHost, "test-app", null) | ||
|
|
||
| assertThat(delegate.currentReactContext).isSameAs(reactContext) | ||
| } | ||
|
|
||
| @Suppress("DEPRECATION") | ||
| private fun createInitializedReactNativeHost( | ||
| instanceManager: ReactInstanceManager, | ||
| ): ReactNativeHost = | ||
| object : ReactNativeHost(activity.application as Application) { | ||
| override val reactInstanceManager: ReactInstanceManager = instanceManager | ||
|
|
||
| override fun hasInstance(): Boolean = true | ||
|
|
||
| override fun getUseDeveloperSupport(): Boolean = false | ||
|
|
||
| override fun getPackages(): List<ReactPackage> = emptyList() | ||
| } | ||
|
|
||
| private fun overrideBridgelessArchitecture(enabled: Boolean) { | ||
| ReactNativeFeatureFlags.override( | ||
| object : ReactNativeFeatureFlagsDefaults() { | ||
| override fun enableBridgelessArchitecture(): Boolean = enabled | ||
| }, | ||
| ) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.