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
This commit is contained in:
Jean Lauliac 2017-05-17 05:14:58 -07:00 committed by Facebook Github Bot
parent 6f61f74e4e
commit b512cf62af
3 changed files with 73 additions and 2 deletions

View File

@ -36,7 +36,7 @@ type Options = {|
+platforms: Set<string>,
|};
type AssetInfo = {platform: ?string, fileName: string};
type AssetInfo = {|platform: ?string, fileName: string|};
type InfoByAssetName = Map<string, Array<AssetInfo>>;
const EMPTY_ARRAY = [];
@ -102,7 +102,7 @@ class AssetResolutionCache {
continue;
}
getWithDefaultArray(results, assetData.assetName).push({
plaform: assetData.platform,
platform: assetData.platform,
fileName,
});
}

View File

@ -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();
});
});

View File

@ -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",
]
`;