Имитация события onError аудиотега с помощью Enzyme - PullRequest
0 голосов
/ 30 сентября 2019

Мне нужно выполнить тест моего компонента, для этого мне нужно смоделировать событие onError аудиотега, а затем убедиться, что поведение моего компонента правильное. Кто-нибудь знает, есть ли способ имитировать это событие с ферментом?

Это мой компонент:

import React, { useState } from "react"

const Audio = ({ src }) => {
const [error, setState] = useState(false)

const setError = _ => setState(true)

return !error ? (
  <audio
    src={src}
    onError={setError}
    controls
  />
 ) : (
  <div className="audio_error_message">
    <i className="material-icons">warning</i>
    <p>This call has an audio, but at this time an error occurred.</p>
  </div>
 ) 
}

export default Audio

Мои тесты:

import React from 'react';
import { shallow, configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

/* Import Components */
import Audio from './Audio';

configure({adapter: new Adapter()});

describe("<Audio /> Component", () => {
   const src = `some-url`

   test("should match the snapshot", () => {
       const component = shallow(<Audio src={src} />);

       expect(component.html()).toMatchSnapshot();
   })


   test("should have han audio", () => {
      const component = shallow(<Audio src={src} />);

      expect(component.find('audio').length).toEqual(1)
   })

   test("display message error", () => {
      // what i am trying to do
      // expect(component.find('div.audio_error_message').length).toEqual(1)
   })


})
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...