Skip to content

Commit 8f98f8a

Browse files
committed
test: add regression test against #533
1 parent 4bfb0e4 commit 8f98f8a

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

packages/render-html/src/__mocks__/react-native.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@ import { Image as RNImage } from 'react-native';
22

33
export * from 'react-native';
44

5+
function getSizeWithHeaders(uri, headers, success, failure) {
6+
setTimeout(() => {
7+
let dimensions = [0, 0];
8+
if (typeof uri === 'string') {
9+
if (uri === 'error') {
10+
failure?.apply(null, new Error('Could not fetch image dimensions'));
11+
return;
12+
}
13+
const match = /(\d+)x(\d+)/gi.exec(uri);
14+
if (!match) {
15+
dimensions = [640, 360];
16+
} else {
17+
dimensions = [parseInt(match[1], 10), parseInt(match[2], 10)];
18+
}
19+
}
20+
success.apply(null, dimensions);
21+
}, 50);
22+
}
23+
524
export class Image extends RNImage {
625
/**
726
* This mock function will parse the uri to find a dimension pattern
@@ -13,26 +32,11 @@ export class Image extends RNImage {
1332
* be extracted, it will use 640x360.
1433
*/
1534
static getSizeWithHeaders(uri, headers, success, failure) {
16-
setTimeout(() => {
17-
let dimensions = [0, 0];
18-
if (typeof uri === 'string') {
19-
if (uri === 'error') {
20-
failure?.apply(null, new Error('Could not fetch image dimensions'));
21-
return;
22-
}
23-
const match = /(\d+)x(\d+)/gi.exec(uri);
24-
if (!match) {
25-
dimensions = [640, 360];
26-
} else {
27-
dimensions = [parseInt(match[1], 10), parseInt(match[2], 10)];
28-
}
29-
}
30-
success.apply(null, dimensions);
31-
}, 50);
35+
getSizeWithHeaders(uri, headers, callback, failure);
3236
}
3337

3438
static getSize(uri, callback, failure) {
35-
Image.getSizeWithHeaders(uri, undefined, callback, failure);
39+
getSizeWithHeaders(uri, undefined, callback, failure);
3640
}
3741
}
3842

packages/render-html/src/__tests__/component.render-html.test.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,7 @@ describe('RenderHTML', () => {
836836
const headers = {
837837
Authorization: 'Bearer XXX'
838838
};
839+
const getSizeWithHeaders = jest.spyOn(Image, 'getSizeWithHeaders');
839840
function provideEmbeddedHeaders(uri: string, tag: string) {
840841
expect(tag).toBe('img');
841842
return headers;
@@ -853,6 +854,10 @@ describe('RenderHTML', () => {
853854
await findByTestId('image-success');
854855
const image = UNSAFE_getByType(Image);
855856
expect(image.props.source.headers).toBe(headers);
857+
expect(getSizeWithHeaders).toHaveBeenCalledWith(
858+
'https://custom.domain/',
859+
headers
860+
);
856861
});
857862
});
858863
describe('regarding enableExperimentalBRCollapsing', () => {

0 commit comments

Comments
 (0)