reactjs - 在 React 组件笑话测试中模拟 Axios 实例和拦截器
问题描述
我正在测试一个调用 API 来用数据填充表的组件。尽管使用了 axios,但 axios 被包装在一种方便的方法中,以便在通过拦截器执行请求之前填充标头。我已经尝试过 axios-mock-adapter,但它不起作用。我仍然是测试 React 的新手,我对如何模拟从 api/axios 返回的数据一无所知。如何模拟 api 调用来模拟数据以使我的测试通过?
这是我的简单测试:
test('<EmailTable/> ', async () => {
const { debug, getByText } = render(<CommunicationEmail />);
await waitFor(() => expect(getByText('Test Email Subject')).toBeTruthy());
}
这是 axios 包装器(api.js):
const instance = axios.create({
baseURL: `${apiUrl}/v1`,
timeout: 12000,
withCredentials: true,
headers: headers,
});
//intercept requests to validate hashed auth token
instance.interceptors.request.use((request) => {
const token = request.headers['X-Our-Access-Token'];
if (
localStorage.getItem('user_token') == null ||
SHA256(token).toString(enc.Hex) == localStorage.getItem('user_token')
) {
return request;
} else {
console.log({ what: 'Auth key invalid' });
return Promise.reject('Invalid token!');
}
});
//intercept responses to handle 401 errors
instance.interceptors.response.use(
(response) => {
return response;
},
(error) => {
// handle 401 authentication errors and redirect to SSO
if (error.response != null && error.response.status != null && error.response.status === 401) {
console.error({ what: 'Authorization error', e: error });
}
return Promise.reject(error);
}
);
export default instance;
这是我要测试的组件的简化:
import api from './api.js';
const EmailTable = () => {
const [emails, setEmails] = useState();
useEffect(() => {
if(!emails) {
getEmails();
}
}, [emails]);
const getEmails = async () => {
await api({
method: 'GET',
url: `/communications/emails`,
}).then((response) => {
if (response.success) {
setEmails(response.emails);
}
}
}
if(!emails) { return <div> Loading... </div> };
return <div>{emails}</div>;
}
更新解决方案:
为了模拟作为我的 API 的 axios 包装器,我必须模拟 api 模块并返回一个已解决的承诺,如下所示:
jest.mock('../api', () => {
return function (request) {
// If we want to mock out responses to multiple API requests, we could do if (request.url = "/blah/blah") { return new Promise.... }
return new Promise((resolve) => {
resolve({
data: { success: true, emails: [] },
});
});
};
});
解决方案
推荐阅读
- visual-studio-code - Visual Studio 代码给出错误“拒绝访问”和“系统无法执行指定的程序”。交替
- python - 对同时包含字母和数字的 Python 列表进行排序
- javascript - 关于嵌套 try catch 语句和错误处理的问题
- python - python pandas合并数据框
- react-native - 到达 ListFooterComponent 末尾时调用 React Native FlatList onEndReached
- ms-office - 将列中的单元格值填充到 Excel Office.js 中另一列中的空白单元格
- javascript - 将我的 JQuery 函数转换为 vanilla js - 多个 forEach 的问题?
- python - 如何从 Python 访问在 EMR 集群中创建的 Hive
- python - 使用 Exchangelib 加快获取电子邮件的速度
- certificate - 在 andorid tv 上安装 CRT 证书文件