mirror of https://github.com/status-im/metro.git
packager: GlobalTransformCache: add unit test
Summary: Finally adding some unit test to increase confidence in the correctness of that piece of code. Reviewed By: davidaurelio Differential Revision: D4721543 fbshipit-source-id: 56776290d61f2b51c69d7eeae09663e3bc892b50
This commit is contained in:
parent
2bec70bf7d
commit
2a9b50c529
|
@ -0,0 +1,91 @@
|
|||
/**
|
||||
* Copyright (c) 2016-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.disableAutomock();
|
||||
jest.useRealTimers();
|
||||
|
||||
const fetchMock = jest.fn();
|
||||
jest.mock('node-fetch', () => fetchMock);
|
||||
|
||||
const GlobalTransformCache = require('../GlobalTransformCache');
|
||||
const FetchError = require('node-fetch/lib/fetch-error');
|
||||
|
||||
async function fetchResultURIs(keys: Array<string>): Promise<Map<string, string>> {
|
||||
return new Map(keys.map(key => [key, `http://globalcache.com/${key}`]));
|
||||
}
|
||||
|
||||
async function fetchResultFromURI(uri: string): Promise<?CachedResult> {
|
||||
return {
|
||||
code: `/* code from ${uri} */`,
|
||||
dependencies: [],
|
||||
dependencyOffsets: [],
|
||||
};
|
||||
}
|
||||
|
||||
describe('GlobalTransformCache', () => {
|
||||
|
||||
it('fetches results', async () => {
|
||||
const cache = new GlobalTransformCache(fetchResultURIs, fetchResultFromURI, null, [
|
||||
{dev: true, minify: false, platform: 'ios'},
|
||||
]);
|
||||
const transformOptions = {
|
||||
dev: true,
|
||||
minify: false,
|
||||
platform: 'ios',
|
||||
transform: {projectRoots: [__dirname]},
|
||||
};
|
||||
const result = await Promise.all([cache.fetch({
|
||||
filePath: 'foo.js',
|
||||
sourceCode: '/* beep */',
|
||||
getTransformCacheKey: () => 'abcd',
|
||||
transformOptions,
|
||||
}), cache.fetch({
|
||||
filePath: 'bar.js',
|
||||
sourceCode: '/* boop */',
|
||||
getTransformCacheKey: () => 'abcd',
|
||||
transformOptions,
|
||||
})]);
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
describe('fetchResultFromURI', () => {
|
||||
|
||||
const defaultFetchMockImpl = async uri => ({
|
||||
status: 200,
|
||||
json: async () => ({
|
||||
code: `/* code from ${uri} */`,
|
||||
dependencies: [],
|
||||
dependencyOffsets: [],
|
||||
}),
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
fetchMock.mockReset();
|
||||
});
|
||||
|
||||
it('fetches result', async () => {
|
||||
fetchMock.mockImplementation(defaultFetchMockImpl);
|
||||
const result = await GlobalTransformCache.fetchResultFromURI('http://globalcache.com/foo');
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('retries once on timeout', async () => {
|
||||
fetchMock.mockImplementation(async uri => {
|
||||
fetchMock.mockImplementation(defaultFetchMockImpl);
|
||||
throw new FetchError('timeout!', 'request-timeout');
|
||||
});
|
||||
const result = await GlobalTransformCache.fetchResultFromURI('http://globalcache.com/foo');
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
|
@ -0,0 +1,32 @@
|
|||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`GlobalTransformCache fetchResultFromURI fetches result 1`] = `
|
||||
Object {
|
||||
"code": "/* code from http://globalcache.com/foo */",
|
||||
"dependencies": Array [],
|
||||
"dependencyOffsets": Array [],
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`GlobalTransformCache fetchResultFromURI retries once on timeout 1`] = `
|
||||
Object {
|
||||
"code": "/* code from http://globalcache.com/foo */",
|
||||
"dependencies": Array [],
|
||||
"dependencyOffsets": Array [],
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`GlobalTransformCache fetches results 1`] = `
|
||||
Array [
|
||||
Object {
|
||||
"code": "/* code from http://globalcache.com/2ad175cb80ae79fd33b914bfb392fb6742982d2a-foo.js */",
|
||||
"dependencies": Array [],
|
||||
"dependencyOffsets": Array [],
|
||||
},
|
||||
Object {
|
||||
"code": "/* code from http://globalcache.com/d6c0a1a4199d572ab68b36c07d0d68607eebb131-bar.js */",
|
||||
"dependencies": Array [],
|
||||
"dependencyOffsets": Array [],
|
||||
},
|
||||
]
|
||||
`;
|
Loading…
Reference in New Issue