Skip to content

Commit

Permalink
Fixed error in _getCallableModule method in MessageQueue
Browse files Browse the repository at this point in the history
Reviewed By: javache

Differential Revision: D5208462

fbshipit-source-id: 13f71e2b7988305eccfa91c349ff120fad9129f0
  • Loading branch information
fromcelticpark authored and facebook-github-bot committed Jun 8, 2017
1 parent a0a7d97 commit e38641c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Libraries/BatchedBridge/MessageQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
29 changes: 29 additions & 0 deletions Libraries/BatchedBridge/__tests__/MessageQueue-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit e38641c

Please sign in to comment.