From a9bfd7bb7080fa30403a91748da178c1bc4b5645 Mon Sep 17 00:00:00 2001 From: Jatin Nagar Date: Fri, 30 Jun 2023 18:57:19 -0700 Subject: [PATCH] converted InteropEventEmitter java file to kotlin (#38144) Summary: Helping in [Kotlin-ify React Native tests](https://github.com/facebook/react-native/issues/37708), Converted [react/fabric/interop/InteropEventEmitterTest.java](https://github.com/facebook/react-native/tree/main/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/interop/InteropEventEmitterTest.java) to kotlin file ## Changelog: [INTERNAL] [CHANGED] - `InteropEventEmitterTest.java` file migrated from java to kotlin Pull Request resolved: https://github.com/facebook/react-native/pull/38144 Test Plan: Tests should pass ``` ./gradlew :packages:react-native:ReactAndroid:test ``` Reviewed By: NickGerleman Differential Revision: D47165007 Pulled By: mdvacca fbshipit-source-id: 459b0867810eb1b782eea354d74152306b9546ff --- .../interop/InteropEventEmitterTest.java | 67 ------------------- .../fabric/interop/InteropEventEmitterTest.kt | 63 +++++++++++++++++ 2 files changed, 63 insertions(+), 67 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/interop/InteropEventEmitterTest.java create mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/interop/InteropEventEmitterTest.kt diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/interop/InteropEventEmitterTest.java b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/interop/InteropEventEmitterTest.java deleted file mode 100644 index 5e86e029ef0..00000000000 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/interop/InteropEventEmitterTest.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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.fabric.interop; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import com.facebook.react.bridge.JavaOnlyMap; -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.bridge.ReactContext; -import com.facebook.react.bridge.WritableMap; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.RuntimeEnvironment; - -@RunWith(RobolectricTestRunner.class) -public class InteropEventEmitterTest { - - ReactContext mReactContext; - FakeEventDispatcher mEventDispatcher; - - @Before - public void setup() { - mReactContext = new ReactApplicationContext(RuntimeEnvironment.application); - mEventDispatcher = new FakeEventDispatcher(); - } - - @Test - public void receiveEvent_dispatchesCorrectly() { - InteropEventEmitter eventEmitter = new InteropEventEmitter(mReactContext); - eventEmitter.overrideEventDispatcher(mEventDispatcher); - - eventEmitter.receiveEvent(42, "onTest", null); - - assertEquals(1, mEventDispatcher.getRecordedDispatchedEvents().size()); - assertEquals("onTest", mEventDispatcher.getRecordedDispatchedEvents().get(0).getEventName()); - assertEquals( - InteropEvent.class, mEventDispatcher.getRecordedDispatchedEvents().get(0).getClass()); - } - - @Test - public void receiveEvent_dataIsPreserved() { - InteropEventEmitter eventEmitter = new InteropEventEmitter(mReactContext); - eventEmitter.overrideEventDispatcher(mEventDispatcher); - WritableMap eventData = JavaOnlyMap.of("color", "indigo"); - - eventEmitter.receiveEvent(42, "onTest", eventData); - - InteropEvent event = (InteropEvent) mEventDispatcher.getRecordedDispatchedEvents().get(0); - WritableMap dispatchedEventData = event.getEventData(); - assertNotNull(dispatchedEventData); - assertEquals("indigo", dispatchedEventData.getString("color")); - } - - @Test(expected = UnsupportedOperationException.class) - public void receiveTouches_isNotSupported() { - InteropEventEmitter eventEmitter = new InteropEventEmitter(mReactContext); - eventEmitter.receiveTouches("a touch", null, null); - } -} diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/interop/InteropEventEmitterTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/interop/InteropEventEmitterTest.kt new file mode 100644 index 00000000000..1554faacd47 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/interop/InteropEventEmitterTest.kt @@ -0,0 +1,63 @@ +/* + * 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.fabric.interop + +import com.facebook.react.bridge.JavaOnlyMap +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.bridge.ReactContext +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment + +@RunWith(RobolectricTestRunner::class) +class InteropEventEmitterTest { + lateinit var reactContext: ReactContext + lateinit var eventDispatcher: FakeEventDispatcher + + @Before + fun setup() { + reactContext = ReactApplicationContext(RuntimeEnvironment.application) + eventDispatcher = FakeEventDispatcher() + } + + @Test + fun receiveEvent_dispatchesCorrectly() { + val eventEmitter = InteropEventEmitter(reactContext) + eventEmitter.overrideEventDispatcher(eventDispatcher) + + eventEmitter.receiveEvent(42, "onTest", null) + + assertEquals(1, eventDispatcher.getRecordedDispatchedEvents().size) + assertEquals("onTest", eventDispatcher.getRecordedDispatchedEvents().get(0).getEventName()) + assertEquals(InteropEvent::class, eventDispatcher.getRecordedDispatchedEvents().get(0)::class) + } + + @Test + fun receiveEvent_dataIsPreserved() { + val eventEmitter = InteropEventEmitter(reactContext) + eventEmitter.overrideEventDispatcher(eventDispatcher) + val eventData = JavaOnlyMap.of("color", "indigo") + + eventEmitter.receiveEvent(42, "onTest", eventData) + + val event = eventDispatcher.getRecordedDispatchedEvents().get(0) as InteropEvent + val dispatchedEventData = event.getEventData() + assertNotNull(dispatchedEventData) + assertEquals("indigo", dispatchedEventData!!.getString("color")) + } + + @Test(expected = java.lang.UnsupportedOperationException::class) + fun receiveTouches_isNotSupported() { + val eventEmitter = InteropEventEmitter(reactContext) + eventEmitter.receiveTouches("a touch", null, null) + } +}