Skip to content

Commit fb08119

Browse files
committed
🔧 修复资源管理器测试:将 axios mock 替换为 fetch mock
1 parent 38fbb4f commit fb08119

2 files changed

Lines changed: 47 additions & 16 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ tailwind.config.js
3232

3333
superpowers
3434
.claude
35+
.omc
3536
CLAUDE.md
3637

3738
test-results

src/app/service/resource/resource.test.ts

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,35 @@ import MessageCenter from "@App/app/message/center";
33
import { MessageHander } from "@App/app/message/message";
44
import initTestEnv from "@App/pkg/utils/test_utils";
55
import ResourceManager from "./manager";
6-
import axios from "axios";
7-
import MockAdapter from "axios-mock-adapter";
86
import { 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
1237
global.sandbox = global;
@@ -18,9 +43,12 @@ IoC.registerInstance(MessageCenter, center).alias([MessageHander]);
1843
describe("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

Comments
 (0)