-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: convert ReactPropForShadowNodeSetterTest to kotlin (#41774)
Summary: This PR convert `ReactPropForShadowNodeSetterTest` to kotlin as part of #38825 ## Changelog: [INTERNAL] [CHANGED] - Convert ReactPropForShadowNodeSetterTest to kotlin <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests Pull Request resolved: #41774 Test Plan: ``` ./gradlew :packages:react-native:ReactAndroid:test ``` Reviewed By: NickGerleman Differential Revision: D51882685 Pulled By: cortinico fbshipit-source-id: ff1cce824dc342200f1f5ccbb297b955747b10c8
- Loading branch information
1 parent
38d07eb
commit 186fc85
Showing
2 changed files
with
154 additions
and
165 deletions.
There are no files selected for viewing
165 changes: 0 additions & 165 deletions
165
...tAndroid/src/test/java/com/facebook/react/uimanager/ReactPropForShadowNodeSetterTest.java
This file was deleted.
Oops, something went wrong.
154 changes: 154 additions & 0 deletions
154
...actAndroid/src/test/java/com/facebook/react/uimanager/ReactPropForShadowNodeSetterTest.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,154 @@ | ||
/* | ||
* 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.uimanager | ||
|
||
import com.facebook.react.bridge.JavaOnlyMap | ||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.facebook.react.bridge.ReadableArray | ||
import com.facebook.react.bridge.ReadableMap | ||
import com.facebook.react.uimanager.annotations.ReactProp | ||
import com.facebook.react.uimanager.annotations.ReactPropGroup | ||
import com.facebook.testutils.shadows.ShadowSoLoader | ||
import com.facebook.testutils.shadows.ShadowYogaConfigProvider | ||
import com.facebook.testutils.shadows.ShadowYogaNodeFactory | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mockito.mock | ||
import org.mockito.Mockito.reset | ||
import org.mockito.Mockito.verify | ||
import org.mockito.Mockito.verifyNoMoreInteractions | ||
import org.robolectric.RobolectricTestRunner | ||
import org.robolectric.RuntimeEnvironment | ||
import org.robolectric.annotation.Config | ||
|
||
/** Test [ReactProp] and [ReactPropGroup] annotations for [ReactShadowNode] */ | ||
@RunWith(RobolectricTestRunner::class) | ||
@Config( | ||
shadows = | ||
[ShadowYogaConfigProvider::class, ShadowSoLoader::class, ShadowYogaNodeFactory::class]) | ||
class ReactPropForShadowNodeSetterTest { | ||
interface ViewManagerUpdatesReceiver { | ||
fun onBooleanSetterCalled(value: Boolean) | ||
|
||
fun onIntSetterCalled(value: Int) | ||
|
||
fun onDoubleSetterCalled(value: Double) | ||
|
||
fun onFloatSetterCalled(value: Float) | ||
|
||
fun onStringSetterCalled(value: String?) | ||
|
||
fun onBoxedBooleanSetterCalled(value: Boolean?) | ||
|
||
fun onBoxedIntSetterCalled(value: Int?) | ||
|
||
fun onArraySetterCalled(value: ReadableArray?) | ||
|
||
fun onMapSetterCalled(value: ReadableMap?) | ||
|
||
fun onFloatGroupPropSetterCalled(index: Int, value: Float) | ||
|
||
fun onIntGroupPropSetterCalled(index: Int, value: Int) | ||
|
||
fun onBoxedIntGroupPropSetterCalled(index: Int, value: Int?) | ||
} | ||
|
||
private inner class ShadowViewUnderTest( | ||
val viewManagerUpdatesReceiver: ViewManagerUpdatesReceiver | ||
) : ReactShadowNodeImpl() { | ||
|
||
init { | ||
setViewClassName("ShadowViewUnderTest") | ||
val context = ReactApplicationContext(RuntimeEnvironment.getApplication()) | ||
setThemedContext(ThemedReactContext(context, context, null, -1)) | ||
} | ||
|
||
@ReactProp(name = "boolProp") | ||
fun setBoolProp(value: Boolean) { | ||
viewManagerUpdatesReceiver.onBooleanSetterCalled(value) | ||
} | ||
|
||
@ReactProp(name = "stringProp") | ||
fun setStringProp(value: String?) { | ||
viewManagerUpdatesReceiver.onStringSetterCalled(value) | ||
} | ||
|
||
@ReactProp(name = "boxedIntProp") | ||
fun setBoxedIntProp(value: Int?) { | ||
viewManagerUpdatesReceiver.onBoxedIntSetterCalled(value) | ||
} | ||
|
||
@ReactPropGroup(names = ["floatGroupPropFirst", "floatGroupPropSecond"]) | ||
fun setFloatGroupProp(index: Int, value: Float) { | ||
viewManagerUpdatesReceiver.onFloatGroupPropSetterCalled(index, value) | ||
} | ||
} | ||
|
||
private lateinit var shadowView: ShadowViewUnderTest | ||
private lateinit var updatesReceiverMock: ViewManagerUpdatesReceiver | ||
|
||
@Before | ||
fun setup() { | ||
updatesReceiverMock = mock(ViewManagerUpdatesReceiver::class.java) | ||
shadowView = ShadowViewUnderTest(updatesReceiverMock) | ||
} | ||
|
||
@Test | ||
fun testBooleanSetter() { | ||
shadowView.updateProperties(buildStyles("boolProp", true)) | ||
verify(updatesReceiverMock).onBooleanSetterCalled(true) | ||
verifyNoMoreInteractions(updatesReceiverMock) | ||
reset(updatesReceiverMock) | ||
|
||
shadowView.updateProperties(buildStyles("boolProp", false)) | ||
verify(updatesReceiverMock).onBooleanSetterCalled(false) | ||
verifyNoMoreInteractions(updatesReceiverMock) | ||
reset(updatesReceiverMock) | ||
|
||
shadowView.updateProperties(buildStyles("boolProp", null)) | ||
verify(updatesReceiverMock).onBooleanSetterCalled(false) | ||
verifyNoMoreInteractions(updatesReceiverMock) | ||
reset(updatesReceiverMock) | ||
} | ||
|
||
@Test | ||
fun testStringSetter() { | ||
shadowView.updateProperties(buildStyles("stringProp", "someRandomString")) | ||
verify(updatesReceiverMock).onStringSetterCalled("someRandomString") | ||
verifyNoMoreInteractions(updatesReceiverMock) | ||
reset(updatesReceiverMock) | ||
|
||
shadowView.updateProperties(buildStyles("stringProp", null)) | ||
verify(updatesReceiverMock).onStringSetterCalled(null) | ||
verifyNoMoreInteractions(updatesReceiverMock) | ||
reset(updatesReceiverMock) | ||
} | ||
|
||
@Test | ||
fun testFloatGroupSetter() { | ||
shadowView.updateProperties(buildStyles("floatGroupPropFirst", 11.0)) | ||
verify(updatesReceiverMock).onFloatGroupPropSetterCalled(0, 11.0f) | ||
verifyNoMoreInteractions(updatesReceiverMock) | ||
reset(updatesReceiverMock) | ||
|
||
shadowView.updateProperties(buildStyles("floatGroupPropSecond", -111.0)) | ||
verify(updatesReceiverMock).onFloatGroupPropSetterCalled(1, -111.0f) | ||
verifyNoMoreInteractions(updatesReceiverMock) | ||
reset(updatesReceiverMock) | ||
|
||
shadowView.updateProperties(buildStyles("floatGroupPropSecond", null)) | ||
verify(updatesReceiverMock).onFloatGroupPropSetterCalled(1, 0.0f) | ||
verifyNoMoreInteractions(updatesReceiverMock) | ||
reset(updatesReceiverMock) | ||
} | ||
|
||
companion object { | ||
fun buildStyles(vararg keysAndValues: Any?) = ReactStylesDiffMap(JavaOnlyMap.of(*keysAndValues)) | ||
} | ||
} |