-
Notifications
You must be signed in to change notification settings - Fork 542
/
local.test.ts
106 lines (92 loc) · 3.17 KB
/
local.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import {
DEFAULT_SNAP_BUNDLE,
getSnapManifest,
} from '@metamask/snaps-utils/test-utils';
import fetchMock from 'jest-fetch-mock';
import { LocalLocation } from './local';
fetchMock.enableMocks();
describe('LocalLocation', () => {
beforeEach(() => {
fetchMock.resetMocks();
});
it('fetches files', async () => {
const manifest = getSnapManifest();
const manifestStr = JSON.stringify(manifest);
fetchMock.mockResponses(manifestStr, DEFAULT_SNAP_BUNDLE);
const location = new LocalLocation(new URL('local:http://localhost'));
expect(await location.manifest()).toStrictEqual(
expect.objectContaining({ value: manifestStr, result: manifest }),
);
const bundle = (
await location.fetch(manifest.source.location.npm.filePath)
).toString();
expect(bundle).toBe(DEFAULT_SNAP_BUNDLE);
});
it('signals that it should be reloaded', () => {
expect(
new LocalLocation(new URL('local:http://localhost')).shouldAlwaysReload,
).toBe(true);
});
it.each([
[
'local:http://localhost/foo',
{
manifest: 'local:http://localhost/snap.manifest.json',
bundle: 'local:http://localhost/foo.js',
},
],
[
'local:http://127.0.0.1/foo/',
{
manifest: 'local:http://127.0.0.1/foo/snap.manifest.json',
bundle: 'local:http://127.0.0.1/foo/foo.js',
},
],
[
'local:https://user:pass@[::1]:8080/foo/bar',
{
manifest: 'local:https://user:pass@[::1]:8080/foo/snap.manifest.json',
bundle: 'local:https://user:pass@[::1]:8080/foo/foo.js',
},
],
])('sets paths properly for %s', async (base, canonical) => {
fetchMock.mockResponses(
JSON.stringify(getSnapManifest()),
DEFAULT_SNAP_BUNDLE,
);
const location = new LocalLocation(new URL(base));
const manifest = await location.manifest();
const bundle = await location.fetch('./foo.js');
expect(manifest.path).toBe('snap.manifest.json');
expect(bundle.path).toBe('foo.js');
expect(manifest.data.canonicalPath).toBe(canonical.manifest);
expect(bundle.data.canonicalPath).toBe(canonical.bundle);
});
it.each([
['local:http://localhost/foo', 'http://localhost/snap.manifest.json'],
['local:https://localhost/foo', 'https://localhost/snap.manifest.json'],
['local:http://localhost/foo/', 'http://localhost/foo/snap.manifest.json'],
])('fetches manifest from proper location', async (base, actuallyFetched) => {
fetchMock.mockResponses(JSON.stringify(getSnapManifest()));
await new LocalLocation(new URL(base)).manifest();
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenNthCalledWith(
1,
actuallyFetched,
expect.anything(),
);
});
it.each(['local:http://localhost', 'local:https://localhost'])(
'fetches with caching disabled',
async (url) => {
fetchMock.mockResponses(DEFAULT_SNAP_BUNDLE);
await new LocalLocation(new URL(url)).fetch('./foo.js');
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenNthCalledWith(
1,
expect.anything(),
expect.objectContaining({ cache: 'no-cache' }),
);
},
);
});