@@ -3,6 +3,7 @@ import { createReadStream } from 'fs';
33import { readFile } from 'fs/promises' ;
44import fetchMock from 'jest-fetch-mock' ;
55import path from 'path' ;
6+ import { Readable } from 'stream' ;
67
78import { NpmLocation } from './npm' ;
89
@@ -22,9 +23,13 @@ describe('NpmLocation', () => {
2223
2324 const tarballUrl = `https://registry.npmjs.cf/@metamask/template-snap/-/template-snap-${ templateSnapVersion } .tgz` ;
2425 const tarballRegistry = `https://registry.npmjs.org/@metamask/template-snap/-/template-snap-${ templateSnapVersion } .tgz` ;
25- fetchMock
26- . mockResponseOnce (
27- JSON . stringify ( {
26+
27+ const customFetchMock = jest . fn ( ) ;
28+
29+ customFetchMock
30+ . mockResolvedValueOnce ( {
31+ ok : true ,
32+ json : async ( ) => ( {
2833 // eslint-disable-next-line @typescript-eslint/naming-convention
2934 'dist-tags' : {
3035 latest : templateSnapVersion ,
@@ -38,27 +43,26 @@ describe('NpmLocation', () => {
3843 } ,
3944 } ,
4045 } ) ,
41- )
42- . mockResponseOnce (
43- ( _req ) =>
44- Promise . resolve ( {
45- ok : true ,
46- // eslint-disable-next-line @typescript-eslint/naming-convention
47- headers : new Headers ( { 'content-length' : '5477' } ) ,
48- body : createReadStream (
49- path . resolve (
50- __dirname ,
51- `../../../test/fixtures/metamask-template-snap-${ templateSnapVersion } .tgz` ,
52- ) ,
46+ } as any )
47+ . mockResolvedValue ( {
48+ ok : true ,
49+ // eslint-disable-next-line @typescript-eslint/naming-convention
50+ headers : new Headers ( { 'content-length' : '5477' } ) ,
51+ body : Readable . toWeb (
52+ createReadStream (
53+ path . resolve (
54+ __dirname ,
55+ `../../../test/fixtures/metamask-template-snap-${ templateSnapVersion } .tgz` ,
5356 ) ,
54- } ) as any ,
55- ) ;
57+ ) ,
58+ ) ,
59+ } as any ) ;
5660
5761 const location = new NpmLocation (
5862 new URL ( 'npm://registry.npmjs.cf/@metamask/template-snap' ) ,
5963 {
6064 versionRange : templateSnapVersion ,
61- fetch : fetchMock as typeof fetch ,
65+ fetch : customFetchMock as typeof fetch ,
6266 allowCustomRegistries : true ,
6367 } ,
6468 ) ;
@@ -72,12 +76,13 @@ describe('NpmLocation', () => {
7276 await location . fetch ( manifest . result . source . location . npm . iconPath )
7377 ) . toString ( ) ;
7478
75- expect ( fetchMock ) . toHaveBeenCalledTimes ( 2 ) ;
76- expect ( fetchMock ) . toHaveBeenNthCalledWith (
79+ expect ( customFetchMock ) . toHaveBeenCalledTimes ( 2 ) ;
80+ expect ( customFetchMock ) . toHaveBeenNthCalledWith (
7781 1 ,
7882 'https://registry.npmjs.cf/@metamask/template-snap' ,
83+ { headers : { accept : 'application/json' } } ,
7984 ) ;
80- expect ( fetchMock ) . toHaveBeenNthCalledWith ( 2 , tarballUrl ) ;
85+ expect ( customFetchMock ) . toHaveBeenNthCalledWith ( 2 , tarballUrl ) ;
8186
8287 expect ( manifest . result ) . toStrictEqual (
8388 JSON . parse (
@@ -109,25 +114,95 @@ describe('NpmLocation', () => {
109114 ) . toString ( 'utf8' ) ,
110115 ) ;
111116
112- const tarballUrl = `https://registry.npmjs.org/@metamask/template-snap/-/template-snap-${ templateSnapVersion } .tgz` ;
113- fetchMock . mockResponseOnce (
114- ( _req ) =>
115- Promise . resolve ( {
116- ok : true ,
117- // eslint-disable-next-line @typescript-eslint/naming-convention
118- headers : new Headers ( { 'content-length' : '5477' } ) ,
119- body : createReadStream (
120- path . resolve (
121- __dirname ,
122- `../../../test/fixtures/metamask-template-snap-${ templateSnapVersion } .tgz` ,
123- ) ,
117+ const customFetchMock = jest . fn ( ) ;
118+
119+ customFetchMock . mockResolvedValue ( {
120+ ok : true ,
121+ // eslint-disable-next-line @typescript-eslint/naming-convention
122+ headers : new Headers ( { 'content-length' : '5477' } ) ,
123+ body : Readable . toWeb (
124+ createReadStream (
125+ path . resolve (
126+ __dirname ,
127+ `../../../test/fixtures/metamask-template-snap-${ templateSnapVersion } .tgz` ,
124128 ) ,
125- } ) as any ,
129+ ) ,
130+ ) ,
131+ } as any ) ;
132+
133+ const tarballUrl = `https://registry.npmjs.org/@metamask/template-snap/-/template-snap-${ templateSnapVersion } .tgz` ;
134+
135+ const location = new NpmLocation ( new URL ( 'npm:@metamask/template-snap' ) , {
136+ versionRange : templateSnapVersion ,
137+ fetch : customFetchMock as typeof fetch ,
138+ } ) ;
139+
140+ const manifest = await location . manifest ( ) ;
141+ const sourceCode = (
142+ await location . fetch ( manifest . result . source . location . npm . filePath )
143+ ) . toString ( ) ;
144+ assert ( manifest . result . source . location . npm . iconPath ) ;
145+ const svgIcon = (
146+ await location . fetch ( manifest . result . source . location . npm . iconPath )
147+ ) . toString ( ) ;
148+
149+ expect ( customFetchMock ) . toHaveBeenCalledTimes ( 1 ) ;
150+ expect ( customFetchMock ) . toHaveBeenNthCalledWith ( 1 , tarballUrl ) ;
151+
152+ expect ( manifest . result ) . toStrictEqual (
153+ JSON . parse (
154+ (
155+ await readFile (
156+ require . resolve ( '@metamask/template-snap/snap.manifest.json' ) ,
157+ )
158+ ) . toString ( 'utf8' ) ,
159+ ) ,
160+ ) ;
161+
162+ expect ( sourceCode ) . toStrictEqual (
163+ (
164+ await readFile (
165+ require . resolve ( '@metamask/template-snap/dist/bundle.js' ) ,
166+ )
167+ ) . toString ( 'utf8' ) ,
168+ ) ;
169+
170+ expect ( svgIcon ?. startsWith ( '<svg' ) && svgIcon . endsWith ( '</svg>' ) ) . toBe (
171+ true ,
126172 ) ;
173+ } ) ;
174+
175+ it ( 'falls back to zlib if DecompressionStream is unavailable' , async ( ) => {
176+ // @ts -expect-error Deleting DecompressionStream for testing reasons
177+ delete globalThis . DecompressionStream ;
178+
179+ const { version : templateSnapVersion } = JSON . parse (
180+ (
181+ await readFile ( require . resolve ( '@metamask/template-snap/package.json' ) )
182+ ) . toString ( 'utf8' ) ,
183+ ) ;
184+
185+ const customFetchMock = jest . fn ( ) ;
186+
187+ customFetchMock . mockResolvedValue ( {
188+ ok : true ,
189+ // eslint-disable-next-line @typescript-eslint/naming-convention
190+ headers : new Headers ( { 'content-length' : '5477' } ) ,
191+ body : Readable . toWeb (
192+ createReadStream (
193+ path . resolve (
194+ __dirname ,
195+ `../../../test/fixtures/metamask-template-snap-${ templateSnapVersion } .tgz` ,
196+ ) ,
197+ ) ,
198+ ) ,
199+ } as any ) ;
200+
201+ const tarballUrl = `https://registry.npmjs.org/@metamask/template-snap/-/template-snap-${ templateSnapVersion } .tgz` ;
127202
128203 const location = new NpmLocation ( new URL ( 'npm:@metamask/template-snap' ) , {
129204 versionRange : templateSnapVersion ,
130- fetch : fetchMock as typeof fetch ,
205+ fetch : customFetchMock as typeof fetch ,
131206 } ) ;
132207
133208 const manifest = await location . manifest ( ) ;
@@ -139,8 +214,8 @@ describe('NpmLocation', () => {
139214 await location . fetch ( manifest . result . source . location . npm . iconPath )
140215 ) . toString ( ) ;
141216
142- expect ( fetchMock ) . toHaveBeenCalledTimes ( 1 ) ;
143- expect ( fetchMock ) . toHaveBeenNthCalledWith ( 1 , tarballUrl ) ;
217+ expect ( customFetchMock ) . toHaveBeenCalledTimes ( 1 ) ;
218+ expect ( customFetchMock ) . toHaveBeenNthCalledWith ( 1 , tarballUrl ) ;
144219
145220 expect ( manifest . result ) . toStrictEqual (
146221 JSON . parse (
0 commit comments