Skip to content

Commit f9b3a50

Browse files
rshestfacebook-github-bot
authored andcommitted
Add device event emit test for the sample C++ TurboModule (#36278)
Summary: Pull Request resolved: #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: 682c1522bf2245796605463fe67fc8aa6cce8eec
1 parent 93fdcba commit f9b3a50

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,19 @@ void NativeCxxModuleExample::voidFunc(jsi::Runtime &rt) {
121121
// Nothing to do
122122
}
123123

124+
void NativeCxxModuleExample::emitCustomDeviceEvent(
125+
jsi::Runtime &rt,
126+
jsi::String eventName) {
127+
// Test emitting device events (RCTDeviceEventEmitter.emit) from C++
128+
// TurboModule with arbitrary arguments
129+
emitDeviceEvent(
130+
rt,
131+
eventName.utf8(rt).c_str(),
132+
[](jsi::Runtime &rt, std::vector<jsi::Value> &args) {
133+
args.emplace_back(jsi::Value(true));
134+
args.emplace_back(jsi::Value(42));
135+
args.emplace_back(jsi::String::createFromAscii(rt, "stringArg"));
136+
});
137+
}
138+
124139
} // namespace facebook::react

packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ class NativeCxxModuleExample
121121
AsyncPromise<std::string> getValueWithPromise(jsi::Runtime &rt, bool error);
122122

123123
void voidFunc(jsi::Runtime &rt);
124+
125+
void emitCustomDeviceEvent(jsi::Runtime &rt, jsi::String eventName);
124126
};
125127

126128
} // namespace facebook::react

packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export interface Spec extends TurboModule {
7171
+getValueWithCallback: (callback: (value: string) => void) => void;
7272
+getValueWithPromise: (error: boolean) => Promise<string>;
7373
+voidFunc: () => void;
74+
+emitCustomDeviceEvent: (eventName: string) => void;
7475
}
7576

7677
export default (TurboModuleRegistry.get<Spec>(

packages/rn-tester/js/examples/TurboModule/NativeCxxModuleExampleExample.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import type {RootTag} from 'react-native/Libraries/ReactNative/RootTag';
1212

1313
import {
14+
DeviceEventEmitter,
1415
StyleSheet,
1516
Text,
1617
View,
@@ -53,7 +54,8 @@ type Examples =
5354
| 'getValue'
5455
| 'promise'
5556
| 'rejectPromise'
56-
| 'voidFunc';
57+
| 'voidFunc'
58+
| 'emitCustomDeviceEvent';
5759

5860
class NativeCxxModuleExampleExample extends React.Component<{||}, State> {
5961
static contextType: React$Context<RootTag> = RootTagContext;
@@ -97,6 +99,17 @@ class NativeCxxModuleExampleExample extends React.Component<{||}, State> {
9799
.then(() => {})
98100
.catch(e => this._setResult('rejectPromise', e.message)),
99101
voidFunc: () => NativeCxxModuleExample?.voidFunc(),
102+
emitCustomDeviceEvent: () => {
103+
const CUSTOM_EVENT_TYPE = 'myCustomDeviceEvent';
104+
DeviceEventEmitter.removeAllListeners(CUSTOM_EVENT_TYPE);
105+
DeviceEventEmitter.addListener(CUSTOM_EVENT_TYPE, (...args) => {
106+
this._setResult(
107+
'emitCustomDeviceEvent',
108+
`${CUSTOM_EVENT_TYPE}(${args.map(s => `${s}`).join(', ')})`,
109+
);
110+
});
111+
NativeCxxModuleExample?.emitCustomDeviceEvent(CUSTOM_EVENT_TYPE);
112+
},
100113
};
101114

102115
_setResult(

0 commit comments

Comments
 (0)