我在Reaction中有一个登录页面,上面有一个自动加载的视频播放.
在运行测试期间,我得到以下错误
console.error
Error: Not implemented: HTMLMediaElement.prototype.play
我的测试代码
import { render, screen } from '@testing-library/react';
import App from './App';
test('Rendering landing page', () => {
/*
const playStub = jest
.spyOn(window.HTMLMediaElement.prototype, 'play')
.mockImplementation(() => {})
// trigger the code that you would expect to call the pause function
//expect(playStub).toHaveBeenCalled()
playStub.mockRestore()
*/
const { container } = render(<App />);
const spy = jest.spyOn(window.HTMLMediaElement.prototype, 'play');
//const isPlaying = video.play();
//expect(spy).toHaveBeenCalled();
//expect(isPlaying).toBe(true);
let link = screen.getAllByText('Signin')[0];
expect(link.href).toContain('/signin-login');
});
我try 了一下,得到一个不同的错误--"Console.Error 错误:未捕获[TypeError:Promise.Then不是函数]"
import { render, screen } from '@testing-library/react';
import App from './App';
describe("Home Page",function(){
test('Rendering landing page', async function(){
Object.defineProperty(window.HTMLMediaElement.prototype, "paused", {
value:true,
writable:true
});
window.HTMLMediaElement.prototype.play = function(){
this.dispatchEvent(new Event('play'));
this.paused = false;
return true;
};
window.HTMLMediaElement.prototype.pause = function(){
this.dispatchEvent(new Event('paused'));
this.paused = true;
return true;
};
render(<App />);
let link = screen.getAllByText('Signin')[0];
expect(link.href).toContain('/signin-login');
});
});