请问老师,useState的值如何mock?

来源:2-12 Jest 中的 Mock(2)

鎏金华舞

2021-02-26

写回答

1回答

鎏金华舞

提问者

2021-02-26

import React, { useState } from 'react';
import Taro, { useDidShow } from '@tarojs/taro';
import { View, Navigator, Text } from '@tarojs/components';
import './index.less';

export default function Index() {
const [bindPhone, setBindPhone] = useState('');
useDidShow(() => {
const { bindPhone } = Taro.getStorageSync('person');
setBindPhone(bindPhone);
});

return bindPhone ? (
<View className="login-components">
<Navigator className="login-content font-medium" url="/page/registerPack/index">
<View className="login-text">登录 / 注册 </View>
</Navigator>
</View>
) : null;

}

请问,我想测试 bindPhone 有值的情况下渲染这个组件,没有值的情况不渲染。如何去测试mock呢,写了很久,网上没找到好的例子


0
1
Dell
参考这篇文章,一看就懂:https://dev.to/theactualgivens/testing-react-hook-state-changes-2oga
2021-02-28
共1条回复

前端要学的测试课 从Jest入门到 TDD/BDD双实战

自动化测试实战 Vue / React 项目,让技术水平和架构思维双提升

1433 学习 · 350 问题

查看课程