-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: As part of the effort to Kotlin-fy React Native tests, I've converted [ReactTestHelper](https://github.com/facebook/react-native/tree/main/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridge/ReactTestHelper.java) to Kotlin. ## Changelog: [Internal] [Changed] - Convert ReactTestHelper to Kotlin Pull Request resolved: #37768 Test Plan: Tests pass: ./gradlew :packages:react-native:ReactAndroid:test Formatted with [KtFmt](https://facebook.github.io/ktfmt/) Reviewed By: rshest Differential Revision: D46587427 Pulled By: cortinico fbshipit-source-id: 82d08d01cf4c7cb7c05a63696e1acdacf60b1ad5
- Loading branch information
1 parent
0aa3308
commit 6ebee63
Showing
2 changed files
with
48 additions
and
59 deletions.
There are no files selected for viewing
59 changes: 0 additions & 59 deletions
59
...es/react-native/ReactAndroid/src/test/java/com/facebook/react/bridge/ReactTestHelper.java
This file was deleted.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
...ages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridge/ReactTestHelper.kt
This file contains 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,48 @@ | ||
/* | ||
* 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.bridge | ||
|
||
import com.facebook.react.bridge.queue.MessageQueueThreadSpec | ||
import com.facebook.react.bridge.queue.ReactQueueConfiguration | ||
import com.facebook.react.bridge.queue.ReactQueueConfigurationImpl | ||
import com.facebook.react.bridge.queue.ReactQueueConfigurationSpec | ||
import com.facebook.react.uimanager.UIManagerModule | ||
import org.mockito.Mockito.mock | ||
import org.mockito.Mockito.`when` as whenever | ||
import org.robolectric.RuntimeEnvironment | ||
|
||
/** Utility for creating pre-configured instances of core react components for tests. */ | ||
object ReactTestHelper { | ||
/** | ||
* @return a ReactApplicationContext that has a CatalystInstance mock returned by | ||
* [createMockCatalystInstance] | ||
*/ | ||
@JvmStatic | ||
fun createCatalystContextForTest(): ReactApplicationContext = | ||
ReactApplicationContext(RuntimeEnvironment.application).apply { | ||
initializeWithInstance(createMockCatalystInstance()) | ||
} | ||
|
||
/** @return a CatalystInstance mock that has a default working ReactQueueConfiguration. */ | ||
@JvmStatic | ||
fun createMockCatalystInstance(): CatalystInstance { | ||
val spec: ReactQueueConfigurationSpec = | ||
ReactQueueConfigurationSpec.builder() | ||
.setJSQueueThreadSpec(MessageQueueThreadSpec.mainThreadSpec()) | ||
.setNativeModulesQueueThreadSpec(MessageQueueThreadSpec.mainThreadSpec()) | ||
.build() | ||
val reactQueueConfiguration: ReactQueueConfiguration = | ||
ReactQueueConfigurationImpl.create(spec) { e -> throw RuntimeException(e) } | ||
val reactInstance: CatalystInstance = mock(CatalystInstance::class.java) | ||
whenever(reactInstance.getReactQueueConfiguration()).thenReturn(reactQueueConfiguration) | ||
whenever(reactInstance.getNativeModule(UIManagerModule::class.java)) | ||
.thenReturn(mock(UIManagerModule::class.java)) | ||
whenever(reactInstance.isDestroyed()).thenReturn(false) | ||
return reactInstance | ||
} | ||
} |