Skip to content

Commit

Permalink
test: add regression test against #533
Browse files Browse the repository at this point in the history
  • Loading branch information
jsamr committed Nov 3, 2021
1 parent 4bfb0e4 commit 8f98f8a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
38 changes: 21 additions & 17 deletions packages/render-html/src/__mocks__/react-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@ import { Image as RNImage } from 'react-native';

export * from 'react-native';

function getSizeWithHeaders(uri, headers, success, failure) {
setTimeout(() => {
let dimensions = [0, 0];
if (typeof uri === 'string') {
if (uri === 'error') {
failure?.apply(null, new Error('Could not fetch image dimensions'));
return;
}
const match = /(\d+)x(\d+)/gi.exec(uri);
if (!match) {
dimensions = [640, 360];
} else {
dimensions = [parseInt(match[1], 10), parseInt(match[2], 10)];
}
}
success.apply(null, dimensions);
}, 50);
}

export class Image extends RNImage {
/**
* This mock function will parse the uri to find a dimension pattern
Expand All @@ -13,26 +32,11 @@ export class Image extends RNImage {
* be extracted, it will use 640x360.
*/
static getSizeWithHeaders(uri, headers, success, failure) {
setTimeout(() => {
let dimensions = [0, 0];
if (typeof uri === 'string') {
if (uri === 'error') {
failure?.apply(null, new Error('Could not fetch image dimensions'));
return;
}
const match = /(\d+)x(\d+)/gi.exec(uri);
if (!match) {
dimensions = [640, 360];
} else {
dimensions = [parseInt(match[1], 10), parseInt(match[2], 10)];
}
}
success.apply(null, dimensions);
}, 50);
getSizeWithHeaders(uri, headers, callback, failure);
}

static getSize(uri, callback, failure) {
Image.getSizeWithHeaders(uri, undefined, callback, failure);
getSizeWithHeaders(uri, undefined, callback, failure);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,7 @@ describe('RenderHTML', () => {
const headers = {
Authorization: 'Bearer XXX'
};
const getSizeWithHeaders = jest.spyOn(Image, 'getSizeWithHeaders');
function provideEmbeddedHeaders(uri: string, tag: string) {
expect(tag).toBe('img');
return headers;
Expand All @@ -853,6 +854,10 @@ describe('RenderHTML', () => {
await findByTestId('image-success');
const image = UNSAFE_getByType(Image);
expect(image.props.source.headers).toBe(headers);
expect(getSizeWithHeaders).toHaveBeenCalledWith(
'https://custom.domain/',
headers
);
});
});
describe('regarding enableExperimentalBRCollapsing', () => {
Expand Down

0 comments on commit 8f98f8a

Please sign in to comment.