Add device event emit test for the sample C++ TurboModule (#36278)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/36278

[Changelog][Internal]

The diff creates a test clause for [TurboModule::emitDeviceEvent C++ API for TurboModules](https://www.internalfb.com/code/fbsource/[929870c905c8fe68cb330ce96bda7eb703bb6ae6]/xplat/js/react-native-github/ReactCommon/react/nativemodule/core/ReactCommon/TurboModule.h?lines=90), which can be seen in either Catalyst or RNTester.

Reviewed By: cipolleschi

Differential Revision: D43466327

fbshipit-source-id: ff4c111b4beaab72b13d2bd89ed03023c9c7b3cf
This commit is contained in:
Ruslan Shestopalyuk
2023-02-24 06:49:14 -08:00
committed by Facebook GitHub Bot
parent bf03e86bc7
commit e106a62b1a
4 changed files with 32 additions and 1 deletions
@@ -11,6 +11,7 @@
import type {RootTag} from 'react-native/Libraries/ReactNative/RootTag';
import {
DeviceEventEmitter,
StyleSheet,
Text,
View,
@@ -53,7 +54,8 @@ type Examples =
| 'getValue'
| 'promise'
| 'rejectPromise'
| 'voidFunc';
| 'voidFunc'
| 'emitCustomDeviceEvent';
class NativeCxxModuleExampleExample extends React.Component<{||}, State> {
static contextType: React$Context<RootTag> = RootTagContext;
@@ -97,6 +99,17 @@ class NativeCxxModuleExampleExample extends React.Component<{||}, State> {
.then(() => {})
.catch(e => this._setResult('rejectPromise', e.message)),
voidFunc: () => NativeCxxModuleExample?.voidFunc(),
emitCustomDeviceEvent: () => {
const CUSTOM_EVENT_TYPE = 'myCustomDeviceEvent';
DeviceEventEmitter.removeAllListeners(CUSTOM_EVENT_TYPE);
DeviceEventEmitter.addListener(CUSTOM_EVENT_TYPE, (...args) => {
this._setResult(
'emitCustomDeviceEvent',
`${CUSTOM_EVENT_TYPE}(${args.map(s => `${s}`).join(', ')})`,
);
});
NativeCxxModuleExample?.emitCustomDeviceEvent(CUSTOM_EVENT_TYPE);
},
};
_setResult(