【发布时间】:2021-04-10 07:40:15
【问题描述】:
我目前正在使用 getByTestId() 在我的 React 应用程序中进行测试。测试如下:
it('should be truthy when clicking to play or pause if "hasVideo" is true', () => {
const newProps = {
hasVideo: true
}
render(<VideoCardForm {...newProps} />);
const videoEl = screen.getByTestId('video-play-pause');
const result = fireEvent.click(videoEl);
expect(result).toBeTruthy();
});
相关的 DOM 部分如下所示:
<video
data-testid="video-play-pause"
className={this.state.isPlaying ? classes.VideoOnPlay : classes.VideoPrep }
onClick={(event) => {
this.onVideoClick(this.props.url, event);
}}
src={`${this.props.url}`}
>
</video>
这行得通。但是对我来说,像这样向 DOM 添加一些东西似乎很笨拙:data-testid,仅用于运行测试。有没有我可以添加到 DOM 的替代方案,它实际上服务于语义目的而不仅仅是允许运行测试?
【问题讨论】:
标签: create-react-app react-testing-library