From e38641cd732d77d1873bce470513af2cf80e13ec Mon Sep 17 00:00:00 2001 From: Alex Dvornikov Date: Thu, 8 Jun 2017 12:40:19 -0700 Subject: [PATCH] Fixed error in _getCallableModule method in MessageQueue Reviewed By: javache Differential Revision: D5208462 fbshipit-source-id: 13f71e2b7988305eccfa91c349ff120fad9129f0 --- Libraries/BatchedBridge/MessageQueue.js | 3 +- .../__tests__/MessageQueue-test.js | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/Libraries/BatchedBridge/MessageQueue.js b/Libraries/BatchedBridge/MessageQueue.js index 173b17709..579dc3065 100644 --- a/Libraries/BatchedBridge/MessageQueue.js +++ b/Libraries/BatchedBridge/MessageQueue.js @@ -152,7 +152,8 @@ class MessageQueue { } _getCallableModule(name: string) { - return this._lazyCallableModules[name](); + const getValue = this._lazyCallableModules[name]; + return getValue ? getValue() : null; } enqueueNativeCall(moduleID: number, methodID: number, params: Array, onFail: ?Function, onSucc: ?Function) { diff --git a/Libraries/BatchedBridge/__tests__/MessageQueue-test.js b/Libraries/BatchedBridge/__tests__/MessageQueue-test.js index 1da3557ca..7b6244ee7 100644 --- a/Libraries/BatchedBridge/__tests__/MessageQueue-test.js +++ b/Libraries/BatchedBridge/__tests__/MessageQueue-test.js @@ -83,4 +83,33 @@ describe('MessageQueue', function() { queue.__invokeCallback(1); expect(() => queue.__invokeCallback(0)).toThrow(); }); + + it('should throw when calling with unknown module', () => { + const unknownModule = 'UnknownModule', unknownMethod = 'UnknownMethod'; + expect(() => queue.__callFunction(unknownModule, unknownMethod)).toThrow( + `Module ${unknownModule} is not a registered callable module (calling ${unknownMethod})`, + ); + }); + + it('should return lazily registered module', () => { + const dummyModule = {}, name = 'modulesName'; + queue.registerLazyCallableModule(name, () => dummyModule); + expect(queue._getCallableModule(name)).toEqual(dummyModule); + }); + + it('should not initialize lazily registered module before it was used for the first time', () => { + const dummyModule = {}, name = 'modulesName'; + const factory = jest.fn(() => dummyModule); + queue.registerLazyCallableModule(name, factory); + expect(factory).not.toHaveBeenCalled(); + }); + + it('should initialize lazily registered module only once', () => { + const dummyModule = {}, name = 'modulesName'; + const factory = jest.fn(() => dummyModule); + queue.registerLazyCallableModule(name, factory); + queue._getCallableModule(name); + queue._getCallableModule(name); + expect(factory).toHaveBeenCalledTimes(1); + }); });