JohnByte
JohnByte

Reputation: 309

TypeError: Cannot read property 'change' of undefined

Why does this error occur in the following code? I have already imported 'Simulate' from 'react-testing-library' but the error states that 'Simulate' is '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);
  });
});

I was searching for this issue but couldn't find any information about 'Simulate' behavior

Upvotes: 4

Views: 1783

Answers (2)

Gio Polvara
Gio Polvara

Reputation: 26978

Simulate has been removed from react-testing-library. What you need to use today is fireEvent.

You can use it in this way:

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

// Then in your test

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

You can read more about fireEvent in the official docs.

Upvotes: 3

Gianluca Filitz
Gianluca Filitz

Reputation: 431

tryed var test = new Simulate(); ?

And then do test.etc....

Upvotes: -2

Related Questions