From 2a9b50c5296c4d0e9d7955af6ae6d00924fcecc7 Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Tue, 21 Mar 2017 05:54:03 -0700 Subject: [PATCH] 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 --- .../__tests__/GlobalTransformCache-test.js | 91 +++++++++++++++++++ .../GlobalTransformCache-test.js.snap | 32 +++++++ 2 files changed, 123 insertions(+) create mode 100644 packages/metro-bundler/src/lib/__tests__/GlobalTransformCache-test.js create mode 100644 packages/metro-bundler/src/lib/__tests__/__snapshots__/GlobalTransformCache-test.js.snap diff --git a/packages/metro-bundler/src/lib/__tests__/GlobalTransformCache-test.js b/packages/metro-bundler/src/lib/__tests__/GlobalTransformCache-test.js new file mode 100644 index 00000000..1900ed85 --- /dev/null +++ b/packages/metro-bundler/src/lib/__tests__/GlobalTransformCache-test.js @@ -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): Promise> { + return new Map(keys.map(key => [key, `http://globalcache.com/${key}`])); +} + +async function fetchResultFromURI(uri: string): Promise { + 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(); + }); + + }); + +}); diff --git a/packages/metro-bundler/src/lib/__tests__/__snapshots__/GlobalTransformCache-test.js.snap b/packages/metro-bundler/src/lib/__tests__/__snapshots__/GlobalTransformCache-test.js.snap new file mode 100644 index 00000000..c0fdf1ad --- /dev/null +++ b/packages/metro-bundler/src/lib/__tests__/__snapshots__/GlobalTransformCache-test.js.snap @@ -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 [], + }, +] +`;