@@ -3,10 +3,35 @@ import MessageCenter from "@App/app/message/center";
33import { MessageHander } from "@App/app/message/message" ;
44import initTestEnv from "@App/pkg/utils/test_utils" ;
55import ResourceManager from "./manager" ;
6- import axios from "axios" ;
7- import MockAdapter from "axios-mock-adapter" ;
86import { Script } from "@App/app/repo/scripts" ;
9- const mock = new MockAdapter ( axios ) ;
7+
8+ // mock fetch 路由表
9+ const fetchMocks : Record <
10+ string ,
11+ { status : number ; blob : Blob ; contentType : string }
12+ > = { } ;
13+
14+ function mockFetchRoute (
15+ url : string ,
16+ status : number ,
17+ blob : Blob ,
18+ contentType : string
19+ ) {
20+ fetchMocks [ url ] = { status, blob, contentType } ;
21+ }
22+
23+ // @ts -ignore
24+ global . fetch = jest . fn ( ( url : string ) => {
25+ const mock = fetchMocks [ url ] ;
26+ if ( ! mock ) {
27+ return Promise . reject ( new Error ( `not implemented` ) ) ;
28+ }
29+ return Promise . resolve ( {
30+ status : mock . status ,
31+ blob : ( ) => Promise . resolve ( mock . blob ) ,
32+ headers : new Headers ( { "content-type" : mock . contentType } ) ,
33+ } ) ;
34+ } ) ;
1035
1136// @ts -ignore
1237global . sandbox = global ;
@@ -18,9 +43,12 @@ IoC.registerInstance(MessageCenter, center).alias([MessageHander]);
1843describe ( "resource manager" , ( ) => {
1944 const manager = IoC . instance ( ResourceManager ) as ResourceManager ;
2045 it ( "get resource" , async ( ) => {
21- mock . onGet ( "http://localhost/resource" ) . reply ( 200 , new Blob ( [ "test" ] ) , {
22- "content-type" : "application/octet-stream" ,
23- } ) ;
46+ mockFetchRoute (
47+ "http://localhost/resource" ,
48+ 200 ,
49+ new Blob ( [ "test" ] ) ,
50+ "application/octet-stream"
51+ ) ;
2452 const resource = await manager . getResource (
2553 1 ,
2654 "http://localhost/resource" ,
@@ -36,11 +64,12 @@ describe("resource manager", () => {
3664 expect ( resource ) . toEqual ( resource2 ) ;
3765 } ) ;
3866 it ( "not text" , async ( ) => {
39- mock
40- . onGet ( "http://localhost/require" )
41- . reply ( 200 , new Blob ( [ String . fromCharCode ( 1 ) + String . fromCharCode ( 2 ) ] ) , {
42- "content-type" : "application/octet-stream" ,
43- } ) ;
67+ mockFetchRoute (
68+ "http://localhost/require" ,
69+ 200 ,
70+ new Blob ( [ String . fromCharCode ( 1 ) + String . fromCharCode ( 2 ) ] ) ,
71+ "application/octet-stream"
72+ ) ;
4473 const require = await manager . getResource (
4574 1 ,
4675 "http://localhost/require" ,
@@ -49,11 +78,12 @@ describe("resource manager", () => {
4978 expect ( require ! . content ) . toEqual ( "" ) ;
5079 } ) ;
5180 it ( "bad resource" , async ( ) => {
52- mock
53- . onGet ( "http://localhost/require2" )
54- . reply ( 200 , new Blob ( [ "test" ] , { type : "text/javascript" } ) , {
55- "content-type" : "text/javascript" ,
56- } ) ;
81+ mockFetchRoute (
82+ "http://localhost/require2" ,
83+ 200 ,
84+ new Blob ( [ "test" ] , { type : "text/javascript" } ) ,
85+ "text/javascript"
86+ ) ;
5787 const script : Script = {
5888 metadata : {
5989 require : [ "http://localhost/require2" , "http://bad/resource" ] ,
0 commit comments