Fixed error in _getCallableModule method in MessageQueue

Reviewed By: javache

Differential Revision: D5208462

fbshipit-source-id: 13f71e2b7988305eccfa91c349ff120fad9129f0
This commit is contained in:
Alex Dvornikov 2017-06-08 12:40:19 -07:00 committed by Facebook Github Bot
parent a0a7d97c7b
commit e38641cd73
2 changed files with 31 additions and 1 deletions

View File

@ -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<any>, onFail: ?Function, onSucc: ?Function) {

View File

@ -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);
});
});