Файл с изображением для проверки - PullRequest
0 голосов
/ 19 апреля 2019

У меня есть компонент, который показывает эскиз загруженного изображения, который выглядит следующим образом:

class Thumb extends Component {
  state = {
    loading: false,
    thumb: undefined,
  };

  componentWillReceiveProps(nextProps) {
    if (!nextProps.file) {
      return;
    }

    this.setState({ loading: true }, () => {
      const reader = new FileReader();

      reader.onloadend = () => {
        this.setState({ loading: false, thumb: reader.result });
      };

      reader.readAsDataURL(nextProps.file);
    });
  }

  render() {
    const { file } = this.props;
    const { loading, thumb } = this.state;
    const imageStyle = {
      maxWidth: '100%',
      height: 'auto',
      marginTop: 10,
    };

    if (!file) {
      return null;
    }

    if (loading) {
      return <p>loading...</p>;
    }

    return (
      <img
        alt={file.name}
        height={200}
        src={thumb}
        style={imageStyle}
        width={200}
      />
    );
  }
}

export default Thumb;

Это почти прямо вверх от: https://hackernoon.com/formik-handling-files-and-recaptcha-209cbeae10bc. Я хочу иметь возможность протестировать этот компонент с помощью теста моментальных снимков, но я не уверен, как смоделировать его (файл изображения).

Мой текущий тестовый файл выглядит так:

import React from 'react';
import { shallow } from 'enzyme';
import { Thumb } from 'admin/components/FormControls/Thumbnail';
import errorImage from 'admin/assets/error.png';

describe('<Thumb />', () => {
  it('should render the correct snapshot', () => {
    const wrapper = shallow(<Thumb file={errorImage} />);
    expect(wrapper).toMatchSnapshot();
  });
});

Это бросает мне TypeError: Cannot read property 'prototype' of undefined в настоящее время.

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