mirror of https://github.com/status-im/metro.git
Implement end() method in DeltaBundler
Reviewed By: jeanlauliac Differential Revision: D5803466 fbshipit-source-id: 78a29cd943774506694513987d97d9f37be04485
This commit is contained in:
parent
197d885ec1
commit
8e2252d35d
|
@ -0,0 +1,106 @@
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* @emails oncall+javascript_foundation
|
||||||
|
* @format
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
jest.mock('../DeltaTransformer');
|
||||||
|
jest.mock('../../Bundler');
|
||||||
|
|
||||||
|
const Bundler = require('../../Bundler');
|
||||||
|
const DeltaTransformer = require('../DeltaTransformer');
|
||||||
|
|
||||||
|
const DeltaBundler = require('../');
|
||||||
|
|
||||||
|
describe('DeltaBundler', () => {
|
||||||
|
let deltaBundler;
|
||||||
|
let bundler;
|
||||||
|
const initialTransformerResponse = {
|
||||||
|
pre: new Map([[1, {code: 'pre'}]]),
|
||||||
|
post: new Map([[2, {code: 'post'}]]),
|
||||||
|
delta: new Map([[3, {code: 'module3'}], [4, {code: 'another'}]]),
|
||||||
|
inverseDependencies: [],
|
||||||
|
reset: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
DeltaTransformer.prototype.getDelta = jest
|
||||||
|
.fn()
|
||||||
|
.mockReturnValueOnce(Promise.resolve(initialTransformerResponse));
|
||||||
|
|
||||||
|
DeltaTransformer.create = jest
|
||||||
|
.fn()
|
||||||
|
.mockReturnValue(Promise.resolve(new DeltaTransformer()));
|
||||||
|
|
||||||
|
bundler = new Bundler();
|
||||||
|
deltaBundler = new DeltaBundler(bundler, {});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should create a new transformer to build the initial bundle', async () => {
|
||||||
|
expect(await deltaBundler.build({deltaBundleId: 10})).toEqual({
|
||||||
|
...initialTransformerResponse,
|
||||||
|
id: 10,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(DeltaTransformer.create.mock.calls.length).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should reuse the same transformer after a second call', async () => {
|
||||||
|
const secondResponse = {
|
||||||
|
delta: new Map([[3, {code: 'a different module'}]]),
|
||||||
|
pre: new Map(),
|
||||||
|
post: new Map(),
|
||||||
|
inverseDependencies: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
DeltaTransformer.prototype.getDelta.mockReturnValueOnce(
|
||||||
|
Promise.resolve(secondResponse),
|
||||||
|
);
|
||||||
|
|
||||||
|
await deltaBundler.build({deltaBundleId: 10});
|
||||||
|
|
||||||
|
expect(await deltaBundler.build({deltaBundleId: 10})).toEqual({
|
||||||
|
...secondResponse,
|
||||||
|
id: 10,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(DeltaTransformer.create.mock.calls.length).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should reset everything after calling end()', async () => {
|
||||||
|
await deltaBundler.build({deltaBundleId: 10});
|
||||||
|
|
||||||
|
deltaBundler.end();
|
||||||
|
|
||||||
|
await deltaBundler.build({deltaBundleId: 10});
|
||||||
|
|
||||||
|
expect(DeltaTransformer.create.mock.calls.length).toBe(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should build the whole stringified bundle', async () => {
|
||||||
|
expect(
|
||||||
|
await deltaBundler.buildFullBundle({deltaBundleId: 10}),
|
||||||
|
).toMatchSnapshot();
|
||||||
|
|
||||||
|
DeltaTransformer.prototype.getDelta.mockReturnValueOnce(
|
||||||
|
Promise.resolve({
|
||||||
|
delta: new Map([[3, {code: 'modified module'}], [4, null]]),
|
||||||
|
pre: new Map([[5, {code: 'more pre'}]]),
|
||||||
|
post: new Map([[6, {code: 'bananas'}], [7, {code: 'apples'}]]),
|
||||||
|
inverseDependencies: [],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
await deltaBundler.buildFullBundle({deltaBundleId: 10}),
|
||||||
|
).toMatchSnapshot();
|
||||||
|
});
|
||||||
|
});
|
|
@ -0,0 +1,23 @@
|
||||||
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||||
|
|
||||||
|
exports[`DeltaBundler should build the whole stringified bundle 1`] = `
|
||||||
|
Object {
|
||||||
|
"bundle": "pre
|
||||||
|
;module3
|
||||||
|
;another
|
||||||
|
;post",
|
||||||
|
"numModifiedFiles": 4,
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`DeltaBundler should build the whole stringified bundle 2`] = `
|
||||||
|
Object {
|
||||||
|
"bundle": "pre
|
||||||
|
;more pre
|
||||||
|
;modified module
|
||||||
|
;post
|
||||||
|
;bananas
|
||||||
|
;apples",
|
||||||
|
"numModifiedFiles": 5,
|
||||||
|
}
|
||||||
|
`;
|
|
@ -60,6 +60,12 @@ class DeltaBundler {
|
||||||
this._options = options;
|
this._options = options;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
end() {
|
||||||
|
this._deltaTransformers.forEach(DeltaTransformer => DeltaTransformer.end());
|
||||||
|
this._deltaTransformers = new Map();
|
||||||
|
this._deltaPatchers = new Map();
|
||||||
|
}
|
||||||
|
|
||||||
async build(options: Options): Promise<DeltaBundle> {
|
async build(options: Options): Promise<DeltaBundle> {
|
||||||
const {deltaTransformer, id} = await this.getDeltaTransformer({
|
const {deltaTransformer, id} = await this.getDeltaTransformer({
|
||||||
...options,
|
...options,
|
||||||
|
|
|
@ -297,8 +297,9 @@ class Server {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
end(): mixed {
|
end() {
|
||||||
return this._bundler.end();
|
this._deltaBundler.end();
|
||||||
|
this._bundler.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
setHMRFileChangeListener(
|
setHMRFileChangeListener(
|
||||||
|
|
Loading…
Reference in New Issue