mirror of https://github.com/status-im/metro.git
[react-packager] Modernize `Server-test` by using ES6 features
This commit is contained in:
parent
7664d5fb76
commit
c3be08e66b
|
@ -8,69 +8,54 @@
|
||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
jest.setMock('worker-farm', function() { return function() {}; })
|
jest.setMock('worker-farm', function() { return () => {}; })
|
||||||
.dontMock('os')
|
.dontMock('os')
|
||||||
.dontMock('path')
|
.dontMock('path')
|
||||||
.dontMock('url')
|
.dontMock('url')
|
||||||
.setMock('timers', {
|
.setMock('timers', { setImmediate: (fn) => setTimeout(fn, 0) })
|
||||||
setImmediate: function(fn) {
|
|
||||||
return setTimeout(fn, 0);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.setMock('uglify-js')
|
.setMock('uglify-js')
|
||||||
.dontMock('../');
|
.dontMock('../');
|
||||||
|
|
||||||
var Promise = require('promise');
|
const Promise = require('promise');
|
||||||
|
|
||||||
describe('processRequest', function() {
|
describe('processRequest', () => {
|
||||||
var server;
|
var server;
|
||||||
var Bundler;
|
var Bundler;
|
||||||
var FileWatcher;
|
var FileWatcher;
|
||||||
|
|
||||||
var options = {
|
const options = {
|
||||||
projectRoots: ['root'],
|
projectRoots: ['root'],
|
||||||
blacklistRE: null,
|
blacklistRE: null,
|
||||||
cacheVersion: null,
|
cacheVersion: null,
|
||||||
polyfillModuleNames: null
|
polyfillModuleNames: null
|
||||||
};
|
};
|
||||||
|
|
||||||
var makeRequest = function(requestHandler, requrl) {
|
const makeRequest = (reqHandler, requrl) => new Promise(resolve =>
|
||||||
return new Promise(function(resolve) {
|
reqHandler(
|
||||||
requestHandler(
|
{ url: requrl },
|
||||||
{ url: requrl },
|
{
|
||||||
{
|
setHeader: jest.genMockFunction(),
|
||||||
setHeader: jest.genMockFunction(),
|
end: res => resolve(res),
|
||||||
end: function(res) {
|
},
|
||||||
resolve(res);
|
{ next: () => {} },
|
||||||
}
|
)
|
||||||
},
|
);
|
||||||
{
|
|
||||||
next: function() {}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
var invalidatorFunc = jest.genMockFunction();
|
const invalidatorFunc = jest.genMockFunction();
|
||||||
var watcherFunc = jest.genMockFunction();
|
const watcherFunc = jest.genMockFunction();
|
||||||
var requestHandler;
|
var requestHandler;
|
||||||
var triggerFileChange;
|
var triggerFileChange;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(() => {
|
||||||
Bundler = require('../../Bundler');
|
Bundler = require('../../Bundler');
|
||||||
FileWatcher = require('../../FileWatcher');
|
FileWatcher = require('../../FileWatcher');
|
||||||
|
|
||||||
Bundler.prototype.bundle = jest.genMockFunction().mockImpl(function() {
|
Bundler.prototype.bundle = jest.genMockFunction().mockImpl(() =>
|
||||||
return Promise.resolve({
|
Promise.resolve({
|
||||||
getSource: function() {
|
getSource: () => 'this is the source',
|
||||||
return 'this is the source';
|
getSourceMap: () => 'this is the source map',
|
||||||
},
|
})
|
||||||
getSourceMap: function() {
|
);
|
||||||
return 'this is the source map';
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
FileWatcher.prototype.on = function(eventType, callback) {
|
FileWatcher.prototype.on = function(eventType, callback) {
|
||||||
if (eventType !== 'all') {
|
if (eventType !== 'all') {
|
||||||
|
@ -83,43 +68,43 @@ describe('processRequest', function() {
|
||||||
|
|
||||||
Bundler.prototype.invalidateFile = invalidatorFunc;
|
Bundler.prototype.invalidateFile = invalidatorFunc;
|
||||||
|
|
||||||
var Server = require('../');
|
const Server = require('../');
|
||||||
server = new Server(options);
|
server = new Server(options);
|
||||||
requestHandler = server.processRequest.bind(server);
|
requestHandler = server.processRequest.bind(server);
|
||||||
});
|
});
|
||||||
|
|
||||||
pit('returns JS bundle source on request of *.bundle',function() {
|
pit('returns JS bundle source on request of *.bundle', () => {
|
||||||
return makeRequest(
|
return makeRequest(
|
||||||
requestHandler,
|
requestHandler,
|
||||||
'mybundle.bundle?runModule=true'
|
'mybundle.bundle?runModule=true'
|
||||||
).then(function(response) {
|
).then(response =>
|
||||||
expect(response).toEqual('this is the source');
|
expect(response).toEqual('this is the source')
|
||||||
});
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
pit('returns JS bundle source on request of *.bundle (compat)',function() {
|
pit('returns JS bundle source on request of *.bundle (compat)', () => {
|
||||||
return makeRequest(
|
return makeRequest(
|
||||||
requestHandler,
|
requestHandler,
|
||||||
'mybundle.runModule.bundle'
|
'mybundle.runModule.bundle'
|
||||||
).then(function(response) {
|
).then(response =>
|
||||||
expect(response).toEqual('this is the source');
|
expect(response).toEqual('this is the source')
|
||||||
});
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
pit('returns sourcemap on request of *.map', function() {
|
pit('returns sourcemap on request of *.map', () => {
|
||||||
return makeRequest(
|
return makeRequest(
|
||||||
requestHandler,
|
requestHandler,
|
||||||
'mybundle.map?runModule=true'
|
'mybundle.map?runModule=true'
|
||||||
).then(function(response) {
|
).then(response =>
|
||||||
expect(response).toEqual('"this is the source map"');
|
expect(response).toEqual('"this is the source map"')
|
||||||
});
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
pit('works with .ios.js extension', function() {
|
pit('works with .ios.js extension', () => {
|
||||||
return makeRequest(
|
return makeRequest(
|
||||||
requestHandler,
|
requestHandler,
|
||||||
'index.ios.includeRequire.bundle'
|
'index.ios.includeRequire.bundle'
|
||||||
).then(function(response) {
|
).then(response => {
|
||||||
expect(response).toEqual('this is the source');
|
expect(response).toEqual('this is the source');
|
||||||
expect(Bundler.prototype.bundle).toBeCalledWith(
|
expect(Bundler.prototype.bundle).toBeCalledWith(
|
||||||
'index.ios.js',
|
'index.ios.js',
|
||||||
|
@ -130,81 +115,75 @@ describe('processRequest', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
pit('watches all files in projectRoot', function() {
|
pit('watches all files in projectRoot', () => {
|
||||||
return makeRequest(
|
return makeRequest(
|
||||||
requestHandler,
|
requestHandler,
|
||||||
'mybundle.bundle?runModule=true'
|
'mybundle.bundle?runModule=true'
|
||||||
).then(function() {
|
).then(() => {
|
||||||
expect(watcherFunc.mock.calls[0][0]).toEqual('all');
|
expect(watcherFunc.mock.calls[0][0]).toEqual('all');
|
||||||
expect(watcherFunc.mock.calls[0][1]).not.toBe(null);
|
expect(watcherFunc.mock.calls[0][1]).not.toBe(null);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('file changes', () => {
|
||||||
describe('file changes', function() {
|
pit('invalides files in bundle when file is updated', () => {
|
||||||
pit('invalides files in bundle when file is updated', function() {
|
|
||||||
return makeRequest(
|
return makeRequest(
|
||||||
requestHandler,
|
requestHandler,
|
||||||
'mybundle.bundle?runModule=true'
|
'mybundle.bundle?runModule=true'
|
||||||
).then(function() {
|
).then(() => {
|
||||||
var onFileChange = watcherFunc.mock.calls[0][1];
|
const onFileChange = watcherFunc.mock.calls[0][1];
|
||||||
onFileChange('all','path/file.js', options.projectRoots[0]);
|
onFileChange('all','path/file.js', options.projectRoots[0]);
|
||||||
expect(invalidatorFunc.mock.calls[0][0]).toEqual('root/path/file.js');
|
expect(invalidatorFunc.mock.calls[0][0]).toEqual('root/path/file.js');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
pit('rebuilds the bundles that contain a file when that file is changed', function() {
|
pit('rebuilds the bundles that contain a file when that file is changed', () => {
|
||||||
var bundleFunc = jest.genMockFunction();
|
const bundleFunc = jest.genMockFunction();
|
||||||
bundleFunc
|
bundleFunc
|
||||||
.mockReturnValueOnce(
|
.mockReturnValueOnce(
|
||||||
Promise.resolve({
|
Promise.resolve({
|
||||||
getSource: function() {
|
getSource: () => 'this is the first source',
|
||||||
return 'this is the first source';
|
getSourceMap: () => {},
|
||||||
},
|
|
||||||
getSourceMap: function() {},
|
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
.mockReturnValue(
|
.mockReturnValue(
|
||||||
Promise.resolve({
|
Promise.resolve({
|
||||||
getSource: function() {
|
getSource: () => 'this is the rebuilt source',
|
||||||
return 'this is the rebuilt source';
|
getSourceMap: () => {},
|
||||||
},
|
|
||||||
getSourceMap: function() {},
|
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
Bundler.prototype.bundle = bundleFunc;
|
Bundler.prototype.bundle = bundleFunc;
|
||||||
|
|
||||||
var Server = require('../../Server');
|
const Server = require('../../Server');
|
||||||
server = new Server(options);
|
server = new Server(options);
|
||||||
|
|
||||||
requestHandler = server.processRequest.bind(server);
|
requestHandler = server.processRequest.bind(server);
|
||||||
|
|
||||||
|
|
||||||
return makeRequest(requestHandler, 'mybundle.bundle?runModule=true')
|
return makeRequest(requestHandler, 'mybundle.bundle?runModule=true')
|
||||||
.then(function(response) {
|
.then(response => {
|
||||||
expect(response).toEqual('this is the first source');
|
expect(response).toEqual('this is the first source');
|
||||||
expect(bundleFunc.mock.calls.length).toBe(1);
|
expect(bundleFunc.mock.calls.length).toBe(1);
|
||||||
triggerFileChange('all','path/file.js', options.projectRoots[0]);
|
triggerFileChange('all','path/file.js', options.projectRoots[0]);
|
||||||
jest.runAllTimers();
|
jest.runAllTimers();
|
||||||
jest.runAllTimers();
|
jest.runAllTimers();
|
||||||
})
|
})
|
||||||
.then(function() {
|
.then(() => {
|
||||||
expect(bundleFunc.mock.calls.length).toBe(2);
|
expect(bundleFunc.mock.calls.length).toBe(2);
|
||||||
return makeRequest(requestHandler, 'mybundle.bundle?runModule=true')
|
return makeRequest(requestHandler, 'mybundle.bundle?runModule=true')
|
||||||
.then(function(response) {
|
.then(response =>
|
||||||
expect(response).toEqual('this is the rebuilt source');
|
expect(response).toEqual('this is the rebuilt source')
|
||||||
});
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('/onchange endpoint', function() {
|
describe('/onchange endpoint', () => {
|
||||||
var EventEmitter;
|
var EventEmitter;
|
||||||
var req;
|
var req;
|
||||||
var res;
|
var res;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(() => {
|
||||||
EventEmitter = require.requireActual('events').EventEmitter;
|
EventEmitter = require.requireActual('events').EventEmitter;
|
||||||
req = new EventEmitter();
|
req = new EventEmitter();
|
||||||
req.url = '/onchange';
|
req.url = '/onchange';
|
||||||
|
@ -214,14 +193,14 @@ describe('processRequest', function() {
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should hold on to request and inform on change', function() {
|
it('should hold on to request and inform on change', () => {
|
||||||
server.processRequest(req, res);
|
server.processRequest(req, res);
|
||||||
triggerFileChange('all', 'path/file.js', options.projectRoots[0]);
|
triggerFileChange('all', 'path/file.js', options.projectRoots[0]);
|
||||||
jest.runAllTimers();
|
jest.runAllTimers();
|
||||||
expect(res.end).toBeCalledWith(JSON.stringify({changed: true}));
|
expect(res.end).toBeCalledWith(JSON.stringify({changed: true}));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not inform changes on disconnected clients', function() {
|
it('should not inform changes on disconnected clients', () => {
|
||||||
server.processRequest(req, res);
|
server.processRequest(req, res);
|
||||||
req.emit('close');
|
req.emit('close');
|
||||||
jest.runAllTimers();
|
jest.runAllTimers();
|
||||||
|
@ -231,36 +210,30 @@ describe('processRequest', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('/assets endpoint', function() {
|
describe('/assets endpoint', () => {
|
||||||
var AssetServer;
|
var AssetServer;
|
||||||
beforeEach(function() {
|
beforeEach(() => {
|
||||||
AssetServer = require('../../AssetServer');
|
AssetServer = require('../../AssetServer');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should serve simple case', function() {
|
it('should serve simple case', () => {
|
||||||
var req = {
|
const req = {url: '/assets/imgs/a.png'};
|
||||||
url: '/assets/imgs/a.png',
|
const res = {end: jest.genMockFn()};
|
||||||
};
|
|
||||||
var res = {
|
|
||||||
end: jest.genMockFn(),
|
|
||||||
};
|
|
||||||
|
|
||||||
AssetServer.prototype.get.mockImpl(function() {
|
AssetServer.prototype.get.mockImpl(() => Promise.resolve('i am image'));
|
||||||
return Promise.resolve('i am image');
|
|
||||||
});
|
|
||||||
|
|
||||||
server.processRequest(req, res);
|
server.processRequest(req, res);
|
||||||
jest.runAllTimers();
|
jest.runAllTimers();
|
||||||
expect(res.end).toBeCalledWith('i am image');
|
expect(res.end).toBeCalledWith('i am image');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return 404', function() {
|
it('should return 404', () => {
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('buildBundle(options)', function() {
|
describe('buildbundle(options)', () => {
|
||||||
it('Calls the bundler with the correct args', function() {
|
it('Calls the bundler with the correct args', () => {
|
||||||
server.buildBundle({
|
server.buildBundle({
|
||||||
entryFile: 'foo file'
|
entryFile: 'foo file'
|
||||||
});
|
});
|
||||||
|
@ -273,8 +246,8 @@ describe('processRequest', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('buildBundleFromUrl(options)', function() {
|
describe('buildBundleFromUrl(options)', () => {
|
||||||
it('Calls the bundler with the correct args', function() {
|
it('Calls the bundler with the correct args', () => {
|
||||||
server.buildBundleFromUrl('/path/to/foo.bundle?dev=false&runModule=false');
|
server.buildBundleFromUrl('/path/to/foo.bundle?dev=false&runModule=false');
|
||||||
expect(Bundler.prototype.bundle).toBeCalledWith(
|
expect(Bundler.prototype.bundle).toBeCalledWith(
|
||||||
'path/to/foo.js',
|
'path/to/foo.js',
|
||||||
|
|
Loading…
Reference in New Issue