Skip to content
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

Convert CompositeReactPackageTest to Kotlin #37734

Closed
Closed

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* 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 com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.uimanager.ViewManager
import java.util.*
import org.junit.Assert
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.mock
import org.mockito.Mockito.verify
import org.mockito.Mockito.`when` as whenever
import org.mockito.MockitoAnnotations
import org.powermock.core.classloader.annotations.PowerMockIgnore
import org.powermock.modules.junit4.rule.PowerMockRule
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
@PowerMockIgnore("org.mockito.*", "org.robolectric.*", "androidx.*", "android.*")
class CompositeReactPackageTest {
@get:Rule var rule = PowerMockRule()

@Mock lateinit var packageNo1: ReactPackage

@Mock lateinit var packageNo2: ReactPackage

@Mock lateinit var packageNo3: ReactPackage

@Mock lateinit var reactContext: ReactApplicationContext

@Before
fun initMocks() {
MockitoAnnotations.initMocks(this)
}

@Test
fun testThatCreateNativeModulesIsCalledOnAllPackages() {
// Given
val composite = CompositeReactPackage(packageNo1, packageNo2, packageNo3)

// When
composite.createNativeModules(reactContext)

// Then
verify(packageNo1).createNativeModules(reactContext)
verify(packageNo2).createNativeModules(reactContext)
verify(packageNo3).createNativeModules(reactContext)
}

@Test
fun testThatCreateViewManagersIsCalledOnAllPackages() {
// Given
val composite = CompositeReactPackage(packageNo1, packageNo2, packageNo3)

// When
composite.createViewManagers(reactContext)

// Then
verify(packageNo1).createViewManagers(reactContext)
verify(packageNo2).createViewManagers(reactContext)
verify(packageNo3).createViewManagers(reactContext)
}

@Test
fun testThatCompositeReturnsASumOfNativeModules() {
// Given
val composite = CompositeReactPackage(packageNo1, packageNo2)
val moduleNo1 = mock(NativeModule::class.java)
whenever(moduleNo1.name).thenReturn("ModuleNo1")

// module2 and module3 will share same name, composite should return only the latter one
val sameModuleName = "SameModuleName"
val moduleNo2 = mock(NativeModule::class.java)
whenever(moduleNo2.name).thenReturn(sameModuleName)
val moduleNo3 = mock(NativeModule::class.java)
whenever(moduleNo3.name).thenReturn(sameModuleName)
val moduleNo4 = mock(NativeModule::class.java)
whenever(moduleNo4.name).thenReturn("ModuleNo4")
whenever(packageNo1.createNativeModules(reactContext))
.thenReturn(listOf(moduleNo1, moduleNo2))
whenever(packageNo2.createNativeModules(reactContext))
.thenReturn(listOf(moduleNo3, moduleNo4))

// When
val compositeModules = composite.createNativeModules(reactContext)

// Then

// Wrapping lists into sets to be order-independent.
// Note that there should be no module2 returned.
val expected: Set<NativeModule> = setOf(moduleNo1, moduleNo3, moduleNo4)
val actual: Set<NativeModule> = compositeModules.toSet()
Assert.assertEquals(expected, actual)
}

@Test
fun testThatCompositeReturnsASumOfViewManagers() {
// Given
val composite = CompositeReactPackage(packageNo1, packageNo2)
val managerNo1 = mock(ViewManager::class.java)
whenever(managerNo1.name).thenReturn("ManagerNo1")

// managerNo2 and managerNo3 will share same name, composite should return only the latter
// one
val sameModuleName = "SameModuleName"
val managerNo2 = mock(ViewManager::class.java)
whenever(managerNo2.name).thenReturn(sameModuleName)
val managerNo3 = mock(ViewManager::class.java)
whenever(managerNo3.name).thenReturn(sameModuleName)
val managerNo4 = mock(ViewManager::class.java)
whenever(managerNo4.name).thenReturn("ManagerNo4")
whenever(packageNo1.createViewManagers(reactContext))
.thenReturn(listOf(managerNo1, managerNo2))
whenever(packageNo2.createViewManagers(reactContext))
.thenReturn(listOf(managerNo3, managerNo4))

// When
val compositeModules = composite.createViewManagers(reactContext)

// Then

// Wrapping lists into sets to be order-independent.
// Note that there should be no managerNo2 returned.
val expected: Set<ViewManager<*, *>> = setOf(managerNo1, managerNo3, managerNo4)
val actual: Set<ViewManager<*, *>> = compositeModules.toSet()
Assert.assertEquals(expected, actual)
}
}