Skip to content

Commit ae00c59

Browse files
committed
[test] Image - add tests covering added/changed functionality around source
1 parent fa84da8 commit ae00c59

File tree

2 files changed

+99
-5
lines changed

2 files changed

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

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

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,38 @@ describe('components/Image', () => {
171171
expect(onLoadEndStub.mock.calls.length).toBe(2);
172172
});
173173

174+
test('is called on update if "headers" are different', () => {
175+
const onLoadStartStub = jest.fn();
176+
const onLoadStub = jest.fn();
177+
const onLoadEndStub = jest.fn();
178+
const { rerender } = render(
179+
<Image
180+
onLoad={onLoadStub}
181+
onLoadEnd={onLoadEndStub}
182+
onLoadStart={onLoadStartStub}
183+
source={{
184+
uri: 'https://test.com/img.jpg',
185+
headers: { 'x-custom-header': 'abc123' }
186+
}}
187+
/>
188+
);
189+
act(() => {
190+
rerender(
191+
<Image
192+
onLoad={onLoadStub}
193+
onLoadEnd={onLoadEndStub}
194+
onLoadStart={onLoadStartStub}
195+
source={{
196+
uri: 'https://test.com/img.jpg',
197+
headers: { 'x-custom-header': '123abc' }
198+
}}
199+
/>
200+
);
201+
});
202+
expect(onLoadStub.mock.calls.length).toBe(2);
203+
expect(onLoadEndStub.mock.calls.length).toBe(2);
204+
});
205+
174206
test('is not called on update if "uri" is the same', () => {
175207
const onLoadStartStub = jest.fn();
176208
const onLoadStub = jest.fn();
@@ -222,6 +254,42 @@ describe('components/Image', () => {
222254
expect(onLoadStub.mock.calls.length).toBe(1);
223255
expect(onLoadEndStub.mock.calls.length).toBe(1);
224256
});
257+
258+
test('is not called on update if "headers" and "uri" the same', () => {
259+
const onLoadStartStub = jest.fn();
260+
const onLoadStub = jest.fn();
261+
const onLoadEndStub = jest.fn();
262+
const { rerender } = render(
263+
<Image
264+
onLoad={onLoadStub}
265+
onLoadEnd={onLoadEndStub}
266+
onLoadStart={onLoadStartStub}
267+
source={{
268+
uri: 'https://test.com/img.jpg',
269+
width: 1,
270+
height: 1,
271+
headers: { 'x-custom-header': 'abc123' }
272+
}}
273+
/>
274+
);
275+
act(() => {
276+
rerender(
277+
<Image
278+
onLoad={onLoadStub}
279+
onLoadEnd={onLoadEndStub}
280+
onLoadStart={onLoadStartStub}
281+
source={{
282+
uri: 'https://test.com/img.jpg',
283+
width: 1,
284+
height: 1,
285+
headers: { 'x-custom-header': 'abc123' }
286+
}}
287+
/>
288+
);
289+
});
290+
expect(onLoadStub.mock.calls.length).toBe(1);
291+
expect(onLoadEndStub.mock.calls.length).toBe(1);
292+
});
225293
});
226294

227295
describe('prop "resizeMode"', () => {
@@ -242,7 +310,8 @@ describe('components/Image', () => {
242310
'',
243311
{},
244312
{ uri: '' },
245-
{ uri: 'https://google.com' }
313+
{ uri: 'https://google.com' },
314+
{ uri: 'https://google.com', headers: { 'x-custom-header': 'abc123' } }
246315
];
247316
sources.forEach((source) => {
248317
expect(() => render(<Image source={source} />)).not.toThrow();
@@ -338,6 +407,31 @@ describe('components/Image', () => {
338407
'http://localhost/static/[email protected]'
339408
);
340409
});
410+
411+
test('it correctly passes headers to ImageLoader', () => {
412+
const uri = 'https://google.com/favicon.ico';
413+
const headers = { 'x-custom-header': 'abc123' };
414+
const source = { uri, headers };
415+
render(<Image source={source} />);
416+
417+
expect(ImageLoader.load).toHaveBeenCalledWith(
418+
expect.objectContaining({ headers }),
419+
expect.any(Function),
420+
expect.any(Function)
421+
);
422+
});
423+
424+
test('it correctly passes uri to ImageLoader', () => {
425+
const uri = 'https://google.com/favicon.ico';
426+
const source = { uri };
427+
render(<Image source={source} />);
428+
429+
expect(ImageLoader.load).toHaveBeenCalledWith(
430+
expect.objectContaining({ uri }),
431+
expect.any(Function),
432+
expect.any(Function)
433+
);
434+
});
341435
});
342436

343437
describe('prop "style"', () => {

0 commit comments

Comments
 (0)