我在反应应用程序中使用视频反应。我必须测试一个从视频反应组件调用ref方法的组件。 我希望使用酶,所以我可以挂载组件并检查我是否在ComponentDidMount中调用了ref方法。
基本上,我如何测试refs.player.seek会像下面那样被调用?
swal({ }).then(() =>{
//you can access this here
});
答案 0 :(得分:1)
由于video-react要求refs调用play,暂停并寻求我的知识,我最终使用了这个模式,所以我可以模拟我自己的ref回调来命中渲染方法。
https://medium.com/@planttheidea/never-render-in-react-testing-again-fc4bcfc2f71d
示例组件代码:
export const createComponentWillReceiveProps = instance => ({seekTime, isPlaying, onSeekComplete}) => {
const {paused} = instance.playerRef.getState().player;
if (seekTime) {
instance.playerRef.seek(seekTime);
onSeekComplete();
}
if (isPlaying && paused) {
instance.playerRef.play();
}
if (!isPlaying && !paused) {
instance.playerRef.pause();
}
};
...
componentWillReceiveProps = createComponentWillReceiveProps(this)
...
<VideoReactPlayer ref={el => this.playerRef = el} preload='auto' src={source}>
...
并且测试看起来像这样:
it('calls playerRef.play when isPlaying prop is true & paused is true', () => {
const instance = {playerRef: {play: jest.fn(), getState: () => ({player: {paused: true}})}};
const componentWillReceiveProps = createComponentWillReceiveProps(instance);
componentWillReceiveProps({isPlaying: true});
expect(instance.playerRef.play).toBeCalled();
});