diff --git a/Libraries/BatchedBridge/MessageQueue.js b/Libraries/BatchedBridge/MessageQueue.js index 173b1770944f26..579dc306533843 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 1da3557ca576ca..7b6244ee7ecfdf 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); + }); });