仍在惹恼React Components的内容以及如何监视内容
我有这个函数,它基本上过滤了一个列表并在我输入team
的值时返回匹配:
findMatchesForTeam = (team, venue) => {
if (team) {
console.log(team) // THIS GETS THE RIGHT TEAM (e.g. Chelsea) WHEN TESTING
return this.props.matches.filter(t => t.homeTeam === team.name || t.awayTeam === team.name)
.map((match) => (
this.result(match)
))
}
}
我的测试:
describe('other functions', () => {
it('expects the matches array to have been filtered', () => {
wrapper.instance().findMatchesForTeam('Chelsea');
expect(matchesStub.length).to.equal(2);
});
});
所以我在我的组件中调用方法并搜索“Chelsea”并且在变量matchesStub
中我得到了:
const matchesStub = [
{awayGoals: 1, awayTeam: "Man City", homeGoals: 1, homeTeam: "Arsenal", month: "February"},
{awayGoals: 2, awayTeam: "Man City", homeGoals: 3, homeTeam: "Chelsea", month: "February"},
{awayGoals: 0, awayTeam: "Chelsea", homeGoals: 0, homeTeam: "Man Utd", month: "February"}
];
然而,我的测试表明它应该是长度3(这是初始长度)并且不会过滤。代码有效,但测试没有。
答案 0 :(得分:1)
感谢上面的Will,这个解决方案有效:
expect(wrapper.instance().findMatchesForTeam('Chelsea').length).to.equal(2)
实际上,这并不完全正确。最终的解决方案是:
const teamToFilterBy = {name: 'Chelsea'};
expect(wrapper.instance().findMatchesForTeam(teamToFilterBy).length).to.equal(2);
这是因为我的上面的过滤方法因为需要name属性而接受一个对象。在我的测试中,我只传递了一个字符串,所以即使它正确记录它,显然字符串也不能有.name
属性。