xref: /expo/apps/test-suite/tests/Asset.js (revision b52e368f)
1'use strict';
2
3import { Asset } from 'expo-asset';
4import * as FileSystem from 'expo-file-system';
5
6export const name = 'Asset';
7
8export function test(t) {
9  t.describe('Asset', () => {
10    [
11      {
12        module: require('../assets/black-128x256.png'),
13        name: 'black-128x256',
14        type: 'png',
15        hash: '4e3911b395b3cc00e737be94c7ed49bb',
16        width: 128,
17        height: 256,
18      },
19      {
20        module: require('../assets/comic.ttf'),
21        name: 'comic',
22        type: 'ttf',
23        hash: '69d77ab5cba970d7934a5f5bcd8fdd11',
24      },
25      {
26        module: 'https://docs.expo.dev/static/images/header-logo.png',
27        name: '',
28        type: 'png',
29        hash: null,
30      },
31    ].forEach(({ module, name, type, ...more }) =>
32      t.describe(`${name}.${type}`, () => {
33        t.it(`has correct name, type, ${Object.keys(more).join(', ')}`, async () => {
34          const asset = Asset.fromModule(module);
35          t.expect(asset.name).toBe(name);
36          t.expect(asset.type).toBe(type);
37          Object.keys(more).forEach((member) => t.expect(asset[member]).toBe(more[member]));
38        });
39
40        t.it("when downloaded, has a 'file://' localUri", async () => {
41          const asset = Asset.fromModule(module);
42          await asset.downloadAsync();
43          t.expect(asset.localUri).toMatch(new RegExp(`^file:\/\/.*\.${type}`));
44        });
45
46        t.it(
47          'when downloaded, exists in cache with matching hash and has ' +
48            'localUri pointing to the cached file',
49          async () => {
50            const asset = Asset.fromModule(module);
51            await asset.downloadAsync();
52
53            const {
54              exists,
55              md5,
56              uri: cacheUri,
57            } = await FileSystem.getInfoAsync(asset.localUri, {
58              cache: true,
59              md5: true,
60            });
61
62            t.expect(exists).toBeTruthy();
63            more['hash'] && t.expect(md5).toBe(asset.hash);
64            t.expect(cacheUri).toBe(asset.localUri);
65          }
66        );
67      })
68    );
69  });
70}
71