我在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');  
  });

});

推荐答案

jsdom不支持任何播放或暂停媒体操作.作为一种解决办法,您可以在测试设置中添加一些修改:

我有Home个组成部分:

function Home(){

    return(
        <div>
            <video data-testid="video" autoPlay src="./video.webm"></video>
        </div>
    )
}

Jest

describe("Home Page",function(){

    test("Initial rendering",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 Promise.resolve(true);
        };
        window.HTMLMediaElement.prototype.pause = function(){
            this.dispatchEvent(new Event('paused'));
            this.paused = true;
            return Promise.resolve(true);
        };
        render(<Home/>);
        const video = screen.getAllByTestId("video")[0];
        await video.play();
        expect(video.paused).toBe(false);
        await video.pause();
        expect(video.paused).toBe(true);
    })

})

Reactjs相关问答推荐

更新数据时有关CQR和更新UI的问题

LocalStore未存储正确的数据

REACT路由DOM根据参数呈现不同的路由

将动态元素中的输入数据传输到Reaction

XChaCha20-Poly1305的解密函数

react -在选项中 Select 我想要显示和图像.但当我 Select 该选项时,我希望控件仅显示该选项的文本部分

获取未捕获的错误:Gatsby生产版本上的最小化react 错误#418

为什么在页面重新加载时Location.State变得未定义?

useRef()的钩子调用无效

如何删除 bootstrap 手风琴上的边框

在reactjs中刷新页面时丢失状态值

React useEffect 依赖项

获取 不能显示为 的后代.错误,但不太确定如何构建我的代码以避免此警告

使用 toast 时挂钩调用无效

为什么刷新页面时我的localStorage PET值变成空字符串?

如何通过 id 从 useSprings 数组中删除元素

如何通过单击多个选项中的特定项目在模态上呈现不同的标题?

React Router 6.4CreateBrowserRouter子元素不显示

是什么导致了这个令人惊讶的数组导致 React

将鼠标悬停在仅适用于该类的第一个实例的 p5.js 类上