Skip to content

Commit 6e26902

Browse files
committed
Pass image unit tests
1 parent 8753112 commit 6e26902

File tree

3 files changed

+15
-22
lines changed

3 files changed

+15
-22
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-57);"
332+
style="filter: url(#tint-58);"
333333
/>
334334
<svg
335335
style="position: absolute; height: 0px; visibility: hidden; width: 0px;"
336336
>
337337
<defs>
338338
<filter
339-
id="tint-57"
339+
id="tint-58"
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-56);"
381+
style="background-image: url(https://google.com/favicon.ico); filter: url(#tint-57);"
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-56"
394+
id="tint-57"
395395
>
396396
<feflood
397397
flood-color="red"

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ 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+
});
2427
});
2528

2629
afterEach(() => {
@@ -107,9 +110,6 @@ describe('components/Image', () => {
107110
describe('prop "onLoad"', () => {
108111
test('is called after image is loaded from network', () => {
109112
jest.useFakeTimers();
110-
ImageLoader.load = jest.fn().mockImplementation((_, onLoad, onError) => {
111-
onLoad();
112-
});
113113
const onLoadStartStub = jest.fn();
114114
const onLoadStub = jest.fn();
115115
const onLoadEndStub = jest.fn();
@@ -127,9 +127,6 @@ describe('components/Image', () => {
127127

128128
test('is called after image is loaded from cache', () => {
129129
jest.useFakeTimers();
130-
ImageLoader.load = jest.fn().mockImplementation((_, onLoad, onError) => {
131-
onLoad();
132-
});
133130
const onLoadStartStub = jest.fn();
134131
const onLoadStub = jest.fn();
135132
const onLoadEndStub = jest.fn();
@@ -261,11 +258,6 @@ describe('components/Image', () => {
261258

262259
test('is set immediately if the image was preloaded', () => {
263260
const uri = 'https://yahoo.com/favicon.ico';
264-
ImageLoader.load = jest
265-
.fn()
266-
.mockImplementationOnce((_, onLoad, onError) => {
267-
onLoad();
268-
});
269261
return Image.prefetch(uri).then(() => {
270262
const source = { uri };
271263
const { container } = render(<Image source={source} />, {
@@ -319,7 +311,7 @@ describe('components/Image', () => {
319311
);
320312
expect(container.firstChild).toMatchSnapshot();
321313
act(() => {
322-
loadCallback();
314+
loadCallback({ uri });
323315
});
324316
expect(container.firstChild).toMatchSnapshot();
325317
});

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,16 +255,17 @@ const Image: React.AbstractComponent<
255255
}
256256

257257
// Image loading
258+
const uri = resolveAssetUri(source);
259+
let headers;
260+
if (source && typeof source.headers === 'object') {
261+
headers = ((source.headers: any): { [key: string]: string });
262+
}
263+
258264
React.useEffect(() => {
259-
const uri = resolveAssetUri(source);
260265
if (uri != null) {
261266
updateState(LOADING);
262267
if (onLoadStart) onLoadStart();
263268

264-
let headers;
265-
if (source && typeof source.headers === 'object')
266-
headers = ((source.headers: any): { [key: string]: string });
267-
268269
const requestId = ImageLoader.load(
269270
{ uri, headers },
270271
function load(result) {
@@ -298,7 +299,7 @@ const Image: React.AbstractComponent<
298299

299300
return effectCleanup;
300301
}
301-
}, [source, updateState, onError, onLoad, onLoadEnd, onLoadStart]);
302+
}, [updateState, onError, onLoad, onLoadEnd, onLoadStart, uri, headers]);
302303

303304
return (
304305
<View

0 commit comments

Comments
 (0)