Files
react-native/Libraries/Image/__tests__/resolveAssetSource-test.js
T
James Ide 1ed352517a Explicitly separate mocked native modules from mocked JS modules (#24809)
Summary:
This commit more clearly defines the mocks RN sets up and uses paths instead of Haste names to define the mocks. The Jest setup script defined mocks for native modules (Obj-C, Java) and mocks for JS modules in the same data structure. This meant that some non-native modules (that is, JS modules) were in the `mockNativeModules` map -- this commit splits them out and mocks them in typical `jest.mock` fashion.

Additionally, the setup script used to mock the modules using the Haste names. As one of the steps toward migrating to standard path-based imports, the setup script now mocks JS modules using paths (native modules don't need a Haste name nor path since they are just entries in `NativeModules`). This gets us closer to being able to remove `hasteImpl`. (Tracking in https://github.com/facebook/react-native/issues/24772.)

Also, this commit removes mocks that are not referenced anywhere in the RN and React repositories (grepped for the names and found no entries outside of the Jest setup scripts).

## Changelog

[General] [Changed] - Explicitly separate mocked native modules from mocked JS modules
Pull Request resolved: https://github.com/facebook/react-native/pull/24809

Differential Revision: D15316882

Pulled By: cpojer

fbshipit-source-id: 039e4e320121bea9580196fe0a091b8b1e8b41bf
2019-05-31 03:19:49 -07:00

325 lines
8.5 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @emails oncall+react_native
*/
'use strict';
describe('resolveAssetSource', () => {
let AssetRegistry;
let resolveAssetSource;
let NativeSourceCode;
let Platform;
beforeEach(() => {
jest.resetModules();
AssetRegistry = require('../AssetRegistry');
resolveAssetSource = require('../resolveAssetSource');
NativeSourceCode = require('../../NativeModules/specs/NativeSourceCode')
.default;
Platform = require('../../Utilities/Platform');
});
it('returns same source for simple static and network images', () => {
const source1 = {uri: 'https://www.facebook.com/logo'};
expect(resolveAssetSource(source1)).toBe(source1);
const source2 = {uri: 'logo'};
expect(resolveAssetSource(source2)).toBe(source2);
});
it('does not change deprecated assets', () => {
expect(
resolveAssetSource({
deprecated: true,
width: 100,
height: 200,
uri: 'logo',
}),
).toEqual({
deprecated: true,
width: 100,
height: 200,
uri: 'logo',
});
});
it('ignores any weird data', () => {
expect(resolveAssetSource(null)).toBe(null);
expect(resolveAssetSource(42)).toBe(null);
expect(resolveAssetSource('nonsense')).toBe(null);
});
describe('bundle was loaded from network (DEV)', () => {
beforeEach(() => {
NativeSourceCode.getConstants = () => ({
scriptURL: 'http://10.0.0.1:8081/main.bundle',
});
Platform.OS = 'ios';
});
it('uses network image', () => {
expectResolvesAsset(
{
__packager_asset: true,
fileSystemLocation: '/root/app/module/a',
httpServerLocation: '/assets/module/a',
width: 100,
height: 200,
scales: [1],
hash: '5b6f00f',
name: 'logo',
type: 'png',
},
{
__packager_asset: true,
width: 100,
height: 200,
uri:
'http://10.0.0.1:8081/assets/module/a/logo.png?platform=ios&hash=5b6f00f',
scale: 1,
},
);
});
it('picks matching scale', () => {
expectResolvesAsset(
{
__packager_asset: true,
fileSystemLocation: '/root/app/module/a',
httpServerLocation: '/assets/module/a',
width: 100,
height: 200,
scales: [1, 2, 3],
hash: '5b6f00f',
name: 'logo',
type: 'png',
},
{
__packager_asset: true,
width: 100,
height: 200,
uri:
'http://10.0.0.1:8081/assets/module/a/logo@2x.png?platform=ios&hash=5b6f00f',
scale: 2,
},
);
});
});
describe('bundle was loaded from file on iOS', () => {
beforeEach(() => {
NativeSourceCode.getConstants = () => ({
scriptURL: 'file:///Path/To/Sample.app/main.bundle',
});
Platform.OS = 'ios';
});
it('uses pre-packed image', () => {
expectResolvesAsset(
{
__packager_asset: true,
fileSystemLocation: '/root/app/module/a',
httpServerLocation: '/assets/module/a',
width: 100,
height: 200,
scales: [1],
hash: '5b6f00f',
name: 'logo',
type: 'png',
},
{
__packager_asset: true,
width: 100,
height: 200,
uri: 'file:///Path/To/Sample.app/assets/module/a/logo.png',
scale: 1,
},
);
});
});
describe('bundle was loaded from assets on Android', () => {
beforeEach(() => {
NativeSourceCode.getConstants = () => ({
scriptURL: 'assets://Path/To/Simulator/main.bundle',
});
Platform.OS = 'android';
});
it('uses pre-packed image', () => {
expectResolvesAsset(
{
__packager_asset: true,
fileSystemLocation: '/root/app/module/a',
httpServerLocation: '/assets/AwesomeModule/Subdir',
width: 100,
height: 200,
scales: [1],
hash: '5b6f00f',
name: '!@Logo#1_\u20ac', // Invalid chars shouldn't get passed to native
type: 'png',
},
{
__packager_asset: true,
width: 100,
height: 200,
uri: 'awesomemodule_subdir_logo1_',
scale: 1,
},
);
});
});
describe('bundle was loaded from file on Android', () => {
beforeEach(() => {
NativeSourceCode.getConstants = () => ({
scriptURL: 'file:///sdcard/Path/To/Simulator/main.bundle',
});
Platform.OS = 'android';
});
it('uses pre-packed image', () => {
expectResolvesAsset(
{
__packager_asset: true,
fileSystemLocation: '/root/app/module/a',
httpServerLocation: '/assets/AwesomeModule/Subdir',
width: 100,
height: 200,
scales: [1],
hash: '5b6f00f',
name: '!@Logo#1_\u20ac',
type: 'png',
},
{
__packager_asset: true,
width: 100,
height: 200,
uri:
'file:///sdcard/Path/To/Simulator/drawable-mdpi/awesomemodule_subdir_logo1_.png',
scale: 1,
},
);
});
});
describe('bundle was loaded from raw file on Android', () => {
beforeEach(() => {
NativeSourceCode.getConstants = () => ({
scriptURL: '/sdcard/Path/To/Simulator/main.bundle',
});
Platform.OS = 'android';
});
it('uses sideloaded image', () => {
expectResolvesAsset(
{
__packager_asset: true,
fileSystemLocation: '/root/app/module/a',
httpServerLocation: '/assets/AwesomeModule/Subdir',
width: 100,
height: 200,
scales: [1],
hash: '5b6f00f',
name: '!@Logo#1_\u20ac',
type: 'png',
},
{
__packager_asset: true,
width: 100,
height: 200,
uri:
'file:///sdcard/Path/To/Simulator/drawable-mdpi/awesomemodule_subdir_logo1_.png',
scale: 1,
},
);
});
});
describe('source resolver can be customized', () => {
beforeEach(() => {
NativeSourceCode.getConstants = () => ({
scriptURL: 'file:///sdcard/Path/To/Simulator/main.bundle',
});
Platform.OS = 'android';
});
it('uses bundled source, event when js is sideloaded', () => {
resolveAssetSource.setCustomSourceTransformer(resolver =>
resolver.resourceIdentifierWithoutScale(),
);
expectResolvesAsset(
{
__packager_asset: true,
fileSystemLocation: '/root/app/module/a',
httpServerLocation: '/assets/AwesomeModule/Subdir',
width: 100,
height: 200,
scales: [1],
hash: '5b6f00f',
name: '!@Logo#1_\u20ac',
type: 'png',
},
{
__packager_asset: true,
width: 100,
height: 200,
uri: 'awesomemodule_subdir_logo1_',
scale: 1,
},
);
});
it('allows any customization', () => {
resolveAssetSource.setCustomSourceTransformer(resolver =>
resolver.fromSource('TEST'),
);
expectResolvesAsset(
{
__packager_asset: true,
fileSystemLocation: '/root/app/module/a',
httpServerLocation: '/assets/AwesomeModule/Subdir',
width: 100,
height: 200,
scales: [1],
hash: '5b6f00f',
name: '!@Logo#1_\u20ac',
type: 'png',
},
{
__packager_asset: true,
width: 100,
height: 200,
uri: 'TEST',
scale: 1,
},
);
});
});
function expectResolvesAsset(input, expectedSource) {
const assetId = AssetRegistry.registerAsset(input);
expect(resolveAssetSource(assetId)).toEqual(expectedSource);
}
});
describe('resolveAssetSource.pickScale', () => {
const resolveAssetSource = require('../resolveAssetSource');
it('picks matching scale', () => {
expect(resolveAssetSource.pickScale([1], 2)).toBe(1);
expect(resolveAssetSource.pickScale([1, 2, 3], 2)).toBe(2);
expect(resolveAssetSource.pickScale([1, 2], 3)).toBe(2);
expect(resolveAssetSource.pickScale([1, 2, 3, 4], 3.5)).toBe(4);
expect(resolveAssetSource.pickScale([3, 4], 2)).toBe(3);
expect(resolveAssetSource.pickScale([], 2)).toBe(1);
});
});