loadAxiosConfig.spec.js
1.25 KB
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
const mockHttpGet = jest.fn(() => {
throw new Error("不应发起 HTTP 配置请求");
});
const mockHttpsGet = jest.fn(() => {
throw new Error("不应发起 HTTPS 配置请求");
});
jest.mock("http", () => ({
get: mockHttpGet,
}));
jest.mock("https", () => ({
get: mockHttpsGet,
}));
const {
loadAxiosConfig,
pickAxiosBlock,
} = require("./loadAxiosConfig");
describe("loadAxiosConfig", () => {
beforeEach(() => {
jest.clearAllMocks();
});
it("从默认开发配置读取 DJ ROP 且不访问网络", async () => {
const result = await loadAxiosConfig({
apiName: "dj",
configUrl: "https://config.invalid/never-request",
});
expect(result.env).toBe("develop");
expect(result.configKey).toBe("axios");
expect(result.baseURL).toContain("dj.dev.glsx.com.cn");
expect(mockHttpGet).not.toHaveBeenCalled();
expect(mockHttpsGet).not.toHaveBeenCalled();
});
it("从同一开发配置读取 SCRM ROP", async () => {
const result = await loadAxiosConfig({ apiName: "scrm" });
expect(result.configKey).toBe("axios2");
expect(result.baseURL).toContain("scrm.dev.glsx.com.cn");
});
it("未知 API 名称返回明确错误", () => {
expect(() => pickAxiosBlock({}, "unknown")).toThrow("Unknown apiName for axios config: unknown");
});
});