From 37bbfa663de8dfab6fdf1235c8f94efbd1046e91 Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Wed, 19 Jun 2019 09:09:57 -0700 Subject: [PATCH] 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 --- .../__mocks__/MessageQueueTestConfig.js | 8 ++-- .../__tests__/NativeModules-test.js | 47 +++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/Libraries/BatchedBridge/__mocks__/MessageQueueTestConfig.js b/Libraries/BatchedBridge/__mocks__/MessageQueueTestConfig.js index 43bd01f3fd9..6491c81c7ec 100644 --- a/Libraries/BatchedBridge/__mocks__/MessageQueueTestConfig.js +++ b/Libraries/BatchedBridge/__mocks__/MessageQueueTestConfig.js @@ -18,16 +18,16 @@ const remoteModulesConfig: $ReadOnlyArray = [ [ 'RemoteModule1', null, - ['remoteMethod', 'promiseMethod', 'promiseReturningMethod'], + ['remoteMethod', 'promiseMethod', 'promiseReturningMethod', 'syncMethod'], [2 /* promiseReturningMethod */], - null, + [3 /* syncMethod */], ], [ 'RemoteModule2', null, - ['remoteMethod', 'promiseMethod', 'promiseReturningMethod'], + ['remoteMethod', 'promiseMethod', 'promiseReturningMethod', 'syncMethod'], [2 /* promiseReturningMethod */], - null, + [3 /* syncMethod */], ], ]; diff --git a/Libraries/BatchedBridge/__tests__/NativeModules-test.js b/Libraries/BatchedBridge/__tests__/NativeModules-test.js index ac0c1b357e0..a2fb9bb5d72 100644 --- a/Libraries/BatchedBridge/__tests__/NativeModules-test.js +++ b/Libraries/BatchedBridge/__tests__/NativeModules-test.js @@ -181,6 +181,53 @@ describe('MessageQueue', function() { }).toThrow(); await promise2; }); + + describe('sync methods', () => { + afterEach(function() { + delete global.nativeCallSyncHook; + }); + + it('throwing an exception', function() { + global.nativeCallSyncHook = jest.fn(() => { + throw new Error('firstFailure'); + }); + + let error; + try { + NativeModules.RemoteModule1.syncMethod('paloAlto', 'menloPark'); + } catch (e) { + error = e; + } + + expect(global.nativeCallSyncHook).toBeCalledTimes(1); + expect(global.nativeCallSyncHook).toBeCalledWith( + 0, // `RemoteModule1` + 3, // `syncMethod` + ['paloAlto', 'menloPark'], + ); + expect(error).toBeInstanceOf(Error); + expect(error).toMatchObject({ + message: 'firstFailure', + }); + }); + + it('returning a value', function() { + global.nativeCallSyncHook = jest.fn(() => { + return 'secondSucc'; + }); + + const result = NativeModules.RemoteModule2.syncMethod('mac', 'windows'); + + expect(global.nativeCallSyncHook).toBeCalledTimes(1); + expect(global.nativeCallSyncHook).toBeCalledWith( + 1, // `RemoteModule2` + 3, // `syncMethod` + ['mac', 'windows'], + ); + + expect(result).toBe('secondSucc'); + }); + }); }); const linesByFile = new Map();