测试React中的上一个和下一个按钮功能失败
P粉936509635
2023-08-15 13:54:35
<p>我的React代码测试失败:
如果我写错了,你能帮忙写对吗?
我尝试使用screen.getByRole('button', {name: '>'})来获取下一个按钮,使用screen.getByRole('button', {name: '<'})来获取上一个按钮,甚至尝试了下面提到的测试代码,但测试一直失败。</p>
<pre class="brush:php;toolbar:false;">export default function App() {
return (
<div className="App">
<div>
{dogs
.map((value, key) => (
<div className="card-container" key={key}>
<div>
<img className="img" src={value.imgUrl} alt={value.title} />
</div>
<div className="card-footer">
{dogCurrPage !== 1 ? (
<button className="back-button" onClick={() => previous()}>
{"<"}
</button>
) : null}
<span className="card-title-text">{value.title}</span>
{dogCurrPage !== dogs.length - 1 ? (
<button className="next-button" onClick={() => next()}>
{">"}
</button>
) : null}{" "}
</div>
</div>
))}
</div>
</div>
);
}</pre>
<pre class="brush:php;toolbar:false;">describe("App", () => {
it("正确处理上一个按钮的点击", () => {
render(<App />);
fireEvent.click(screen.getByText("<"));
});
it("正确处理下一个按钮的点击", () => {
render(<App />);
fireEvent.click(screen.getByText(">"));
});
});</pre>
也许尝试这段代码: