Skip to content

Commit 04900d9

Browse files
committed
[change] Rework Image loading and source management logic
Since introducing the change to support headers changes to the original changes are needed: - support loading a default source with headers - handle source object changes - update uri resolving logic to handle blob URLs create by `URL.createObjectURL` - move the URI/source resolving logic to the `ImageLoader`
1 parent 4988c7b commit 04900d9

File tree

4 files changed

+196
-128
lines changed

4 files changed

+196
-128
lines changed

packages/react-native-web/src/exports/Image/__tests__/__snapshots__/index-test.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,14 +329,14 @@ exports[`components/Image prop "style" removes other unsupported View styles 1`]
329329
>
330330
<div
331331
class="css-view-175oi2r r-backgroundColor-1niwhzg r-backgroundPosition-vvn4in r-backgroundRepeat-u6sd8q r-bottom-1p0dtai r-height-1pi2tsx r-left-1d2f490 r-position-u8s1d r-right-zchlnj r-top-ipm5af r-width-13qz1uu r-zIndex-1wyyakw r-backgroundSize-4gszlv"
332-
style="filter: url(#tint-72);"
332+
style="filter: url(#tint-86);"
333333
/>
334334
<svg
335335
style="position: absolute; height: 0px; visibility: hidden; width: 0px;"
336336
>
337337
<defs>
338338
<filter
339-
id="tint-72"
339+
id="tint-86"
340340
>
341341
<feflood
342342
flood-color="blue"
@@ -378,7 +378,7 @@ exports[`components/Image prop "style" supports "tintcolor" property (convert to
378378
>
379379
<div
380380
class="css-view-175oi2r r-backgroundColor-1niwhzg r-backgroundPosition-vvn4in r-backgroundRepeat-u6sd8q r-bottom-1p0dtai r-height-1pi2tsx r-left-1d2f490 r-position-u8s1d r-right-zchlnj r-top-ipm5af r-width-13qz1uu r-zIndex-1wyyakw r-backgroundSize-4gszlv"
381-
style="background-image: url(https://google.com/favicon.ico); filter: url(#tint-71);"
381+
style="background-image: url(https://google.com/favicon.ico); filter: url(#tint-84);"
382382
/>
383383
<img
384384
alt=""
@@ -391,7 +391,7 @@ exports[`components/Image prop "style" supports "tintcolor" property (convert to
391391
>
392392
<defs>
393393
<filter
394-
id="tint-71"
394+
id="tint-84"
395395
>
396396
<feflood
397397
flood-color="red"

packages/react-native-web/src/exports/Image/__tests__/index-test.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ describe('components/Image', () => {
2121
beforeEach(() => {
2222
ImageUriCache._entries = {};
2323
window.Image = jest.fn(() => ({}));
24-
ImageLoader.load = jest.fn().mockImplementation((src, onLoad, onError) => {
25-
onLoad(src);
26-
});
24+
ImageLoader.load = jest
25+
.fn()
26+
.mockImplementation((source, onLoad, onError) => {
27+
onLoad({ source });
28+
});
2729
});
2830

2931
afterEach(() => {
@@ -309,6 +311,7 @@ describe('components/Image', () => {
309311
null,
310312
'',
311313
{},
314+
[],
312315
{ uri: '' },
313316
{ uri: 'https://google.com' },
314317
{ uri: 'https://google.com', headers: { 'x-custom-header': 'abc123' } }
@@ -369,19 +372,32 @@ describe('components/Image', () => {
369372
test('is correctly updated only when loaded if defaultSource provided', () => {
370373
const defaultUri = 'https://testing.com/preview.jpg';
371374
const uri = 'https://testing.com/fullSize.jpg';
372-
let loadCallback;
373-
ImageLoader.load = jest
374-
.fn()
375-
.mockImplementationOnce((_, onLoad, onError) => {
376-
loadCallback = onLoad;
377-
});
375+
const calls = [];
376+
377+
// Capture calls and resolve them after render
378+
ImageLoader.load = jest.fn().mockImplementation((source, onLoad) => {
379+
calls.push({ source, onLoad });
380+
});
381+
378382
const { container } = render(
379383
<Image defaultSource={{ uri: defaultUri }} source={{ uri }} />
380384
);
385+
386+
// Both defaultSource and source are loaded at the same time
387+
// But we assume defaultSource is loaded quicker
388+
act(() => {
389+
const call = calls.find(({ source }) => source.uri === defaultUri);
390+
call.onLoad({ source: call.source });
391+
});
392+
381393
expect(container.firstChild).toMatchSnapshot();
394+
395+
// After a while the main source loads as well
382396
act(() => {
383-
loadCallback({ uri });
397+
const call = calls.find(({ source }) => source.uri === uri);
398+
call.onLoad({ source: call.source });
384399
});
400+
385401
expect(container.firstChild).toMatchSnapshot();
386402
});
387403

0 commit comments

Comments
 (0)