如何在使用效果中测试带有AXIOS请求的反应组件? [英] How to test react component with axios request in useEffect?
本文介绍了如何在使用效果中测试带有AXIOS请求的反应组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
const App = () => {
const [data, setData] = useState<IJoke | undefined>(undefined);
const [isLoading, setIsLoading] = useState<boolean>(true);
useEffect(() => {
axios
.get("https://v2.jokeapi.dev/joke/Programming?type=single")
.then((res: AxiosResponse<IJoke>) => {
setData(res.data);
})
.catch((err) => console.log(err))
.finally(() => setIsLoading(false));
}, []);
return (
<div className="App">
{isLoading ? (
<h2>Loading...</h2>
) : (
<div className="info">
<div className="info__cat">
{data?.category ? `category: ${data.category}` : "bad category"}
</div>
<div className="info__joke">
{data?.joke ? `joke: ${data?.joke}` : "bad data"}
</div>
</div>
)}
</div>
);
};
如何使用测试覆盖组件?我需要在请求之前、时间和之后测试状态。如何模拟此上下文中的请求?
推荐答案
选项1.使用msw在网络级别截取请求来模拟。
选项2.如果不想安装任何程序包和安装程序,可以使用jest.spyOn(object, 'method').mockResolvedValueOnce()
为axios.get()
方法创建模拟已解析/已拒绝值。
下面的示例使用选项2。
App.tsx
:
import axios, { AxiosResponse } from 'axios';
import React, { useEffect, useState } from 'react';
interface IJoke {
category: string;
joke: string;
}
export const App = () => {
const [data, setData] = useState<IJoke | undefined>(undefined);
const [isLoading, setIsLoading] = useState<boolean>(true);
useEffect(() => {
axios
.get('https://v2.jokeapi.dev/joke/Programming?type=single')
.then((res: AxiosResponse<IJoke>) => {
setData(res.data);
})
.catch((err) => console.log(err))
.finally(() => setIsLoading(false));
}, []);
return (
<div className="App">
{isLoading ? (
<h2>Loading...</h2>
) : (
<div className="info">
<div className="info__cat">{data?.category ? `category: ${data.category}` : 'bad category'}</div>
<div className="info__joke">{data?.joke ? `joke: ${data?.joke}` : 'bad data'}</div>
</div>
)}
</div>
);
};
App.test.tsx
:
import { App } from './App';
import axios, { AxiosResponse } from 'axios';
import { act, render, screen } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import React from 'react';
describe('70450576', () => {
afterEach(() => {
jest.restoreAllMocks();
});
test('should render category and joke', async () => {
const mAxiosResponse = {
data: { category: 'smart', joke: 'sam' },
} as AxiosResponse;
jest.spyOn(axios, 'get').mockResolvedValueOnce(mAxiosResponse);
render(<App />);
expect(screen.getByText('Loading...')).toBeInTheDocument();
expect(await screen.findByText('category: smart')).toBeInTheDocument();
expect(await screen.findByText('joke: sam')).toBeInTheDocument();
});
});
测试结果:
PASS examples/70450576/App.test.tsx (8.874 s)
70450576
✓ should render category and joke (43 ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 91.67 | 72.22 | 80 | 90.91 |
App.tsx | 91.67 | 72.22 | 80 | 90.91 | 19
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 9.393 s, estimated 10 s
这篇关于如何在使用效果中测试带有AXIOS请求的反应组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文