mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
47a05bc26a
Summary: This PR is for adding the support for `crossOrigin`, `referrerPolicy`, `width`, `height` and `srcSet` props to Image Component and mapping the `src` prop to `source.uri` of Image Component for the issue https://github.com/facebook/react-native/issues/34424. An example is also added in the RNTester ImageExample. ## Changelog [General] [Changed] - Map the `src` prop to `source.uri` prop in Image Component. [General] [Added] - added `crossOrigin`, `referrerPolicy`, `width`, `height` and `srcSet` props to Image Component. Pull Request resolved: https://github.com/facebook/react-native/pull/34481 Test Plan: 1. Navigate to Image Component Example in the RNTester app. 2. Contains an example of the Image component using the `src` and `srcSet` prop. 3. For headers, inspect the Image request using Flipper. <img src="https://user-images.githubusercontent.com/32268377/186153246-d7b72ce3-e082-46d9-87d1-aefacd3af34f.png" height="500" /> Reviewed By: christophpurrer Differential Revision: D38982041 Pulled By: cipolleschi fbshipit-source-id: dd6594e39b8f3b36cfcdafa35695254034f1fb7f
146 lines
4.1 KiB
JavaScript
146 lines
4.1 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and 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
|
|
*/
|
|
|
|
const {getImageSourcesFromImageProps} = require('../ImageSourceUtils');
|
|
|
|
describe('ImageSourceUtils', () => {
|
|
afterEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it('source prop provided', () => {
|
|
const imageProps = {source: require('./img/img1.png')};
|
|
const sources = getImageSourcesFromImageProps(imageProps);
|
|
|
|
expect(sources).toBeDefined();
|
|
});
|
|
|
|
it('should ignore source when src is provided', () => {
|
|
let uri = 'imageURI';
|
|
const imageProps = {source: require('./img/img1.png'), src: uri};
|
|
const sources = getImageSourcesFromImageProps(imageProps);
|
|
|
|
expect(sources).toBeDefined();
|
|
expect(sources).toHaveLength(1);
|
|
expect(sources[0].uri).toBe(uri);
|
|
});
|
|
|
|
it('should ignore source and src when srcSet is provided', () => {
|
|
let uri = 'imageURI';
|
|
|
|
let uri1 = 'uri1';
|
|
let scale1 = '1x';
|
|
|
|
let uri2 = 'uri2';
|
|
let scale2 = '2x';
|
|
|
|
const imageProps = {
|
|
source: require('./img/img1.png'),
|
|
src: uri,
|
|
srcSet: `${uri1} ${scale1}, ${uri2} ${scale2}`,
|
|
};
|
|
const sources = getImageSourcesFromImageProps(imageProps);
|
|
|
|
expect(sources).toBeDefined();
|
|
expect(sources).toHaveLength(2);
|
|
expect(sources[0]).toEqual(expect.objectContaining({uri: uri1, scale: 1}));
|
|
expect(sources[1]).toEqual(expect.objectContaining({uri: uri2, scale: 2}));
|
|
});
|
|
|
|
it('should use src as default when 1x scale is not provided in srcSet', () => {
|
|
let uri = 'imageURI';
|
|
|
|
let uri1 = 'uri1';
|
|
let scale1 = '3x';
|
|
|
|
let uri2 = 'uri2';
|
|
let scale2 = '2x';
|
|
|
|
const imageProps = {
|
|
src: uri,
|
|
srcSet: `${uri1} ${scale1}, ${uri2} ${scale2}`,
|
|
};
|
|
const sources = getImageSourcesFromImageProps(imageProps);
|
|
|
|
expect(sources).toBeDefined();
|
|
expect(sources).toHaveLength(3);
|
|
expect(sources[0]).toEqual(expect.objectContaining({uri: uri1, scale: 3}));
|
|
expect(sources[1]).toEqual(expect.objectContaining({uri: uri2, scale: 2}));
|
|
expect(sources[2]).toEqual(expect.objectContaining({uri: uri, scale: 1}));
|
|
});
|
|
|
|
it('should use 1x as default scale if only url is provided in srcSet', () => {
|
|
let uri1 = 'uri1';
|
|
let scale1 = '2x';
|
|
|
|
let uri2 = 'uri2';
|
|
|
|
const imageProps = {
|
|
srcSet: `${uri1} ${scale1}, ${uri2}`,
|
|
};
|
|
const sources = getImageSourcesFromImageProps(imageProps);
|
|
|
|
expect(sources).toBeDefined();
|
|
expect(sources).toHaveLength(2);
|
|
expect(sources[0]).toEqual(expect.objectContaining({uri: uri1, scale: 2}));
|
|
expect(sources[1]).toEqual(expect.objectContaining({uri: uri2, scale: 1}));
|
|
});
|
|
|
|
it('should warn when an unsupported scale is provided in srcSet', () => {
|
|
const mockWarn = jest.spyOn(console, 'warn');
|
|
let uri1 = 'uri1';
|
|
let scale1 = '300w';
|
|
|
|
let uri2 = 'uri2';
|
|
|
|
const imageProps = {
|
|
srcSet: `${uri1} ${scale1}, ${uri2}`,
|
|
};
|
|
const sources = getImageSourcesFromImageProps(imageProps);
|
|
|
|
expect(sources).toBeDefined();
|
|
expect(sources).toHaveLength(1);
|
|
expect(mockWarn).toHaveBeenCalled();
|
|
});
|
|
|
|
it('should contain crossorigin headers when provided with src', () => {
|
|
let uri = 'imageURI';
|
|
|
|
const imageProps = {
|
|
src: uri,
|
|
crossOrigin: 'use-credentials',
|
|
};
|
|
const sources = getImageSourcesFromImageProps(imageProps);
|
|
|
|
expect(sources).toBeDefined();
|
|
expect(sources).toHaveLength(1);
|
|
expect(sources[0]).toHaveProperty('headers', {
|
|
['Access-Control-Allow-Credentials']: 'true',
|
|
});
|
|
});
|
|
|
|
it('should contain referrerPolicy headers when provided with src', () => {
|
|
let uri = 'imageURI';
|
|
|
|
let referrerPolicy = 'origin-when-cross-origin';
|
|
const imageProps = {
|
|
src: uri,
|
|
referrerPolicy: referrerPolicy,
|
|
};
|
|
const sources = getImageSourcesFromImageProps(imageProps);
|
|
|
|
expect(sources).toBeDefined();
|
|
expect(sources).toHaveLength(1);
|
|
expect(sources[0]).toHaveProperty('headers', {
|
|
['Referrer-Policy']: referrerPolicy,
|
|
});
|
|
});
|
|
});
|