Skip to content

Commit

Permalink
Convert ReactTestHelper (#37768)
Browse files Browse the repository at this point in the history
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
abdennour-jebbar-nw authored and facebook-github-bot committed Jun 9, 2023
1 parent 0aa3308 commit 6ebee63
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 59 deletions.

This file was deleted.

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
}
}

0 comments on commit 6ebee63

Please sign in to comment.