Skip to content

Commit 37bbfa6

Browse files
motiz88facebook-github-bot
authored andcommitted
Add test for sync methods (type=sync)
Summary: Adds a test for synchronous methods (`type === 'sync'`) in NativeModules. This doesn't modify any behaviour. Reviewed By: amnn Differential Revision: D15804757 fbshipit-source-id: 4db76dbd0b0b111ed9311d4b7ec35a077c377f01
1 parent 88e9098 commit 37bbfa6

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

Libraries/BatchedBridge/__mocks__/MessageQueueTestConfig.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ const remoteModulesConfig: $ReadOnlyArray<ModuleConfig> = [
1818
[
1919
'RemoteModule1',
2020
null,
21-
['remoteMethod', 'promiseMethod', 'promiseReturningMethod'],
21+
['remoteMethod', 'promiseMethod', 'promiseReturningMethod', 'syncMethod'],
2222
[2 /* promiseReturningMethod */],
23-
null,
23+
[3 /* syncMethod */],
2424
],
2525
[
2626
'RemoteModule2',
2727
null,
28-
['remoteMethod', 'promiseMethod', 'promiseReturningMethod'],
28+
['remoteMethod', 'promiseMethod', 'promiseReturningMethod', 'syncMethod'],
2929
[2 /* promiseReturningMethod */],
30-
null,
30+
[3 /* syncMethod */],
3131
],
3232
];
3333

Libraries/BatchedBridge/__tests__/NativeModules-test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,53 @@ describe('MessageQueue', function() {
181181
}).toThrow();
182182
await promise2;
183183
});
184+
185+
describe('sync methods', () => {
186+
afterEach(function() {
187+
delete global.nativeCallSyncHook;
188+
});
189+
190+
it('throwing an exception', function() {
191+
global.nativeCallSyncHook = jest.fn(() => {
192+
throw new Error('firstFailure');
193+
});
194+
195+
let error;
196+
try {
197+
NativeModules.RemoteModule1.syncMethod('paloAlto', 'menloPark');
198+
} catch (e) {
199+
error = e;
200+
}
201+
202+
expect(global.nativeCallSyncHook).toBeCalledTimes(1);
203+
expect(global.nativeCallSyncHook).toBeCalledWith(
204+
0, // `RemoteModule1`
205+
3, // `syncMethod`
206+
['paloAlto', 'menloPark'],
207+
);
208+
expect(error).toBeInstanceOf(Error);
209+
expect(error).toMatchObject({
210+
message: 'firstFailure',
211+
});
212+
});
213+
214+
it('returning a value', function() {
215+
global.nativeCallSyncHook = jest.fn(() => {
216+
return 'secondSucc';
217+
});
218+
219+
const result = NativeModules.RemoteModule2.syncMethod('mac', 'windows');
220+
221+
expect(global.nativeCallSyncHook).toBeCalledTimes(1);
222+
expect(global.nativeCallSyncHook).toBeCalledWith(
223+
1, // `RemoteModule2`
224+
3, // `syncMethod`
225+
['mac', 'windows'],
226+
);
227+
228+
expect(result).toBe('secondSucc');
229+
});
230+
});
184231
});
185232

186233
const linesByFile = new Map();

0 commit comments

Comments
 (0)