我正在使用 Jest 和 Enzyme 来测试 React 功能组件。
我的组件:
export const getGroups = async () => {
const data = await fetch(groupApiUrl);
return await data.json()
};
export default function MyWidget({
groupId,
}) {
// Store group object in state
const [group, setGroup] = useState(null);
// Retrive groups on load
useEffect(() => {
if (groupId && group === null) {
const runEffect = async () => {
const { groups } = await getGroups();
const groupData = groups.find(
g => g.name === …Run Code Online (Sandbox Code Playgroud) 我真的很困惑试图用玩笑的文档的帮助下创建测试https://facebook.github.io/jest/docs/timer-mocks.html#content
我正在尝试检查容器安装时的状态,然后几秒后,我在状态下手动设置值(使用setTimeout()).
我在Main的componentDidMount中有一个函数,如下所示:
componentDidMount() {
this.setStateAfterDelay();
}
Run Code Online (Sandbox Code Playgroud)
该功能的作用是:
setStateAfterDelay = () => {
setTimeout(() => {
this.setState({ fruits: ['banana', 'apple', 'orange', 'vodka', 'kiwi'] });
}, 1500);
}
Run Code Online (Sandbox Code Playgroud)
我完成了第一部分:
const component = mount(<Main />);
expect(component.state().fruits).toEqual(null);
Run Code Online (Sandbox Code Playgroud)
但我不知道如何再次检查状态,让我说2000ms?
任何帮助表示赞赏:)