From 44955dfa178f9a82bbab7ddc4e3adfde3d0d0083 Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Wed, 17 May 2017 05:14:58 -0700 Subject: [PATCH] packager: AssetResolutionCache: tests + fix bug Summary: Or, a case in point that Flow doesn't saves us from all the ills. I think it didn't complain because `MapWithDefaults` doesn't have proper typing. I'll deal with that separately. Reviewed By: davidaurelio Differential Revision: D5077707 fbshipit-source-id: c43623c5046d2dea9964685a44ad4877d060232e --- .../src/node-haste/AssetResolutionCache.js | 4 +- .../__tests__/AssetResolutionCache-test.js | 53 +++++++++++++++++++ .../AssetResolutionCache-test.js.snap | 18 +++++++ 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 packager/src/node-haste/__tests__/AssetResolutionCache-test.js create mode 100644 packager/src/node-haste/__tests__/__snapshots__/AssetResolutionCache-test.js.snap diff --git a/packager/src/node-haste/AssetResolutionCache.js b/packager/src/node-haste/AssetResolutionCache.js index e96a7c15b..afad46c6c 100644 --- a/packager/src/node-haste/AssetResolutionCache.js +++ b/packager/src/node-haste/AssetResolutionCache.js @@ -36,7 +36,7 @@ type Options = {| +platforms: Set, |}; -type AssetInfo = {platform: ?string, fileName: string}; +type AssetInfo = {|platform: ?string, fileName: string|}; type InfoByAssetName = Map>; const EMPTY_ARRAY = []; @@ -102,7 +102,7 @@ class AssetResolutionCache { continue; } getWithDefaultArray(results, assetData.assetName).push({ - plaform: assetData.platform, + platform: assetData.platform, fileName, }); } diff --git a/packager/src/node-haste/__tests__/AssetResolutionCache-test.js b/packager/src/node-haste/__tests__/AssetResolutionCache-test.js new file mode 100644 index 000000000..a5396848e --- /dev/null +++ b/packager/src/node-haste/__tests__/AssetResolutionCache-test.js @@ -0,0 +1,53 @@ +/** + * 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. + * + * @format + */ + +'use strict'; + +jest.disableAutomock(); + +const AssetResolutionCache = require('../AssetResolutionCache'); + +const MOCK_FILE_NAMES = [ + 'test@2x.ios.png', + 'test@1x.ios.png', + 'foo.jpg', + 'bar.ios.png', + 'test@1.5x.ios.png', + 'foo@2x.jpg', + 'test@3x.android.png', + 'test.android.png', +]; + +describe('AssetResolutionCache', () => { + let fileNames, cache; + + beforeEach(() => { + fileNames = [...MOCK_FILE_NAMES]; + cache = new AssetResolutionCache({ + assetExtensions: new Set(['png', 'jpg']), + getDirFiles: dirPath => (dirPath === '/assets' ? fileNames : []), + platforms: new Set(['ios', 'android']), + }); + }); + + it('finds the correct assets', () => { + const results = cache.resolve('/assets', 'test.png', 'ios'); + expect(results).toMatchSnapshot(); + }); + + it('correctly clears out', () => { + cache.resolve('/assets', 'test.png', 'ios'); + fileNames.push('test@3x.ios.png'); + cache.clear(); + const results = cache.resolve('/assets', 'test.png', 'ios'); + expect(results).toMatchSnapshot(); + }); +}); diff --git a/packager/src/node-haste/__tests__/__snapshots__/AssetResolutionCache-test.js.snap b/packager/src/node-haste/__tests__/__snapshots__/AssetResolutionCache-test.js.snap new file mode 100644 index 000000000..1f6708713 --- /dev/null +++ b/packager/src/node-haste/__tests__/__snapshots__/AssetResolutionCache-test.js.snap @@ -0,0 +1,18 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`AssetResolutionCache correctly clears out 1`] = ` +Array [ + "test@2x.ios.png", + "test@1x.ios.png", + "test@1.5x.ios.png", + "test@3x.ios.png", +] +`; + +exports[`AssetResolutionCache finds the correct assets 1`] = ` +Array [ + "test@2x.ios.png", + "test@1x.ios.png", + "test@1.5x.ios.png", +] +`;