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