xref: /expo/apps/test-suite/tests/Image.js (revision 022636e2)
1'use strict';
2
3import { Image } from 'expo-image';
4import React from 'react';
5
6import { mountAndWaitFor, mountAndWaitForWithTimeout, TimeoutError } from './helpers';
7
8export const name = 'Image';
9
10const REMOTE_SOURCE = { uri: 'http://source.unsplash.com/random' };
11const NON_EXISTENT_SOURCE = { uri: 'file://non_existent_path.jpg' };
12
13export async function test(t, { setPortalChild, cleanupPortal }) {
14  t.describe('Image', () => {
15    t.afterEach(async () => {
16      await cleanupPortal();
17    });
18
19    t.describe('onLoadStart', () => {
20      t.it('emits an event when the image starts to load (will load successfully)', async () => {
21        await mountAndWaitFor(<Image source={REMOTE_SOURCE} />, 'onLoadStart', setPortalChild);
22      });
23
24      t.it('emits an event when the image starts to load (will error)', async () => {
25        await mountAndWaitFor(
26          <Image source={NON_EXISTENT_SOURCE} />,
27          'onLoadStart',
28          setPortalChild
29        );
30      });
31    });
32
33    t.describe('onLoad', () => {
34      t.it('emits an event when the image loads successfully', async () => {
35        await mountAndWaitFor(<Image source={REMOTE_SOURCE} />, 'onLoad', setPortalChild);
36      });
37
38      t.it('does not emit an event if the image errors', async () => {
39        try {
40          await mountAndWaitForWithTimeout(
41            <Image source={NON_EXISTENT_SOURCE} />,
42            'onLoad',
43            setPortalChild,
44            3000
45          );
46        } catch (e) {
47          if (!(e instanceof TimeoutError)) {
48            throw e;
49          }
50        }
51      });
52    });
53
54    t.describe('onError', () => {
55      t.it('emits an event when the image fails to load successfully', async () => {
56        await mountAndWaitFor(<Image source={NON_EXISTENT_SOURCE} />, 'onError', setPortalChild);
57      });
58
59      t.it('does not emit an event if the image loads successfully', async () => {
60        try {
61          await mountAndWaitForWithTimeout(
62            <Image source={REMOTE_SOURCE} />,
63            'onError',
64            setPortalChild,
65            3000
66          );
67        } catch (e) {
68          if (!(e instanceof TimeoutError)) {
69            throw e;
70          }
71        }
72      });
73    });
74
75    t.describe('onLoadEnd', () => {
76      t.it('emits an event when the image loads successfully', async () => {
77        await mountAndWaitFor(<Image source={REMOTE_SOURCE} />, 'onLoadEnd', setPortalChild);
78      });
79
80      t.it('emits an event when the image errors', async () => {
81        await mountAndWaitFor(<Image source={NON_EXISTENT_SOURCE} />, 'onLoadEnd', setPortalChild);
82      });
83    });
84  });
85}
86