4

为什么下面的代码会出现这个错误?我已经从“react-testing-library”导入了“模拟”,但错误指出“模拟”是“未定义”

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);
  });
});

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

4

2 回答 2

3

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

你可以这样使用它:

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

// Then in your test

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

您可以fireEvent官方文档中了解更多信息。

于 2019-02-25T13:03:42.683 回答
-1

尝试过 var test = new Simulate(); ?

然后做 test.etc ......

于 2019-02-25T12:44:00.803 回答