mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-24 20:35:43 +08:00
[ReactNative] Refactor BatchedBridge and MessageQueue
Summary: @public The current implementation of `MessageQueue` is huge, over-complicated and spread across `MethodQueue`, `MethodQueueMixin`, `BatchedBridge` and `BatchedBridgeFactory` Refactored in a simpler way, were it's just a `MessageQueue` class and `BatchedBridge` is only an instance of it. Test Plan: I had to make some updates to the tests, but no real update to the native side. There's also tests covering the `remoteAsync` methods, and more integration tests for UIExplorer. Verified whats being used by Android, and it should be safe, also tests Android tests have been pretty reliable. Manually testing: Create a big hierarchy, like `<ListView>` example. Use the `TimerMixin` example to generate multiple calls. Test the failure callback on the `Geolocation` example. All the calls go through this entry point, so it's hard to miss if it's broken.
This commit is contained in:
142
Libraries/Utilities/__tests__/MessageQueue-test.js
Normal file
142
Libraries/Utilities/__tests__/MessageQueue-test.js
Normal file
@@ -0,0 +1,142 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
jest.dontMock('MessageQueue');
|
||||
|
||||
var ReactUpdates = require('ReactUpdates');
|
||||
var MessageQueue = require('MessageQueue');
|
||||
|
||||
let MODULE_IDS = 0;
|
||||
let METHOD_IDS = 1;
|
||||
let PARAMS = 2;
|
||||
|
||||
let TestModule = {
|
||||
testHook1(){}, testHook2(){},
|
||||
};
|
||||
|
||||
let customRequire = (moduleName) => TestModule;
|
||||
|
||||
let assertQueue = (flushedQueue, index, moduleID, methodID, params) => {
|
||||
expect(flushedQueue[MODULE_IDS][index]).toEqual(moduleID);
|
||||
expect(flushedQueue[METHOD_IDS][index]).toEqual(methodID);
|
||||
expect(flushedQueue[PARAMS][index]).toEqual(params);
|
||||
};
|
||||
|
||||
var queue;
|
||||
|
||||
describe('MessageQueue', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
queue = new MessageQueue(
|
||||
remoteModulesConfig,
|
||||
localModulesConfig,
|
||||
customRequire,
|
||||
);
|
||||
|
||||
TestModule.testHook1 = jasmine.createSpy();
|
||||
TestModule.testHook2 = jasmine.createSpy();
|
||||
});
|
||||
|
||||
it('should enqueue native calls', () => {
|
||||
queue.__nativeCall(0, 1, [2]);
|
||||
let flushedQueue = queue.flushedQueue();
|
||||
assertQueue(flushedQueue, 0, 0, 1, [2]);
|
||||
});
|
||||
|
||||
it('should call a local function with id', () => {
|
||||
expect(TestModule.testHook1.callCount).toEqual(0);
|
||||
queue.__callFunction(0, 0, [1]);
|
||||
expect(TestModule.testHook1.callCount).toEqual(1);
|
||||
});
|
||||
|
||||
it('should call a local function with the function name', () => {
|
||||
expect(TestModule.testHook2.callCount).toEqual(0);
|
||||
queue.__callFunction('one', 'testHook2', [2]);
|
||||
expect(TestModule.testHook2.callCount).toEqual(1);
|
||||
});
|
||||
|
||||
it('should generate native modules', () => {
|
||||
queue.RemoteModules.one.remoteMethod1('foo');
|
||||
let flushedQueue = queue.flushedQueue();
|
||||
assertQueue(flushedQueue, 0, 0, 0, ['foo']);
|
||||
});
|
||||
|
||||
it('should store callbacks', () => {
|
||||
queue.RemoteModules.one.remoteMethod2('foo', () => {}, () => {});
|
||||
let flushedQueue = queue.flushedQueue();
|
||||
assertQueue(flushedQueue, 0, 0, 1, ['foo', 0, 1]);
|
||||
});
|
||||
|
||||
it('should call the stored callback', (done) => {
|
||||
var done = false;
|
||||
queue.RemoteModules.one.remoteMethod1(() => { done = true; });
|
||||
queue.__invokeCallback(1);
|
||||
expect(done).toEqual(true);
|
||||
});
|
||||
|
||||
it('should throw when calling the same callback twice', () => {
|
||||
queue.RemoteModules.one.remoteMethod1(() => {});
|
||||
queue.__invokeCallback(1);
|
||||
expect(() => queue.__invokeCallback(1)).toThrow();
|
||||
});
|
||||
|
||||
it('should throw when calling both success and failure callback', () => {
|
||||
queue.RemoteModules.one.remoteMethod1(() => {}, () => {});
|
||||
queue.__invokeCallback(1);
|
||||
expect(() => queue.__invokeCallback(0)).toThrow();
|
||||
});
|
||||
|
||||
describe('processBatch', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
ReactUpdates.batchedUpdates = (fn) => fn();
|
||||
});
|
||||
|
||||
it('should call __invokeCallback for invokeCallbackAndReturnFlushedQueue', () => {
|
||||
queue.__invokeCallback = jasmine.createSpy();
|
||||
queue.processBatch([{
|
||||
method: 'invokeCallbackAndReturnFlushedQueue',
|
||||
args: [],
|
||||
}]);
|
||||
expect(queue.__invokeCallback.callCount).toEqual(1);
|
||||
});
|
||||
|
||||
it('should call __callFunction for callFunctionReturnFlushedQueue', () => {
|
||||
queue.__callFunction = jasmine.createSpy();
|
||||
queue.processBatch([{
|
||||
method: 'callFunctionReturnFlushedQueue',
|
||||
args: [],
|
||||
}]);
|
||||
expect(queue.__callFunction.callCount).toEqual(1);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
var remoteModulesConfig = {
|
||||
'one': {
|
||||
'moduleID':0,
|
||||
'methods': {
|
||||
'remoteMethod1':{ 'type': 'remote', 'methodID': 0 },
|
||||
'remoteMethod2':{ 'type': 'remote', 'methodID': 1 },
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
var localModulesConfig = {
|
||||
'one': {
|
||||
'moduleID': 0,
|
||||
'methods': {
|
||||
'testHook1':{ 'type': 'local', 'methodID': 0 },
|
||||
'testHook2':{ 'type': 'local', 'methodID': 1 },
|
||||
}
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user