TypeError:无法读取未定义的属性“更改”

时间:2019-02-25 11:58:21

标签: javascript reactjs react-testing-library

为什么在以下代码中会发生此错误?我已经从“ react-testing-library”中导入了“ Simulate”,但是错误指出“ Simulate”是“ undefined”

import React from 'react';
import { render, Simulate } from 'react-testing-library';
import CommentFeed from './CommentFeed';

const createProps = props => ({
header: 'Comment Feed',
comments: [
   {
      author: 'Ian Wilson',
      text: 'A boats a boat but a mystery box could be anything.',
   },
   {
     author: 'Max Powers Jr',
     text: 'Krypton sucks.',
   },
  ],
  createComment: jest.fn(),
  ...props,
})

describe('CommentFeed', () => {
  const props = { header : 'Comment Feed', comments : []};

  /* ... */

  it('allows the user to add a comment', () => {
    // Arrange
    const newComment = { author: 'Socrates', text: 'Why?' };
    let props = createProps();
    const { container, getByLabelText } = render(<CommentFeed {...props} />);

    const authorNode = getByLabelText('Author');
    const textNode = getByLabelText('Comment');
    const formNode = container.querySelector('form');

   // Act
    authorNode.value = newComment.author;
    textNode.value = newComment.text;

    Simulate.change(authorNode); // Where the error occurs
    Simulate.change(textNode);

    Simulate.submit(formNode);

    // Assert
    expect(props.createComment).toHaveBeenCalledTimes(1);
    expect(props.createComment).toHaveBeenCalledWith(newComment);
  });
});

我正在搜索此问题,但找不到有关“模拟”行为的任何信息

2 个答案:

答案 0 :(得分:1)

Simulate已从react-testing-library中删除。您今天需要使用的是fireEvent

您可以通过以下方式使用它:

import { fireEvent } from 'react-testing-library'

// Then in your test

fireEvent.change(authorNode, { target: { value: newComment.author } })

您可以在official docs中阅读有关fireEvent的更多信息。

答案 1 :(得分:-1)

tryed var test = new Simulate(); ?

然后进行测试等等。