Skip to content

Commit 5c0a48f

Browse files
christophpurrermeta-codesync[bot]
authored andcommitted
Align SampleTurboModuleExample UI with NativeCxxModuleExampleExample (#57985)
Summary: Pull Request resolved: #57985 The two RNTester TurboModule example screens had drifted apart, making it hard to compare TurboModule and C++ TurboModule behaviour side by side. Align `SampleTurboModuleExample` with the style already used by `NativeCxxModuleExampleExample`: - Order and group the entries in `_tests` the same way (callback, ArrayBuffer group, `get*` group, promises, `voidFunc`), so the buttons render in the same order on both screens. - Make the `Examples` union match the tests that actually exist; it still listed many entries copied from the C++ example that `SampleTurboModule` does not implement (`getCustomHostObject`, `getSet`, `setMenuItem`, ...) and was missing `getEnum`, `getRootTag` and `getUnsafeObject`. - Add the missing `installJSIBindings` entry to `ErrorExamples` and type `_renderResult` as `Examples | ErrorExamples`. - Surface rejected promises from the error tests in the UI instead of only logging them to the console, matching the other screen. - Fix `getUnsafeObject` to call `getUnsafeObject` instead of `getObject`. - Merge the duplicated `NativeSampleTurboModule` imports and drop a stale Flow suppression. Changelog: [Internal] Differential Revision: D116375214
1 parent 4cd800f commit 5c0a48f

1 file changed

Lines changed: 44 additions & 68 deletions

File tree

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

Lines changed: 44 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ import RNTesterText from '../../components/RNTesterText';
1414
import styles from './TurboModuleExampleCommon';
1515
import * as React from 'react';
1616
import {FlatList, RootTagContext, TouchableOpacity, View} from 'react-native';
17-
import NativeSampleTurboModule from 'react-native/Libraries/TurboModule/samples/NativeSampleTurboModule';
18-
import {EnumInt} from 'react-native/Libraries/TurboModule/samples/NativeSampleTurboModule';
17+
import NativeSampleTurboModule, {
18+
EnumInt,
19+
} from 'react-native/Libraries/TurboModule/samples/NativeSampleTurboModule';
1920

2021
type State = {
2122
testResults: {
@@ -31,39 +32,31 @@ type State = {
3132
type Examples =
3233
| 'callback'
3334
| 'getArray'
35+
| 'getArrayBuffer'
36+
| 'createNativeBuffer'
37+
| 'processAsyncBuffer'
38+
| 'getAsyncBuffer'
3439
| 'getBool'
3540
| 'getConstants'
36-
| 'getCustomEnum'
37-
| 'getCustomHostObject'
38-
| 'getBinaryTreeNode'
39-
| 'getGraphNode'
40-
| 'getNumEnum'
41-
| 'getStrEnum'
42-
| 'getMap'
41+
| 'getEnum'
4342
| 'getNumber'
4443
| 'getObject'
45-
| 'getSet'
44+
| 'getRootTag'
4645
| 'getString'
47-
| 'getUnion'
46+
| 'getUnsafeObject'
4847
| 'getValue'
49-
| 'getArrayBuffer'
50-
| 'createNativeBuffer'
51-
| 'processAsyncBuffer'
52-
| 'getAsyncBuffer'
5348
| 'promise'
5449
| 'rejectPromise'
55-
| 'voidFunc'
56-
| 'setMenuItem'
57-
| 'optionalArgs'
58-
| 'emitDeviceEvent';
50+
| 'voidFunc';
5951

6052
type ErrorExamples =
6153
| 'voidFuncThrows'
6254
| 'getObjectThrows'
6355
| 'promiseThrows'
6456
| 'voidFuncAssert'
6557
| 'getObjectAssert'
66-
| 'promiseAssert';
58+
| 'promiseAssert'
59+
| 'installJSIBindings';
6760

6861
class SampleTurboModuleExample extends React.Component<{}, State> {
6962
static contextType: React.Context<RootTag> = RootTagContext;
@@ -80,38 +73,12 @@ class SampleTurboModuleExample extends React.Component<{}, State> {
8073
NativeSampleTurboModule.getValueWithCallback(callbackValue =>
8174
this._setResult('callback', callbackValue),
8275
),
83-
promise: () =>
84-
NativeSampleTurboModule.getValueWithPromise(false).then(valuePromise =>
85-
this._setResult('promise', valuePromise),
86-
),
87-
rejectPromise: () =>
88-
NativeSampleTurboModule.getValueWithPromise(true)
89-
.then(() => {})
90-
.catch(e => {
91-
this._setResult('rejectPromise', e.message);
92-
}),
93-
getConstants: () => NativeSampleTurboModule.getConstants(),
94-
voidFunc: () => NativeSampleTurboModule.voidFunc(),
95-
getBool: () => NativeSampleTurboModule.getBool(true),
96-
getEnum: () =>
97-
NativeSampleTurboModule.getEnum
98-
? NativeSampleTurboModule.getEnum(EnumInt.A)
99-
: null,
100-
getNumber: () => NativeSampleTurboModule.getNumber(99.95),
101-
getString: () => NativeSampleTurboModule.getString('Hello'),
10276
getArray: () =>
10377
NativeSampleTurboModule.getArray([
10478
{a: 1, b: 'foo'},
10579
{a: 2, b: 'bar'},
10680
null,
10781
]),
108-
getObject: () =>
109-
NativeSampleTurboModule.getObject({a: 1, b: 'foo', c: null}),
110-
getUnsafeObject: () =>
111-
NativeSampleTurboModule.getObject({a: 1, b: 'foo', c: null}),
112-
getRootTag: () => NativeSampleTurboModule.getRootTag(this.context),
113-
getValue: () =>
114-
NativeSampleTurboModule.getValue(5, 'test', {a: 1, b: 'foo'}),
11582
getArrayBuffer: () => {
11683
const input = new Uint8Array([1, 2, 3, 4]);
11784
const result = NativeSampleTurboModule.getArrayBuffer(input.buffer);
@@ -135,6 +102,30 @@ class SampleTurboModuleExample extends React.Component<{}, State> {
135102
NativeSampleTurboModule.getAsyncBuffer(4).then(buffer =>
136103
this._setResult('getAsyncBuffer', Array.from(new Uint8Array(buffer))),
137104
),
105+
getBool: () => NativeSampleTurboModule.getBool(true),
106+
getConstants: () => NativeSampleTurboModule.getConstants(),
107+
getEnum: () =>
108+
NativeSampleTurboModule.getEnum
109+
? NativeSampleTurboModule.getEnum(EnumInt.A)
110+
: null,
111+
getNumber: () => NativeSampleTurboModule.getNumber(99.95),
112+
getObject: () =>
113+
NativeSampleTurboModule.getObject({a: 1, b: 'foo', c: null}),
114+
getRootTag: () => NativeSampleTurboModule.getRootTag(this.context),
115+
getString: () => NativeSampleTurboModule.getString('Hello'),
116+
getUnsafeObject: () =>
117+
NativeSampleTurboModule.getUnsafeObject({a: 1, b: 'foo', c: null}),
118+
getValue: () =>
119+
NativeSampleTurboModule.getValue(5, 'test', {a: 1, b: 'foo'}),
120+
promise: () =>
121+
NativeSampleTurboModule.getValueWithPromise(false).then(valuePromise =>
122+
this._setResult('promise', valuePromise),
123+
),
124+
rejectPromise: () =>
125+
NativeSampleTurboModule.getValueWithPromise(true)
126+
.then(() => {})
127+
.catch(e => this._setResult('rejectPromise', e.message)),
128+
voidFunc: () => NativeSampleTurboModule.voidFunc(),
138129
};
139130

140131
// $FlowFixMe[missing-local-annot]
@@ -143,51 +134,39 @@ class SampleTurboModuleExample extends React.Component<{}, State> {
143134
try {
144135
NativeSampleTurboModule.voidFuncThrows?.();
145136
} catch (e) {
146-
console.error(e);
147137
return e.message;
148138
}
149139
},
150140
getObjectThrows: () => {
151141
try {
152142
NativeSampleTurboModule.getObjectThrows?.({a: 1, b: 'foo', c: null});
153143
} catch (e) {
154-
console.error(e);
155144
return e.message;
156145
}
157146
},
158-
promiseThrows: () => {
147+
promiseThrows: () =>
159148
NativeSampleTurboModule.promiseThrows?.()
160149
.then(() => {})
161-
.catch(e => {
162-
console.error(e);
163-
});
164-
},
150+
.catch(e => this._setResult('promiseThrows', e.message)),
165151
voidFuncAssert: () => {
166152
try {
167153
NativeSampleTurboModule.voidFuncAssert?.();
168154
} catch (e) {
169-
console.error(e);
170155
return e.message;
171156
}
172157
},
173158
getObjectAssert: () => {
174159
try {
175160
NativeSampleTurboModule.getObjectAssert?.({a: 1, b: 'foo', c: null});
176161
} catch (e) {
177-
console.error(e);
178162
return e.message;
179163
}
180164
},
181-
promiseAssert: () => {
165+
promiseAssert: () =>
182166
NativeSampleTurboModule.promiseAssert?.()
183167
.then(() => {})
184-
.catch(e => {
185-
console.error(e);
186-
});
187-
},
188-
installJSIBindings: () => {
189-
return global.__SampleTurboModuleJSIBindings;
190-
},
168+
.catch(e => this._setResult('promiseAssert', e.message)),
169+
installJSIBindings: () => global.__SampleTurboModuleJSIBindings,
191170
};
192171

193172
_setResult(
@@ -204,9 +183,6 @@ class SampleTurboModuleExample extends React.Component<{}, State> {
204183
| Array<$FlowFixMe>,
205184
) {
206185
this.setState(({testResults}) => ({
207-
/* $FlowFixMe[cannot-spread-indexer] (>=0.122.0 site=react_native_fb)
208-
* This comment suppresses an error found when Flow v0.122.0 was
209-
* deployed. To see the error, delete this comment and run Flow. */
210186
testResults: {
211187
...testResults,
212188
/* $FlowFixMe[invalid-computed-prop] (>=0.111.0 site=react_native_fb)
@@ -217,7 +193,7 @@ class SampleTurboModuleExample extends React.Component<{}, State> {
217193
}));
218194
}
219195

220-
_renderResult(name: string): React.Node {
196+
_renderResult(name: Examples | ErrorExamples): React.Node {
221197
const result = this.state.testResults[name] || {};
222198
return (
223199
<View style={styles.result}>
@@ -282,7 +258,7 @@ class SampleTurboModuleExample extends React.Component<{}, State> {
282258
)
283259
}>
284260
<RNTesterText style={styles.buttonTextLarge}>
285-
Run all tests
261+
Run function call tests
286262
</RNTesterText>
287263
</TouchableOpacity>
288264
<TouchableOpacity

0 commit comments

Comments
 (0)