reactjs - 如何测试(断言)使用钩子的 React 组件的中间状态
问题描述
这个问题是关于:如何测试使用useEffect
钩子和useState
钩子的组件,使用react-testing-library
.
我有以下组件:
function MyComponent() {
const [state, setState] = useState(0);
useEffect(() => {
// 'request' is an async call which takes ~2seconds to complete
request().then(() => {
setState(1);
});
}, [state]);
return <div>{state}</div>
}
当我渲染这个应用程序时,我看到的行为如下:
- 该应用程序最初呈现
0
- 大约 2 秒后,应用程序呈现
1
现在,我想使用 and 来测试和断言这种react-testing-library
行为jest
。
这是我到目前为止所拥有的:
import {render, act} from '@testing-library/react';
// Ignoring the describe wrapper for the simplicity
test('MyComponent', async () => {
let result;
await act(async () => {
result = render(<MyComponent />);
});
expect(result.container.firstChild.textContent).toBe(1);
})
测试通过。但是,我还想断言用户最初看到应用程序呈现的事实0
(在1
2 秒后呈现之前)。
我怎么做?提前致谢。
解决方案
正如 Sunil Pai 在此博客中指出的那样:https ://github.com/threepointone/react-act-examples/blob/master/sync.md
这是我设法解决这个问题的方法:
import {request} from '../request';
jest.mock('../request');
test('MyComponent', async () => {
let resolve;
request.mockImplementation(() => new Promise(resolve => {
// Save the resolver to a local variable
// so that we can trigger the resolve action later
resolve = _resolve;
}));
let result;
await act(async () => {
result = render(<MyComponent />);
});
// Unlike the non-mocked example in the question, we see '0' as the result
// This is because the value is not resolved yet
expect(result.container.firstChild.textContent).toBe('0');
// Now the setState will be called inside the useEffect hook
await act(async () => resolve());
// So now, the rendered value will be 1
expect(result.container.firstChild.textContent).toBe('1');
})
推荐阅读
- android - Android房间数据库创建没有ID的实体对象
- node.js - 使用网络语音 API 的网络应用程序对话流中的上下文不起作用
- python - 使用 Python 以大写形式保存文本文件
- c# - 工厂的依赖注入生命周期
- c++ - C++/QT继承程序架构
- c# - 为什么我会丢失信息?
- python-3.x - 我正在使用 openpyxl 它打开一些 xl 文件并且无法打开具有相同格式的其他 xl 文件
- c# - Fill() 方法在更新父属性时不会引发错误
- c# - 发布单个 Razor 页面后出现编译错误。发布整个项目后仍然存在。(“可能缺少编译参考”)
- opengl - 限制多边形边缘的纹理坐标