当前正在尝试使用挂钩对单元进行测试(useState和useEffects)。如我所读,生命周期只能使用安装而不是浅渲染进行测试。
实施代码:
export function MainPageRouter({issuerDeals, match, dbLoadIssuerDeals}) {
console.log("MainPageRouter")
const [isLoading, setIsLoading] = useState(true);
const selectedIssuerId = match.params.id;
const issuerDeal = filterIssuerDealByIssuerId(issuerDeals,
selectedIssuerId);
useEffect(() => {
dbLoadIssuerDeals(selectedIssuerId)
.then(() => {
setIsLoading(false);
})
.catch(function (error) {
setIsLoading(false);
});
}, [selectedIssuerId]);
if (isLoading) {
return <MainPageLoading />
} else if(issuerDeal.length > 0) {
return <MappedDeal match={match}/>
} else {
return <MapDeal match={match}/>
}
}
const mapStateToProps = state => {
return {
deals: state.deals,
issuerDeals: state.issuerDeals
}
};
const mapDispatchToProps = { …Run Code Online (Sandbox Code Playgroud) 使用“create-react-app”创建反应应用程序后,到目前为止一切都已成功。我做了一个有效的反应项目。但是现在我想用玩笑来实现单元测试。
使用 npm 安装: 'npm install --save-dev jest' 然后运行: npm ls jest 我注意到 react-script 正在寻找一个明显更旧的 jest 版本。
包.json:
"dependencies": {
"react": "^16.8.1",
"react-dom": "^16.8.1",
"react-redux": "^6.0.0",
"react-scripts": "2.1.5",
"redux": "^4.0.1"
},
"devDependencies": {
"jest": "^24.7.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "jest",
"eject": "react-scripts eject"
}
Run Code Online (Sandbox Code Playgroud)
npm ls 开玩笑的结果:
+-- jest@24.7.1
`-- react-scripts@2.1.5
`-- jest@23.6.0
Run Code Online (Sandbox Code Playgroud)
npm启动错误:
There might be a problem with the project dependency tree.
It is likely not a bug in Create React App, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 jQuery 来隐藏一大块 div 并使用一个按钮来显示更多。
但是,由于我的 div 是内联块,因此它们没有被隐藏。
只应显示前 8 个 div,其余部分应隐藏。
一旦我开始工作,我将处理显示按钮。
类似于本网站上的旧帖子按钮:https : //melodydemo.wordpress.com/
这是我用于隐藏和显示 div 的 jQuery:
$(function () {
$("div").slice(0, 8).show();
$("#loadMore").on('click', function (e) {
e.preventDefault();
$("div:hidden").slice(0, 8).slideDown();
if ($("div:hidden").length == 0) {
$("#load").fadeOut('slow');
}
$('html,body').animate({
scrollTop: $(this).offset().top
}, 1500);
});
});
$('a[href=#top]').click(function () {
$('body,html').animate({
scrollTop: 0
}, 600);
return false;
});
$(window).scroll(function () {
if ($(this).scrollTop() > 50) {
$('.totop a').fadeIn();
} else {
$('.totop a').fadeOut();
}
});
Run Code Online (Sandbox Code Playgroud)
这是我尝试的代码笔:
javascript ×2
css ×1
enzyme ×1
html ×1
jestjs ×1
jquery ×1
node.js ×1
react-redux ×1
reactjs ×1
unit-testing ×1
web ×1