Javascript 在React组件中测试API调用
我想在我的React组件中测试API调用,并通过上网尝试了几种方法 MyComponent:Javascript 在React组件中测试API调用,javascript,reactjs,unit-testing,jestjs,Javascript,Reactjs,Unit Testing,Jestjs,我想在我的React组件中测试API调用,并通过上网尝试了几种方法 MyComponent: export const API = { getData() { return fetch(localStorage.blobUrl).then(res => res.blob()) } } const downloadArchiveFile = () => { localStorage.setItem('blobUr
export const API = {
getData() {
return fetch(localStorage.blobUrl).then(res => res.blob())
}
}
const downloadArchiveFile = () => {
localStorage.setItem('blobUrl',bloburl);
API.getData().then((blob) => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.style.display = "none";
a.href = url;
// the filename you want
a.download = templateName;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
})
};
MyComponent.test.js
it('api call', async () => {
const fakeUserResponse = {
'myResponse': {
'a': '1',
'b': '2' }
};
var apiFunc = jest.spyOn(API, 'getData').mockImplementationOnce(() => {
return Promise.resolve({
json: () => Promise.resolve(fakeUserResponse)
})
})
})
测试用例正在通过。但在看到代码覆盖率时,此代码未被覆盖。
请参阅代码覆盖率报告
我所指的链接:
export const API = {
getData() {
return fetch(localStorage.blobUrl).then(res => res.blob())
}
}
const downloadArchiveFile = () => {
localStorage.setItem('blobUrl',bloburl);
API.getData().then((blob) => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.style.display = "none";
a.href = url;
// the filename you want
a.download = templateName;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
})
};
您使用哪个库来测试react组件<代码>反应测试库或
酶
?我使用了酶库