mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-03-29 22:41:56 +08:00
Summary:As far as we agreed to merge `rnpm` into react-native core, we need to align our dependencies to exclude duplications. One of the steps forward would be to use the same utilities library. According to the thread on fb, everybody is fine with replacing underscore by lodash (which we use internally for rnpm). So, here we go! cc mkonicek davidaurelio grabbou **Test plan** ``` $ npm test ```  **Code formatting** Changes are aligned with the current [style guide](https://github.com/facebook/react-native/blob/master/CONTRIBUTING.md#style-guide). Closes https://github.com/facebook/react-native/pull/6030 Differential Revision: D3016271 Pulled By: davidaurelio fb-gh-sync-id: c4f6776a7de7470283d3ca5a8b56e423247f5e45 shipit-source-id: c4f6776a7de7470283d3ca5a8b56e423247f5e45
82 lines
2.9 KiB
JavaScript
82 lines
2.9 KiB
JavaScript
/**
|
|
* 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.setMock('uglify-js')
|
|
.mock('child_process')
|
|
.dontMock('../');
|
|
|
|
var SocketInterface = require('../');
|
|
var SocketClient = require('../SocketClient');
|
|
var childProcess = require('child_process');
|
|
var fs = require('fs');
|
|
|
|
describe('SocketInterface', () => {
|
|
describe('getOrCreateSocketFor', () => {
|
|
pit('creates socket path by hashing options', () => {
|
|
fs.existsSync = jest.genMockFn().mockImpl(() => true);
|
|
fs.unlinkSync = jest.genMockFn();
|
|
let callback;
|
|
|
|
childProcess.spawn.mockImpl(() => ({
|
|
on: (event, cb) => callback = cb,
|
|
send: (message) => {
|
|
setImmediate(() => callback({ type: 'createdServer' }));
|
|
},
|
|
unref: () => undefined,
|
|
disconnect: () => undefined,
|
|
}));
|
|
|
|
// Check that given two equivelant server options, we end up with the same
|
|
// socket path.
|
|
const options1 = { projectRoots: ['/root'], transformModulePath: '/root/foo' };
|
|
const options2 = { transformModulePath: '/root/foo', projectRoots: ['/root'] };
|
|
const options3 = { projectRoots: ['/root', '/root2'] };
|
|
|
|
return SocketInterface.getOrCreateSocketFor(options1).then(() => {
|
|
expect(SocketClient.create).toBeCalled();
|
|
return SocketInterface.getOrCreateSocketFor(options2).then(() => {
|
|
expect(SocketClient.create.mock.calls.length).toBe(2);
|
|
expect(SocketClient.create.mock.calls[0]).toEqual(SocketClient.create.mock.calls[1]);
|
|
return SocketInterface.getOrCreateSocketFor(options3).then(() => {
|
|
expect(SocketClient.create.mock.calls.length).toBe(3);
|
|
expect(SocketClient.create.mock.calls[1]).not.toEqual(SocketClient.create.mock.calls[2]);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
pit('should fork a server', () => {
|
|
fs.existsSync = jest.genMockFn().mockImpl(() => false);
|
|
fs.unlinkSync = jest.genMockFn();
|
|
let sockPath;
|
|
let callback;
|
|
|
|
childProcess.spawn.mockImpl(() => ({
|
|
on: (event, cb) => callback = cb,
|
|
send: (message) => {
|
|
expect(message.type).toBe('createSocketServer');
|
|
expect(message.data.options).toEqual({ projectRoots: ['/root'] });
|
|
expect(message.data.sockPath).toContain('react-packager');
|
|
sockPath = message.data.sockPath;
|
|
|
|
setImmediate(() => callback({ type: 'createdServer' }));
|
|
},
|
|
unref: () => undefined,
|
|
disconnect: () => undefined,
|
|
}));
|
|
|
|
return SocketInterface.getOrCreateSocketFor({ projectRoots: ['/root'] })
|
|
.then(() => {
|
|
expect(SocketClient.create).toBeCalledWith(sockPath);
|
|
});
|
|
});
|
|
});
|
|
});
|