xref: /expo/apps/test-suite/tests/Asset.js (revision cb0e1ce3)
1'use strict';
2
3import * as FileSystem from 'expo-file-system';
4import { Asset } from 'expo-asset';
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    ].forEach(({ module, name, type, ...more }) =>
26      t.describe(`${name}.${type}`, () => {
27        t.it(`has correct name, type, ${Object.keys(more).join(', ')}`, async () => {
28          const asset = Asset.fromModule(module);
29          t.expect(asset.name).toBe(name);
30          t.expect(asset.type).toBe(type);
31          Object.keys(more).forEach(member => t.expect(asset[member]).toBe(more[member]));
32        });
33
34        t.it("when downloaded, has a 'file://' localUri", async () => {
35          const asset = Asset.fromModule(module);
36          await asset.downloadAsync();
37          t.expect(asset.localUri).toMatch(new RegExp(`^file:\/\/.*\.${type}`));
38        });
39
40        t.it(
41          'when downloaded, exists in cache with matching hash and has ' +
42            'localUri pointing to the cached file',
43          async () => {
44            const asset = Asset.fromModule(module);
45            await asset.downloadAsync();
46
47            const { exists, md5, uri: cacheUri } = await FileSystem.getInfoAsync(asset.localUri, {
48              cache: true,
49              md5: true,
50            });
51
52            t.expect(exists).toBeTruthy();
53            t.expect(md5).toBe(asset.hash);
54            t.expect(cacheUri).toBe(asset.localUri);
55          }
56        );
57      })
58    );
59  });
60}
61