我在模拟模块内部的函数时遇到麻烦。 (不确定是否可行。
所以我有一个名为myActions.ts的模块
import * as Config from "../utils/Config";
const _config = Config.getConfig();
export const requestLoadDataA = (postLoadAction: Function = undefined) => {
return async function (dispatch, getState) {
const client = { url: `${_config.ApiUrl}getDataA` };
...
}
}
该模块包含一个Config.getConfig(),这就是我要模拟的内容。
配置模块如下所示:
export const getConfig = () => {
const app = document.getElementById("react-app");
if (app) {
const config = app.dataset.configuration;
return JSON.parse(config) as IConfiguration;
} else {
return undefined;
}
};
这是我到目前为止进行的开玩笑的测试,但无效:
describe("DATA_A Action Creator (Sync): Tests", () => {
afterEach(fetchMock.restore);
it("REQUEST and SUCCESS actions on successful loadData()", () => {
const dataA: any = require("../../__mockData__/dataA.json");
fetchMock.mock("/getDataA", {
status: 200,
body: dataA
});
const _config = { };
const spy = jest.spyOn(Config, "getConfig");
spy.mockReturnValue(_config);
const store = mockStore({
dataA: {
hasLoadedEntities: false,
isLoadingEntities: false
}
});
return store.dispatch(aActions.requestLoadDataA())
.then(() => {
const expectedActions = store.getActions();
expect(expectedActions.length).toEqual(2);
expect(expectedActions).toContainEqual({ type: ACTION_TYPES.LOAD_A_REQUESTED });
expect(expectedActions).toContainEqual({ type: ACTION_TYPES.LOAD_A_SUCCESS, data: resultData });
});
});
}
我得到一个“无法读取未定义的属性'ApiUrl'。 如何模拟_config.ApiUrl对象?
答案 0 :(得分:1)
不确定我是否了解你。
您可以像这样模拟默认导入
Dockers