Skip to content

Commit e38641c

Browse files
fromcelticparkfacebook-github-bot
authored andcommitted
Fixed error in _getCallableModule method in MessageQueue
Reviewed By: javache Differential Revision: D5208462 fbshipit-source-id: 13f71e2b7988305eccfa91c349ff120fad9129f0
1 parent a0a7d97 commit e38641c

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

Libraries/BatchedBridge/MessageQueue.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ class MessageQueue {
152152
}
153153

154154
_getCallableModule(name: string) {
155-
return this._lazyCallableModules[name]();
155+
const getValue = this._lazyCallableModules[name];
156+
return getValue ? getValue() : null;
156157
}
157158

158159
enqueueNativeCall(moduleID: number, methodID: number, params: Array<any>, onFail: ?Function, onSucc: ?Function) {

Libraries/BatchedBridge/__tests__/MessageQueue-test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,33 @@ describe('MessageQueue', function() {
8383
queue.__invokeCallback(1);
8484
expect(() => queue.__invokeCallback(0)).toThrow();
8585
});
86+
87+
it('should throw when calling with unknown module', () => {
88+
const unknownModule = 'UnknownModule', unknownMethod = 'UnknownMethod';
89+
expect(() => queue.__callFunction(unknownModule, unknownMethod)).toThrow(
90+
`Module ${unknownModule} is not a registered callable module (calling ${unknownMethod})`,
91+
);
92+
});
93+
94+
it('should return lazily registered module', () => {
95+
const dummyModule = {}, name = 'modulesName';
96+
queue.registerLazyCallableModule(name, () => dummyModule);
97+
expect(queue._getCallableModule(name)).toEqual(dummyModule);
98+
});
99+
100+
it('should not initialize lazily registered module before it was used for the first time', () => {
101+
const dummyModule = {}, name = 'modulesName';
102+
const factory = jest.fn(() => dummyModule);
103+
queue.registerLazyCallableModule(name, factory);
104+
expect(factory).not.toHaveBeenCalled();
105+
});
106+
107+
it('should initialize lazily registered module only once', () => {
108+
const dummyModule = {}, name = 'modulesName';
109+
const factory = jest.fn(() => dummyModule);
110+
queue.registerLazyCallableModule(name, factory);
111+
queue._getCallableModule(name);
112+
queue._getCallableModule(name);
113+
expect(factory).toHaveBeenCalledTimes(1);
114+
});
86115
});

0 commit comments

Comments
 (0)